2019-02-18 00:00:03 +01:00
|
|
|
<?php
|
|
|
|
|
2019-05-30 16:52:02 +02:00
|
|
|
namespace tests\Unit\Domain\Request\Entity;
|
2019-02-18 00:00:03 +01:00
|
|
|
|
2020-04-02 21:13:35 +02:00
|
|
|
use Infinito\DBAL\Types\Meta\Right\LayerType;
|
2019-05-30 16:30:03 +02:00
|
|
|
use Infinito\Domain\Repository\LayerRepositoryFactoryServiceInterface;
|
2020-04-02 21:13:35 +02:00
|
|
|
use Infinito\Domain\Request\Entity\LazyRequestedEntity;
|
|
|
|
use Infinito\Domain\Request\Entity\RequestedEntityInterface;
|
|
|
|
use Infinito\Domain\Request\Right\RequestedRightInterface;
|
2019-02-18 00:00:03 +01:00
|
|
|
use Infinito\Entity\Source\PureSource;
|
2020-04-02 21:13:35 +02:00
|
|
|
use Infinito\Repository\RepositoryInterface;
|
2019-02-18 00:00:03 +01:00
|
|
|
use Infinito\Repository\Source\SourceRepositoryInterface;
|
2020-04-02 21:13:35 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-02-18 00:00:03 +01:00
|
|
|
|
|
|
|
class LazyRequestedEntityTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var RequestedEntityInterface
|
|
|
|
*/
|
|
|
|
private $lazyRequestedEntity;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var LayerRepositoryFactoryServiceInterface
|
|
|
|
*/
|
|
|
|
private $layerRepositoryFactoryService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SourceRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @see \PHPUnit\Framework\TestCase::setUp()
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->repository = $this->createMock(SourceRepositoryInterface::class);
|
|
|
|
$this->layerRepositoryFactoryService = new class($this->repository) implements LayerRepositoryFactoryServiceInterface {
|
|
|
|
private $count = 0;
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
public function __construct($repository)
|
|
|
|
{
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRepository(string $layer): RepositoryInterface
|
|
|
|
{
|
|
|
|
if ($this->count > 0) {
|
|
|
|
//Just for information and testing thrown
|
|
|
|
throw new \Exception('The function '.__FUNCTION__.' was called multiple times!');
|
|
|
|
}
|
|
|
|
++$this->count;
|
|
|
|
|
|
|
|
return $this->repository;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$this->lazyRequestedEntity = new LazyRequestedEntity($this->layerRepositoryFactoryService);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSlug(): void
|
|
|
|
{
|
|
|
|
$slug = 'hello';
|
|
|
|
$requestedSource = new PureSource();
|
|
|
|
$requestedSource->setSlug($slug);
|
|
|
|
$requestedSource->setId('123');
|
|
|
|
$this->repository->method('findOneBySlug')->willReturn($requestedSource);
|
|
|
|
$requestedRight = $this->createMock(RequestedRightInterface::class);
|
|
|
|
$requestedRight->method('getLayer')->willReturn(LayerType::SOURCE);
|
|
|
|
$this->assertNull($this->lazyRequestedEntity->setRequestedRight($requestedRight));
|
|
|
|
$this->assertNull($this->lazyRequestedEntity->setSlug($slug));
|
|
|
|
//Call 1
|
|
|
|
$this->assertEquals($requestedSource, $this->lazyRequestedEntity->getEntity());
|
|
|
|
//Call 2
|
|
|
|
$this->assertEquals($requestedSource, $this->lazyRequestedEntity->getEntity());
|
|
|
|
//Call with other slug
|
|
|
|
$this->assertNull($this->lazyRequestedEntity->setSlug('abcde'));
|
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->lazyRequestedEntity->getEntity();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testId(): void
|
|
|
|
{
|
|
|
|
$id = 12345;
|
|
|
|
$requestedSource = new PureSource();
|
|
|
|
$requestedSource->setId($id);
|
|
|
|
$this->repository->method('find')->willReturn($requestedSource);
|
|
|
|
$requestedRight = $this->createMock(RequestedRightInterface::class);
|
|
|
|
$requestedRight->method('getLayer')->willReturn(LayerType::SOURCE);
|
|
|
|
$this->assertNull($this->lazyRequestedEntity->setRequestedRight($requestedRight));
|
|
|
|
$this->assertNull($this->lazyRequestedEntity->setId($id));
|
|
|
|
//Call 1
|
|
|
|
$this->assertEquals($requestedSource, $this->lazyRequestedEntity->getEntity());
|
|
|
|
//Call 2
|
|
|
|
$this->assertEquals($requestedSource, $this->lazyRequestedEntity->getEntity());
|
|
|
|
//Call with other slug
|
|
|
|
$this->assertNull($this->lazyRequestedEntity->setId(65432));
|
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->lazyRequestedEntity->getEntity();
|
|
|
|
}
|
|
|
|
}
|