appConfig = $this->createStub(AppConfig::class); $this->appConfig->method("getSKey")->willReturn("secret"); $this->crypt = new Crypt($this->appConfig); } /** * Encodes a payload with getHash() and decodes it back to the original via readHash(), with no error. */ public function testGetHashAndReadHash(): void { $data = [ "key" => "1234567890", "name" => "John Doe", ]; $token = $this->crypt->getHash($data); $this->assertNotEmpty($token); $decoded = $this->crypt->readHash($token); $this->assertCount(2, $decoded); $this->assertEquals((object)$data, $decoded[0]); $this->assertNull($decoded[1]); } /** * Returns a null payload and a descriptive error message for an empty token, rather than throwing. */ public function testReadHashWithEmptyToken(): void { $decoded = $this->crypt->readHash(""); $this->assertNull($decoded[0]); $this->assertSame("token is empty", $decoded[1]); } /** * Returns a null payload and a non-null error message when given a malformed token that cannot be decoded. */ public function testReadHashWithInvalidToken(): void { $decoded = $this->crypt->readHash("invalid_token"); $this->assertNull($decoded[0]); $this->assertNotNull($decoded[1]); } }