Skip to content

Instantly share code, notes, and snippets.

@tarlepp
Created May 18, 2017 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarlepp/9467664383f2e4024552e4b547b10749 to your computer and use it in GitHub Desktop.
Save tarlepp/9467664383f2e4024552e4b547b10749 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
/**
* /tests/Integration/Resource/GenericResourceTest.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@protacon.com>
*/
namespace Integration\Resource;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Resource\UserResource;
use App\Rest\Interfaces\Resource as ResourceInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use PHPUnit_Framework_MockObject_MockBuilder;
use PHPUnit_Framework_MockObject_MockObject;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Class GenericResourceTest
*
* @package Integration\Resource
* @author TLe, Tarmo Leppänen <tarmo.leppanen@protacon.com>
*/
class GenericResourceTest extends KernelTestCase
{
protected $entityClass = User::class;
protected $resourceClass = UserResource::class;
protected $repositoryClass = UserRepository::class;
/**
* @return ValidatorInterface
*/
private static function getValidator(): ValidatorInterface
{
return static::$kernel->getContainer()->get('validator');
}
/**
* @return EntityManagerInterface|Object
*/
private static function getEntityManager(): EntityManagerInterface
{
return static::$kernel->getContainer()->get('doctrine.orm.entity_manager');
}
public function setUp(): void
{
parent::setUp();
static::bootKernel();
}
/**
* @expectedException \UnexpectedValueException
* @expectedExceptionMessage DTO class not specified for
*/
public function testThatGetDtoClassThrowsAnExceptionWithoutDto(): void
{
/** @var PHPUnit_Framework_MockObject_MockObject|UserRepository $repository */
$repository = $this->getRepositoryMockBuilder()->getMock();
/** @var ResourceInterface $resource */
$resource = new $this->resourceClass($repository, self::getValidator());
$resource->getDtoClass();
}
public function testThatGetDtoClassReturnsExpectedWithDto(): void
{
/** @var PHPUnit_Framework_MockObject_MockObject|UserRepository $repository */
$repository = $this->getRepositoryMockBuilder()->getMock();
/** @var ResourceInterface $resource */
$resource = new $this->resourceClass($repository, self::getValidator());
$resource->setDtoClass('foobar');
static::assertSame('foobar', $resource->getDtoClass());
}
public function testThatGetEntityNameCallsExpectedRepositoryMethod(): void
{
/** @var PHPUnit_Framework_MockObject_MockObject|UserRepository $repository */
$repository = $this->getRepositoryMockBuilder()->getMock();
$repository
->expects(static::once())
->method('getEntityName');
/** @var ResourceInterface $resource */
$resource = new $this->resourceClass($repository, self::getValidator());
$resource->getEntityName();
}
public function testThatGetReferenceCallsExpectedRepositoryMethod(): void
{
/** @var PHPUnit_Framework_MockObject_MockObject|UserRepository $repository */
$repository = $this->getRepositoryMockBuilder()->getMock();
$repository
->expects(static::once())
->method('getReference');
/** @var ResourceInterface $resource */
$resource = new $this->resourceClass($repository, self::getValidator());
$resource->getReference('some id');
}
public function testThatGetAssociationsCallsExpectedRepositoryMethod(): void
{
/** @var PHPUnit_Framework_MockObject_MockObject|UserRepository $repository */
$repository = $this->getRepositoryMockBuilder()->getMock();
$repository
->expects(static::once())
->method('getAssociations');
/** @var ResourceInterface $resource */
$resource = new $this->resourceClass($repository, self::getValidator());
$resource->getAssociations();
}
/**
* @dataProvider dataProviderTestThatFindCallsExpectedRepositoryMethodWithCorrectParameters
*
* @param array $expectedArguments
* @param array $arguments
*/
public function testThatFindCallsExpectedRepositoryMethodWithCorrectParameters(
array $expectedArguments,
array $arguments
): void
{
/** @var PHPUnit_Framework_MockObject_MockObject|UserRepository $repository */
$repository = $this->getRepositoryMockBuilder()->getMock();
$repository
->expects(static::once())
->method('findByAdvanced')
->with(...$expectedArguments)
;
/** @var ResourceInterface $resource */
$resource = new $this->resourceClass($repository, self::getValidator());
$resource->find(...$arguments);
}
/**
* @return array
*/
public function dataProviderTestThatFindCallsExpectedRepositoryMethodWithCorrectParameters(): array
{
return [
[
[[], [], 0, 0, []],
[null, null, null, null, null],
],
[
[['foo'], [], 0, 0, []],
[['foo'], null, null, null, null],
],
[
[['foo'], ['foo'], 0, 0, []],
[['foo'], ['foo'], null, null, null],
],
[
[['foo'], ['foo'], 1, 0, []],
[['foo'], ['foo'], 1, null, null],
],
[
[['foo'], ['foo'], 1, 2, []],
[['foo'], ['foo'], 1, 2, null],
],
[
[['foo'], ['foo'], 1, 2, ['foo']],
[['foo'], ['foo'], 1, 2, ['foo']],
],
];
}
/**
* @return PHPUnit_Framework_MockObject_MockBuilder
*/
private function getRepositoryMockBuilder(): PHPUnit_Framework_MockObject_MockBuilder
{
return $this
->getMockBuilder(UserRepository::class)
->setConstructorArgs([self::getEntityManager(), new ClassMetadata($this->entityClass)]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment