Skip to content

Add tests for cached metadata classes #839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,8 @@ public function create(string $resourceClass, string $property, array $options =
return $propertyMetadata;
}

try {
$cacheItem->set($propertyMetadata);
$this->cacheItemPool->save($cacheItem);
} catch (CacheException $e) {
// do nothing
}
$cacheItem->set($propertyMetadata);
$this->cacheItemPool->save($cacheItem);

return $propertyMetadata;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,8 @@ public function create(string $resourceClass, array $options = []): PropertyName
return $propertyNameCollection;
}

try {
$cacheItem->set($propertyNameCollection);
$this->cacheItemPool->save($cacheItem);
} catch (CacheException $e) {
// do nothing
}
$cacheItem->set($propertyNameCollection);
$this->cacheItemPool->save($cacheItem);

return $propertyNameCollection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,8 @@ public function create(string $resourceClass): ResourceMetadata
return $resourceMetadata;
}

try {
$cacheItem->set($resourceMetadata);
$this->cacheItemPool->save($cacheItem);
} catch (CacheException $e) {
// do nothing
}
$cacheItem->set($resourceMetadata);
$this->cacheItemPool->save($cacheItem);

return $resourceMetadata;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Tests\Metadata\Property\Factory;

use ApiPlatform\Core\Metadata\Property\Factory\CachedPropertyMetadataFactory;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use Psr\Cache\CacheException;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;

/**
* @author Baptiste Meyer <[email protected]>
*/
class CachedPropertyMetadataFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCreateWithItemHit()
{
$cacheItem = $this->prophesize(CacheItemInterface::class);
$cacheItem->isHit()->willReturn(true)->shouldBeCalled();
$cacheItem->get()->willReturn(new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false))->shouldBeCalled();

$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalled();

$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);

$cachedPropertyMetadataFactory = new CachedPropertyMetadataFactory($cacheItemPool->reveal(), $decoratedPropertyMetadataFactory->reveal());
$resultedPropertyMetadata = $cachedPropertyMetadataFactory->create(Dummy::class, 'dummy');

$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
$this->assertEquals(new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false), $resultedPropertyMetadata);
}

public function testCreateWithItemNotHit()
{
$propertyMetadata = new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false);

$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$decoratedPropertyMetadataFactory->create(Dummy::class, 'dummy', [])->willReturn($propertyMetadata)->shouldBeCalled();

$cacheItem = $this->prophesize(CacheItemInterface::class);
$cacheItem->isHit()->willReturn(false)->shouldBeCalled();
$cacheItem->set($propertyMetadata)->willReturn($cacheItem->reveal())->shouldBeCalled();

$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalled();
$cacheItemPool->save($cacheItem->reveal())->willReturn(true)->shouldBeCalled();

$cachedPropertyMetadataFactory = new CachedPropertyMetadataFactory($cacheItemPool->reveal(), $decoratedPropertyMetadataFactory->reveal());
$resultedPropertyMetadata = $cachedPropertyMetadataFactory->create(Dummy::class, 'dummy');

$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
$this->assertEquals(new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false), $resultedPropertyMetadata);
}

public function testCreateWithGetCacheItemThrowsCacheException()
{
$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$decoratedPropertyMetadataFactory->create(Dummy::class, 'dummy', [])->willReturn(new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false))->shouldBeCalled();

$cacheException = $this->prophesize(CacheException::class);
$cacheException->willExtend(\Exception::class);

$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPool->getItem($this->generateCacheKey())->willThrow($cacheException->reveal())->shouldBeCalled();

$cachedPropertyMetadataFactory = new CachedPropertyMetadataFactory($cacheItemPool->reveal(), $decoratedPropertyMetadataFactory->reveal());
$resultedPropertyMetadata = $cachedPropertyMetadataFactory->create(Dummy::class, 'dummy');

$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
$this->assertEquals(new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false), $resultedPropertyMetadata);
}

private function generateCacheKey(string $resourceClass = Dummy::class, string $property = 'dummy', array $options = [])
{
return CachedPropertyMetadataFactory::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $property, $options]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Tests\Metadata\Property\Factory;

use ApiPlatform\Core\Metadata\Property\Factory\CachedPropertyNameCollectionFactory;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use Psr\Cache\CacheException;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;

/**
* @author Baptiste Meyer <[email protected]>
*/
class CachedPropertyNameCollectionFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCreateWithItemHit()
{
$cacheItem = $this->prophesize(CacheItemInterface::class);
$cacheItem->isHit()->willReturn(true)->shouldBeCalled();
$cacheItem->get()->willReturn(new PropertyNameCollection(['id', 'name', 'description', 'dummy']))->shouldBeCalled();

$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalled();

$decoratedPropertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class);

$cachedPropertyNameCollectionFactory = new CachedPropertyNameCollectionFactory($cacheItemPool->reveal(), $decoratedPropertyNameCollectionFactory->reveal());
$resultedPropertyNameCollection = $cachedPropertyNameCollectionFactory->create(Dummy::class);

$this->assertInstanceOf(PropertyNameCollection::class, $resultedPropertyNameCollection);
$this->assertEquals(new PropertyNameCollection(['id', 'name', 'description', 'dummy']), $resultedPropertyNameCollection);
}

public function testCreateWithItemNotHit()
{
$resourceNameCollection = new PropertyNameCollection(['id', 'name', 'description', 'dummy']);

$decoratedPropertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$decoratedPropertyNameCollectionFactory->create(Dummy::class, [])->willReturn($resourceNameCollection)->shouldBeCalled();

$cacheItem = $this->prophesize(CacheItemInterface::class);
$cacheItem->isHit()->willReturn(false)->shouldBeCalled();
$cacheItem->set($resourceNameCollection)->willReturn($cacheItem->reveal())->shouldBeCalled();

$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalled();
$cacheItemPool->save($cacheItem->reveal())->willReturn(true)->shouldBeCalled();

$cachedPropertyNameCollectionFactory = new CachedPropertyNameCollectionFactory($cacheItemPool->reveal(), $decoratedPropertyNameCollectionFactory->reveal());
$resultedPropertyNameCollection = $cachedPropertyNameCollectionFactory->create(Dummy::class);

$this->assertInstanceOf(PropertyNameCollection::class, $resultedPropertyNameCollection);
$this->assertEquals(new PropertyNameCollection(['id', 'name', 'description', 'dummy']), $resultedPropertyNameCollection);
}

public function testCreateWithGetCacheItemThrowsCacheException()
{
$decoratedPropertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$decoratedPropertyNameCollectionFactory->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['id', 'name', 'description', 'dummy']))->shouldBeCalled();

$cacheException = $this->prophesize(CacheException::class);
$cacheException->willExtend(\Exception::class);

$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPool->getItem($this->generateCacheKey())->willThrow($cacheException->reveal())->shouldBeCalled();

$cachedPropertyNameCollectionFactory = new CachedPropertyNameCollectionFactory($cacheItemPool->reveal(), $decoratedPropertyNameCollectionFactory->reveal());
$resultedPropertyNameCollection = $cachedPropertyNameCollectionFactory->create(Dummy::class);

$this->assertInstanceOf(PropertyNameCollection::class, $resultedPropertyNameCollection);
$this->assertEquals(new PropertyNameCollection(['id', 'name', 'description', 'dummy']), $resultedPropertyNameCollection);
}

private function generateCacheKey(string $resourceClass = Dummy::class, array $options = [])
{
return CachedPropertyNameCollectionFactory::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $options]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Tests\Metadata\Resource\Factory;

use ApiPlatform\Core\Metadata\Resource\Factory\CachedResourceMetadataFactory;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use Psr\Cache\CacheException;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;

/**
* @author Baptiste Meyer <[email protected]>
*/
class CachedResourceMetadataFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCreateWithItemHit()
{
$cacheItem = $this->prophesize(CacheItemInterface::class);
$cacheItem->isHit()->willReturn(true)->shouldBeCalled();
$cacheItem->get()->willReturn(new ResourceMetadata(null, 'Dummy.'))->shouldBeCalled();

$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalled();

$decoratedResourceMetadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class);

$cachedResourceMetadataFactory = new CachedResourceMetadataFactory($cacheItemPool->reveal(), $decoratedResourceMetadataFactory->reveal());
$resultedResourceMetadata = $cachedResourceMetadataFactory->create(Dummy::class);

$this->assertInstanceOf(ResourceMetadata::class, $resultedResourceMetadata);
$this->assertEquals(new ResourceMetadata(null, 'Dummy.'), $resultedResourceMetadata);
}

public function testCreateWithItemNotHit()
{
$propertyMetadata = new ResourceMetadata(null, 'Dummy.');

$decoratedResourceMetadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class);
$decoratedResourceMetadataFactory->create(Dummy::class)->willReturn($propertyMetadata)->shouldBeCalled();

$cacheItem = $this->prophesize(CacheItemInterface::class);
$cacheItem->isHit()->willReturn(false)->shouldBeCalled();
$cacheItem->set($propertyMetadata)->willReturn($cacheItem->reveal())->shouldBeCalled();

$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalled();
$cacheItemPool->save($cacheItem->reveal())->willReturn(true)->shouldBeCalled();

$cachedResourceMetadataFactory = new CachedResourceMetadataFactory($cacheItemPool->reveal(), $decoratedResourceMetadataFactory->reveal());
$resultedResourceMetadata = $cachedResourceMetadataFactory->create(Dummy::class);

$this->assertInstanceOf(ResourceMetadata::class, $resultedResourceMetadata);
$this->assertEquals(new ResourceMetadata(null, 'Dummy.'), $resultedResourceMetadata);
}

public function testCreateWithGetCacheItemThrowsCacheException()
{
$decoratedResourceMetadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class);
$decoratedResourceMetadataFactory->create(Dummy::class)->willReturn(new ResourceMetadata(null, 'Dummy.'))->shouldBeCalled();

$cacheException = $this->prophesize(CacheException::class);
$cacheException->willExtend(\Exception::class);

$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPool->getItem($this->generateCacheKey())->willThrow($cacheException->reveal())->shouldBeCalled();

$cachedResourceMetadataFactory = new CachedResourceMetadataFactory($cacheItemPool->reveal(), $decoratedResourceMetadataFactory->reveal());
$resultedResourceMetadata = $cachedResourceMetadataFactory->create(Dummy::class);

$this->assertInstanceOf(ResourceMetadata::class, $resultedResourceMetadata);
$this->assertEquals(new ResourceMetadata(null, 'Dummy.'), $resultedResourceMetadata);
}

private function generateCacheKey(string $resourceClass = Dummy::class)
{
return CachedResourceMetadataFactory::CACHE_KEY_PREFIX.md5(serialize([$resourceClass]));
}
}