. */ namespace SP\Tests\Core\Crypt; use Defuse\Crypto\Exception\EnvironmentIsBrokenException; use Defuse\Crypto\Key; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\MockObject\MockObject; use SessionHandler; use SP\Core\Context\ContextException; use SP\Core\Crypt\CryptSessionHandler; use SP\Domain\Core\Crypt\CryptInterface; use SP\Domain\Core\Exceptions\CryptException; use SP\Tests\UnitaryTestCase; /** * Class CryptSessionHandlerTest */ #[Group('unitary')] class CryptSessionHandlerTest extends UnitaryTestCase { private MockObject|CryptInterface $crypt; private CryptSessionHandler $cryptSessionHandler; private MockObject|SessionHandler $sessionHandler; private Key $key; public function testRead() { $this->sessionHandler ->expects($this->once()) ->method('read') ->with('test') ->willReturn('session_data'); $this->crypt ->expects($this->once()) ->method('decrypt') ->with('session_data', $this->key) ->willReturn('decrypted_session_data'); $out = $this->cryptSessionHandler->read('test'); $this->assertEquals('decrypted_session_data', $out); } public function testReadWithNodata() { $this->sessionHandler ->expects($this->once()) ->method('read') ->with('test') ->willReturn(false); $this->crypt ->expects($this->never()) ->method('decrypt'); $out = $this->cryptSessionHandler->read('test'); $this->assertEmpty($out); } public function testReadWithException() { $this->sessionHandler ->expects($this->once()) ->method('read') ->with('test') ->willReturn('session_data'); $this->crypt ->expects($this->once()) ->method('decrypt') ->willThrowException(CryptException::error('test')); $out = $this->cryptSessionHandler->read('test'); $this->assertEquals('session_data', $out); } public function testWrite() { $data = serialize(['a' => 'testA']); $this->sessionHandler ->expects($this->once()) ->method('write') ->with('test', 'encrypted_session_data') ->willReturn(true); $this->crypt ->expects($this->once()) ->method('encrypt') ->with($data, $this->key) ->willReturn('encrypted_session_data'); $this->assertTrue($this->cryptSessionHandler->write('test', $data)); } public function testWriteWithException() { $data = serialize(['a' => 'testA']); $this->sessionHandler ->expects($this->once()) ->method('write') ->with('test', $data) ->willReturn(true); $this->crypt ->expects($this->once()) ->method('encrypt') ->willThrowException(CryptException::error('test')); $this->assertTrue($this->cryptSessionHandler->write('test', $data)); } public function testClose() { $this->sessionHandler ->expects($this->once()) ->method('close'); $this->cryptSessionHandler->close(); } public function testDestroy() { $this->sessionHandler ->expects($this->once()) ->method('destroy') ->with('test'); $this->cryptSessionHandler->destroy('test'); } public function testGc() { $this->sessionHandler ->expects($this->once()) ->method('gc') ->with(1000); $this->cryptSessionHandler->gc(1000); } public function testOpen() { $this->sessionHandler ->expects($this->once()) ->method('open') ->with('a_path', 'test'); $this->cryptSessionHandler->open('a_path', 'test'); } /** * @throws ContextException * @throws Exception * @throws EnvironmentIsBrokenException */ protected function setUp(): void { parent::setUp(); $this->key = Key::createNewRandomKey(); $this->crypt = $this->createMock(CryptInterface::class); $this->sessionHandler = $this->createMock(SessionHandler::class); $this->cryptSessionHandler = new CryptSessionHandler($this->key, $this->crypt, $this->sessionHandler); } }