diff --git a/src/Session/Base.php b/src/Session/Base.php index 653e275..ff181ad 100644 --- a/src/Session/Base.php +++ b/src/Session/Base.php @@ -14,7 +14,7 @@ class Base implements SessionInterface public function live(): bool { - return session_status() === PHP_SESSION_ACTIVE; + return \session_status() === PHP_SESSION_NONE || \session_status() === PHP_SESSION_ACTIVE; } public function tryStore(string $key, mixed $val): bool diff --git a/tests/SessionTest.php b/tests/SessionTest.php new file mode 100644 index 0000000..06fe6a2 --- /dev/null +++ b/tests/SessionTest.php @@ -0,0 +1,56 @@ +assertTrue($session->live()); + } + + public function testPreservesValues(): void + { + $key = "testVar"; + $value = "someData"; + + $session = new BaseSession(); + + $session->tryStore($key, $value); + + $this->assertSame($session->get($key), $value); + } + + public function testTryStoreNeverOverwrites(): void + { + $key = "test"; + $value1 = "someImportantData"; + $value2 = "DataArrivingLate"; + + $session = new BaseSession(); + + $this->assertTrue($session->tryStore($key, $value1)); + $this->assertTrue(!$session->tryStore($key, $value2)); + + $this->assertSame($session->get($key), $value1); + } + + public function testStoreOverwrites(): void + { + $key = "test"; + $value1 = "initialData"; + $value2 = "overwriteData"; + + $session = new BaseSession(); + + $session->store($key, $value1); + $session->store($key, $value2); + + $this->assertSame($session->get($key), $value2); + } +} +?> diff --git a/tests/URITest.php b/tests/URITest.php index c9a726c..a7f2395 100644 --- a/tests/URITest.php +++ b/tests/URITest.php @@ -4,19 +4,34 @@ use PHPUnit\Framework\TestCase; use BradyMcD\TAATP\URI\Otpauth; +// For more information about the specification, refer to google's documentation on github. +// https://github.com/google/google-authenticator/wiki/Key-Uri-Format /** @SuppressWarnings(PHPMD.StaticAccess)*/ final class URITest extends TestCase { + public function testRejectsInvalidUri(): void + { + $string = 'totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=MD5&digits=8&period=60'; + + $this->expectException(\InvalidArgumentException::class); + + Otpauth::fromString($string); + } + + + public function testRejectsMissingSecretInformation(): void + { + $string = 'otpauth://totp/ACME%20Co:john.doe@email.com?issuer=ACME%20Co&algorithm=MD5&digits=8&period=60'; + + $this->expectException(\InvalidArgumentException::class); + + Otpauth::fromString($string); + } + public function testPreservesAllUriFields(): void { - // Sourced from Google's otpauth URI specification - // https://github.com/google/google-authenticator/wiki/Key-Uri-Format $string = 'otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=MD5&digits=8&period=60'; - $urlComponents = [ "scheme" => "otpauth", - "host" => "totp", - "path" => "ACME%20Co:john.doe@email.com/", - "query" => "secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30",]; $queryComponents = [ "secret" => "HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ", "issuer" => "ACME Co", "algorithm" => "MD5",