diff --git a/.cookies_test/.gitkeep b/.cookies_test/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/.gitignore b/.gitignore index 5a0be5c..99ed9f9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ *.swm tags .cookies +.cookies_test /.phpunit.result.cache diff --git a/src/Authentication/Authenticator.php b/src/Authentication/Authenticator.php index d9b367f..c7671ac 100644 --- a/src/Authentication/Authenticator.php +++ b/src/Authentication/Authenticator.php @@ -9,5 +9,11 @@ abstract class Authenticator { abstract public function login(int $mglnr, string $password): self; abstract public function http(): PendingRequest; abstract public function isLoggedIn(): bool; + protected static $path = __DIR__.'/../../.cookies'; + + public static function setPath(string $path): void + { + static::$path = $path; + } } diff --git a/src/Authentication/MainCookie.php b/src/Authentication/MainCookie.php index 9dc15d2..d80fbfe 100644 --- a/src/Authentication/MainCookie.php +++ b/src/Authentication/MainCookie.php @@ -10,7 +10,6 @@ use Zoomyboy\LaravelNami\LoginException; class MainCookie extends Authenticator { - private string $path = __DIR__.'/../../.cookies'; private FileCookieJar $cookie; private string $url = 'https://nami.dpsg.de'; @@ -59,7 +58,7 @@ class MainCookie extends Authenticator { private function newFileName(): string { - return $this->path.'/'.time().'.txt'; + return parent::$path.'/'.time().'.txt'; } private function isExpired(): bool @@ -76,7 +75,7 @@ class MainCookie extends Authenticator { */ private function file(): ?string { - $files = glob($this->path.'/*'); + $files = glob(parent::$path.'/*'); if (!count($files)) { return null; diff --git a/tests/TestCase.php b/tests/TestCase.php index 15c0cc7..4dca63e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,7 +4,9 @@ namespace Zoomyboy\LaravelNami\Tests; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Storage; use Zoomyboy\LaravelNami\Api; +use Zoomyboy\LaravelNami\Authentication\Authenticator; use Zoomyboy\LaravelNami\Cookies\Cookie; use Zoomyboy\LaravelNami\Cookies\FakeCookie; use Zoomyboy\LaravelNami\Nami; @@ -17,7 +19,7 @@ class TestCase extends \Orchestra\Testbench\TestCase public function setUp(): void { parent::setUp(); - $this->clearCookies(); + $this->setupCookies(); } protected function getPackageProviders($app) @@ -37,14 +39,16 @@ class TestCase extends \Orchestra\Testbench\TestCase public function login(): Api { - touch (__DIR__.'/../.cookies/'.time().'.txt'); + touch (__DIR__.'/../.cookies_test/'.time().'.txt'); return Nami::login(123, 'secret'); } - private function clearCookies(): void + private function setupCookies(): void { - foreach (glob(__DIR__.'/../.cookies/*') as $file) { + Authenticator::setPath(__DIR__.'/../.cookies_test'); + + foreach (glob(__DIR__.'/../.cookies_test/*') as $file) { unlink($file); } } diff --git a/tests/Unit/LoginTest.php b/tests/Unit/LoginTest.php index d0a1281..6a8a090 100644 --- a/tests/Unit/LoginTest.php +++ b/tests/Unit/LoginTest.php @@ -57,12 +57,12 @@ class LoginTest extends TestCase Nami::login(12345, 'secret'); - $this->assertFileExists(__DIR__.'/../../.cookies/'.time().'.txt'); + $this->assertFileExists(__DIR__.'/../../.cookies_test/'.time().'.txt'); } public function test_dont_login_if_cookie_exists(): void { - touch(__DIR__.'/../../.cookies/'.time().'.txt'); + touch(__DIR__.'/../../.cookies_test/'.time().'.txt'); Nami::login(12345, 'secret'); @@ -72,19 +72,19 @@ class LoginTest extends TestCase public function test_delete_expired_cookie_before_login(): void { $lastLogin = now()->subHours(2)->timestamp; - touch(__DIR__."/../../.cookies/{$lastLogin}.txt"); + touch(__DIR__."/../../.cookies_test/{$lastLogin}.txt"); Http::fake($this->fakeSuccessfulLogin()); Nami::login(12345, 'secret'); Http::assertSentCount(2); - $this->assertFileDoesNotExist(__DIR__."/../../.cookies/{$lastLogin}.txt"); + $this->assertFileDoesNotExist(__DIR__."/../../.cookies_test/{$lastLogin}.txt"); } public function test_login_once_if_cookie_is_expired(): void { $lastLogin = now()->subHour()->subMinutes(10)->timestamp; - touch(__DIR__."/../../.cookies/{$lastLogin}.txt"); + touch(__DIR__."/../../.cookies_test/{$lastLogin}.txt"); Http::fake($this->fakeSuccessfulLogin()); Nami::login(12345, 'secret');