cacheMock = $this->createMock(CacheInterface::class); $this->cacheItemMock = $this->createMock(CacheItemInterface::class); $this->loggerMock = $this->createMock(Logger::class); $this->subject = new ItemPoolSimpleCacheAdapter(); $this->subject->setLogger($this->loggerMock); $this->subject->setServiceLocator( $this->getServiceLocatorMock( [ SimpleCache::SERVICE_ID => $this->cacheMock, ] ) ); } public function testGetItemThatExistInCache(): void { $this->cacheMock ->expects($this->once()) ->method('get') ->with('key') ->willReturn('value'); $result = $this->subject->getItem('key'); $this->assertInstanceOf(CacheItem::class, $result); $this->assertSame('value', $result->get()); $this->assertSame('key', $result->getKey()); $this->assertTrue($result->isHit()); } public function testGetItemThatNotExistInCache(): void { $this->cacheMock ->expects($this->once()) ->method('get') ->with('key') ->willReturn(null); $result = $this->subject->getItem('key'); $this->assertInstanceOf(CacheItem::class, $result); $this->assertSame('key', $result->getKey()); $this->assertNull($result->get()); $this->assertFalse($result->isHit()); } public function testGetItems(): void { $this->cacheMock ->method('get') ->withConsecutive( ['key1'], ['key2'] ) ->willReturnOnConsecutiveCalls('value1', 'value2'); $result = $this->subject->getItems(['key1', 'key2']); $this->assertIsArray($result); $this->assertInstanceOf(CacheItem::class, $result[0]); $this->assertInstanceOf(CacheItem::class, $result[1]); $this->assertSame('value1', $result[0]->get()); $this->assertSame('value2', $result[1]->get()); } public function testHasItem(): void { $this->cacheMock ->expects($this->once()) ->method('has') ->with('key') ->willReturn(true); $result = $this->subject->hasItem('key'); $this->assertTrue($result); } public function testClear(): void { $this->cacheMock ->expects($this->once()) ->method('clear') ->willReturn(true); $this->assertTrue($this->subject->clear()); } public function testDeleteItem(): void { $this->cacheMock ->expects($this->once()) ->method('delete') ->with('key') ->willReturn(true); $this->assertTrue($this->subject->deleteItem('key')); } public function testDeleteItems(): void { $this->cacheMock ->expects($this->once()) ->method('deleteMultiple') ->with(['key1', 'key2']) ->willReturn(true); $this->assertTrue($this->subject->deleteItems(['key1', 'key2'])); } public function testSave(): void { $this->cacheItemMock ->expects($this->once()) ->method('getKey') ->willReturn('key'); $this->cacheItemMock ->expects($this->once()) ->method('get') ->willReturn('value'); $this->cacheMock ->expects($this->once()) ->method('set') ->with('key', 'value') ->willReturn(true); $this->assertTrue($this->subject->save($this->cacheItemMock)); } public function testSaveCatchError(): void { $this->cacheMock ->method('set') ->willThrowException(new Exception()); $this->loggerMock ->expects($this->once()) ->method('error'); $this->assertFalse($this->subject->save($this->cacheItemMock)); } public function testSaveDeferredAndCommit(): void { $this->cacheItemMock ->expects($this->exactly(4)) ->method('getKey') ->willReturnOnConsecutiveCalls( 'key1', 'key2', 'key1', 'key2' ); $this->cacheItemMock ->expects($this->exactly(2)) ->method('get') ->willReturnOnConsecutiveCalls( 'value1', 'value2' ); $this->cacheMock ->expects($this->exactly(2)) ->method('set') ->withConsecutive( ['key1', 'value1'], ['key2', 'value2'] )->willReturnOnConsecutiveCalls( true, true ); $this->subject->saveDeferred($this->cacheItemMock); $this->subject->saveDeferred($this->cacheItemMock); $this->assertTrue($this->subject->commit()); } public function testCommitCatchError(): void { $this->cacheMock ->method('set') ->willThrowException(new Exception()); $this->loggerMock ->expects($this->once()) ->method('error'); $this->subject->saveDeferred($this->cacheItemMock); $this->assertFalse($this->subject->commit()); } }