1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Large\D\Entity\Savers; |
5
|
|
|
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractLargeTest; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractTest; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\GetGeneratedCodeContainerTrait; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\TestCodeGenerator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @large |
14
|
|
|
*/ |
15
|
|
|
class EntityUnitOfWorkHelperLargeTest extends AbstractLargeTest |
16
|
|
|
{ |
17
|
|
|
use GetGeneratedCodeContainerTrait; |
18
|
|
|
|
19
|
|
|
public const WORK_DIR = AbstractTest::VAR_PATH . '/' . self::TEST_TYPE_LARGE . '/EntityUnitOfWorkHelperLargeTest'; |
20
|
|
|
|
21
|
|
|
private const TEST_ENTITY_FQN = self::TEST_ENTITIES_ROOT_NAMESPACE . TestCodeGenerator::TEST_ENTITY_EMAIL; |
22
|
|
|
|
23
|
|
|
protected static $buildOnce = true; |
24
|
|
|
/** @var string */ |
25
|
|
|
private $entityFqn; |
26
|
|
|
/** @var NamespaceHelper */ |
27
|
|
|
private $namespaceHelper; |
28
|
|
|
|
29
|
|
|
public function setUp() |
30
|
|
|
{ |
31
|
|
|
parent::setUp(); |
32
|
|
|
if (false === self::$built) { |
33
|
|
|
$this->getTestCodeGenerator() |
34
|
|
|
->copyTo(self::WORK_DIR); |
35
|
|
|
self::$built = true; |
36
|
|
|
} |
37
|
|
|
$this->setupCopiedWorkDirAndCreateDatabase(); |
38
|
|
|
$this->entityFqn = $this->getCopiedFqn(self::TEST_ENTITY_FQN); |
39
|
|
|
$this->namespaceHelper = self::$containerStaticRef->get(NamespaceHelper::class); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @test |
44
|
|
|
*/ |
45
|
|
|
public function itCanReturnAnEntityFromTheUnitOfWork(): void |
46
|
|
|
{ |
47
|
|
|
$entity = $this->createEntity($this->entityFqn); |
48
|
|
|
$dto = $this->getEntityDtoFactory()->createDtoFromEntity($entity); |
49
|
|
|
$class = $this->getClass(); |
50
|
|
|
$this->getEntitySaver()->save($entity); |
51
|
|
|
$savedEntity = $this->getRepository()->get($entity->getId()); |
52
|
|
|
$class->addEntityRecord($savedEntity); |
53
|
|
|
$fetchedEntity = $class->getEntityFromUnitOfWorkUsingDto($dto); |
54
|
|
|
self::assertSame($savedEntity, $fetchedEntity); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function getClass() |
58
|
|
|
{ |
59
|
|
|
$class = $this->namespaceHelper->getEntityUnitOfWorkHelperFqnFromEntityFqn($this->entityFqn); |
60
|
|
|
|
61
|
|
|
return $this->getGeneratedClass($class); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function getRepository() |
65
|
|
|
{ |
66
|
|
|
$class = $this->namespaceHelper->getRepositoryqnFromEntityFqn($this->entityFqn); |
67
|
|
|
|
68
|
|
|
return $this->getGeneratedClass($class); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
*/ |
74
|
|
|
public function itIsAbleToLearnAboutAnEntity(): void |
75
|
|
|
{ |
76
|
|
|
$entity = $this->createEntity($this->entityFqn); |
77
|
|
|
$dto = $this->getEntityDtoFactory()->createDtoFromEntity($entity); |
78
|
|
|
$class = $this->getClass(); |
79
|
|
|
self::assertFalse($class->hasRecordOfDto($dto)); |
80
|
|
|
$this->getEntitySaver()->save($entity); |
81
|
|
|
$this->getRepository()->get($entity->getId()); |
82
|
|
|
self::assertTrue($class->hasRecordOfDto($dto)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @test |
87
|
|
|
*/ |
88
|
|
|
public function itWillReturnFalseIfItDoesNotKnowAboutAnEntity(): void |
89
|
|
|
{ |
90
|
|
|
$dto = $this->getEntityDtoFactory()->createEmptyDtoFromEntityFqn($this->entityFqn); |
91
|
|
|
$class = $this->getClass(); |
92
|
|
|
self::assertFalse($class->hasRecordOfDto($dto)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @test |
97
|
|
|
*/ |
98
|
|
|
public function itWillThrowAnExceptionIfItTriedToFetchAnEntityItDoesNotKnowAbout(): void |
99
|
|
|
{ |
100
|
|
|
$dto = $this->getEntityDtoFactory()->createEmptyDtoFromEntityFqn($this->entityFqn); |
101
|
|
|
$class = $this->getClass(); |
102
|
|
|
$this->expectException(\RuntimeException::class); |
103
|
|
|
$class->getEntityFromUnitOfWorkUsingDto($dto); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|