cache = $this->prophesize(Cache::class); $this->repository = $this->prophesize(DataboxRepositoryInterface::class); $this->factory = $this->prophesize(DataboxFactory::class); $this->sut = new CachedDataboxRepository( $this->repository->reveal(), $this->cache->reveal(), $this->cacheKey, $this->factory->reveal() ); } public function testItImplementsDataboxRepositoryInterface() { $this->assertInstanceOf(DataboxRepositoryInterface::class, $this->sut); } public function testItFindsASpecificDataboxWhenNotInCache() { $databox = $this->prophesize(\databox::class); $this->cache->fetch($this->cacheKey) ->willReturn(false); $this->repository->find(42) ->willReturn($databox->reveal()); $this->assertSame($databox->reveal(), $this->sut->find(42)); } public function testItHydrateDataboxWhenInCache() { $databox = $this->prophesize(\databox::class); $this->cache->fetch($this->cacheKey) ->willReturn([42 => ['foo' => 'bar']]); $this->repository->find(42) ->shouldNotBeCalled(); $this->factory->create(42, ['foo' => 'bar']) ->willReturn($databox->reveal()); $this->assertSame($databox->reveal(), $this->sut->find(42)); } public function testItProperlySaveCacheOnFindAll() { $databox = $this->prophesize(\databox::class); $databox->get_sbas_id() ->willReturn(42); $databox->getRawData() ->willReturn(['foo' => 'bar']); $cache_data = [42 => ['foo' => 'bar']]; $databoxes = [42 => $databox->reveal()]; $this->cache->fetch($this->cacheKey) ->willReturn(false); $this->repository->findAll() ->willReturn($databoxes); $this->cache->save($this->cacheKey, $cache_data) ->shouldBeCalled(); $this->factory->createMany(Argument::any()) ->shouldNotBeCalled(); $this->assertSame($databoxes, $this->sut->findAll()); } public function testItFindsAllDeclaredDataboxesFromCache() { $databox = $this->prophesize(\databox::class); $cache_data = [42 => ['foo' => 'bar']]; $databoxes = [42 => $databox->reveal()]; $this->cache->fetch($this->cacheKey) ->willReturn($cache_data); $this->repository->findAll() ->shouldNotBeCalled(); $this->factory->createMany($cache_data) ->willReturn($databoxes); $this->assertSame($databoxes, $this->sut->findAll()); } }