1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padam87\AttributeBundle\Tests\EventListener; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Logging\DebugStack; |
6
|
|
|
use \Mockery as m; |
7
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
8
|
|
|
use Doctrine\ORM\EntityManager; |
9
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
10
|
|
|
use Padam87\AttributeBundle\Entity\Definition; |
11
|
|
|
use Padam87\AttributeBundle\Entity\Schema; |
12
|
|
|
use Padam87\AttributeBundle\Tests\Model\Subscriber; |
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
14
|
|
|
|
15
|
|
|
class AttributeCreatorListenerTest extends WebTestCase |
16
|
|
|
{ |
17
|
|
|
protected function setUp() |
18
|
|
|
{ |
19
|
|
|
static::bootKernel(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function tearDown() |
23
|
|
|
{ |
24
|
|
|
m::close(); |
25
|
|
|
|
26
|
|
|
parent::tearDown(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @test |
31
|
|
|
* @group functional |
32
|
|
|
*/ |
33
|
|
|
public function entityShouldHaveAttributes() |
34
|
|
|
{ |
35
|
|
|
$container = static::$kernel->getContainer(); |
36
|
|
|
/** @var ManagerRegistry $doctrine */ |
37
|
|
|
$doctrine = $container->get('doctrine'); |
38
|
|
|
/** @var EntityManager $em */ |
39
|
|
|
$em = $doctrine->getManager(); |
40
|
|
|
$metadata = $em->getMetadataFactory()->getAllMetadata(); |
41
|
|
|
|
42
|
|
|
$schemaTool = new SchemaTool($em); |
43
|
|
|
$schemaTool->dropSchema($metadata); |
44
|
|
|
$schemaTool->createSchema($metadata); |
45
|
|
|
|
46
|
|
|
$schema = new Schema(); |
47
|
|
|
$schema->setClassName('Padam87\AttributeBundle\Tests\Model\Subscriber'); |
48
|
|
|
|
49
|
|
|
/** @var DebugStack $profiler */ |
50
|
|
|
$profiler = $container->get('doctrine.dbal.logger.profiling.default'); |
51
|
|
|
|
52
|
|
|
$subscriber = new Subscriber(); |
53
|
|
|
|
54
|
|
|
// FIRST PASS |
55
|
|
View Code Duplication |
for ($i = 0; $i < 5; $i++) { |
|
|
|
|
56
|
|
|
$definition = new Definition(); |
57
|
|
|
$definition->setName($i); |
58
|
|
|
$definition->setType('text'); |
59
|
|
|
|
60
|
|
|
$schema->addDefinition($definition); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$em->persist($schema); |
64
|
|
|
$em->flush($schema); |
65
|
|
|
$em->refresh($schema); |
66
|
|
|
|
67
|
|
|
$this->assertCount(5, $schema->getDefinitions()); |
68
|
|
|
|
69
|
|
|
$em->persist($subscriber); |
70
|
|
|
$em->flush($subscriber); |
71
|
|
|
|
72
|
|
|
$profiler->queries = []; // reset the profiler to show oly relevant queries |
73
|
|
|
|
74
|
|
|
$em->refresh($subscriber); |
75
|
|
|
|
76
|
|
|
$this->assertCount(5, $subscriber->getAttributes()); |
77
|
|
|
$this->assertCount(16, $profiler->queries); |
78
|
|
|
|
79
|
|
|
// SECOND PASS |
80
|
|
View Code Duplication |
for ($i = 5; $i < 10; $i++) { |
|
|
|
|
81
|
|
|
$definition = new Definition(); |
82
|
|
|
$definition->setName($i); |
83
|
|
|
$definition->setType('text'); |
84
|
|
|
|
85
|
|
|
$schema->addDefinition($definition); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$em->persist($schema); |
89
|
|
|
$em->flush($schema); |
90
|
|
|
$em->refresh($schema); |
91
|
|
|
|
92
|
|
|
$this->assertCount(10, $schema->getDefinitions()); |
93
|
|
|
|
94
|
|
|
$subscriber->setName('test'); |
95
|
|
|
$em->flush($subscriber); |
96
|
|
|
|
97
|
|
|
$profiler->queries = []; // reset the profiler to show oly relevant queries |
98
|
|
|
|
99
|
|
|
$em->refresh($subscriber); |
100
|
|
|
|
101
|
|
|
$this->assertCount(10, $subscriber->getAttributes()); |
102
|
|
|
$this->assertCount(16, $profiler->queries); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.