|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Khepin\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use \Mockery as m; |
|
6
|
|
|
use Khepin\YamlFixturesBundle\Loader\YamlLoader; |
|
7
|
|
|
use Khepin\Utils\BaseTestCaseOrm; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @group orm |
|
11
|
|
|
*/ |
|
12
|
|
|
class YamlFixtureTest extends BaseTestCaseOrm |
|
13
|
|
|
{ |
|
14
|
|
|
protected $kernel = null; |
|
15
|
|
|
|
|
16
|
|
|
public function setUp() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->getDoctrine(); |
|
19
|
|
|
$service = m::mock()->shouldReceive('lowerCaseName')->withAnyArgs()->andReturnUsing( |
|
|
|
|
|
|
20
|
|
|
function ($car) { |
|
21
|
|
|
$car->setName(strtolower($car->getName())); |
|
22
|
|
|
} |
|
23
|
|
|
)->mock(); |
|
24
|
|
|
$container = m::mock('Container') |
|
|
|
|
|
|
25
|
|
|
->shouldReceive('get')->with('my_service')->andReturn($service) |
|
26
|
|
|
->shouldReceive('get')->withAnyArgs()->andReturn($this->doctrine) |
|
27
|
|
|
->mock(); |
|
28
|
|
|
$this->kernel = m::mock( |
|
29
|
|
|
'\Symfony\Component\HttpKernel\KernelInterface', |
|
30
|
|
|
array( |
|
31
|
|
|
'locateResource' => __DIR__ . '/simple_loading/', |
|
32
|
|
|
'getContainer' => $container |
|
33
|
|
|
) |
|
34
|
|
|
); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testSimpleLoading() |
|
38
|
|
|
{ |
|
39
|
|
|
$loader = new YamlLoader($this->kernel, array('SomeBundle'), 'DataFixtures'); |
|
40
|
|
|
$loader->loadFixtures(); |
|
41
|
|
|
|
|
42
|
|
|
$em = $this->doctrine->getEntityManager(); |
|
|
|
|
|
|
43
|
|
|
$cars = $em->getRepository('Khepin\Fixture\Entity\Car')->findAll(); |
|
44
|
|
|
$this->assertEquals(2, count($cars)); |
|
45
|
|
|
$car = $cars[0]; |
|
46
|
|
|
$this->assertEquals('Mercedes', $car->getName()); |
|
47
|
|
|
$date = new \DateTime('2012-01-01'); |
|
48
|
|
|
$this->assertEquals($date, $car->getDatePurchased()); |
|
49
|
|
|
$this->assertEquals(get_class($date), get_class($car->getDatePurchased())); |
|
50
|
|
|
|
|
51
|
|
|
$car = $cars[1]; |
|
52
|
|
|
$this->assertEquals('BMW', $car->getName()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
View Code Duplication |
public function testLoadSingleFile() |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$loader = new YamlLoader($this->kernel, array('SomeBundle/cars'), 'DataFixtures'); |
|
58
|
|
|
$loader->loadFixtures(); |
|
59
|
|
|
|
|
60
|
|
|
$em = $this->doctrine->getManager(); |
|
|
|
|
|
|
61
|
|
|
$cars = $em->getRepository('Khepin\Fixture\Entity\Car')->findAll(); |
|
62
|
|
|
$this->assertEquals(2, count($cars)); |
|
63
|
|
|
$drivers = $em->getRepository('Khepin\Fixture\Entity\Driver')->findAll(); |
|
64
|
|
|
$this->assertEquals(0, count($drivers)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
View Code Duplication |
public function testSingleFileLoadedOnlyOnce() |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
$loader = new YamlLoader($this->kernel, array('SomeBundle/cars', 'SomeBundle'), 'DataFixtures'); |
|
70
|
|
|
$loader->loadFixtures(); |
|
71
|
|
|
|
|
72
|
|
|
$em = $this->doctrine->getManager(); |
|
|
|
|
|
|
73
|
|
|
$cars = $em->getRepository('Khepin\Fixture\Entity\Car')->findAll(); |
|
74
|
|
|
$this->assertEquals(2, count($cars)); |
|
75
|
|
|
$drivers = $em->getRepository('Khepin\Fixture\Entity\Driver')->findAll(); |
|
76
|
|
|
$this->assertEquals(0, count($drivers)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
View Code Duplication |
public function testContext() |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
$loader = new YamlLoader($this->kernel, array('SomeBundle'), 'DataFixtures'); |
|
82
|
|
|
$loader->loadFixtures('french_cars'); |
|
83
|
|
|
|
|
84
|
|
|
$repo = $this->doctrine->getEntityManager()->getRepository('Khepin\Fixture\Entity\Car'); |
|
|
|
|
|
|
85
|
|
|
$cars = $repo->findAll(); |
|
86
|
|
|
$this->assertEquals(4, count($cars)); |
|
87
|
|
|
|
|
88
|
|
|
$car = $repo->findOneBy(array('name' => 'Peugeot')); |
|
89
|
|
|
$this->assertEquals('Peugeot', $car->getName()); |
|
90
|
|
|
|
|
91
|
|
|
$car = $repo->findOneBy(array('name' => 'BMW')); |
|
92
|
|
|
$this->assertEquals('BMW', $car->getName()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
public function testWithAssociation() |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
$loader = new YamlLoader($this->kernel, array('SomeBundle'), 'DataFixtures'); |
|
98
|
|
|
$loader->loadFixtures('with_drivers'); |
|
99
|
|
|
|
|
100
|
|
|
$repo = $this->doctrine->getEntityManager()->getRepository('Khepin\Fixture\Entity\Driver'); |
|
|
|
|
|
|
101
|
|
|
$driver = $repo->findOneBy(array('name' => 'Mom')); |
|
102
|
|
|
$this->assertEquals($driver->getCar()->getName(), 'Mercedes'); |
|
103
|
|
|
$driver = $repo->findOneBy(array('name' => 'Dad')); |
|
104
|
|
|
$this->assertEquals($driver->getCar()->getName(), 'BMW'); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertEquals(get_class($driver->getCar()), 'Khepin\Fixture\Entity\Car'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
View Code Duplication |
public function testPurge() |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
$loader = new YamlLoader($this->kernel, array('SomeBundle'), 'DataFixtures'); |
|
112
|
|
|
$loader->loadFixtures('with_drivers'); |
|
113
|
|
|
$loader->purgeDatabase('orm'); |
|
114
|
|
|
|
|
115
|
|
|
$cars = $this->doctrine->getEntityManager()->getRepository('Khepin\Fixture\Entity\Car')->findAll(); |
|
|
|
|
|
|
116
|
|
|
$this->assertEmpty($cars); |
|
117
|
|
|
$drivers = $this->doctrine->getEntityManager()->getRepository('Khepin\Fixture\Entity\Driver')->findAll(); |
|
|
|
|
|
|
118
|
|
|
$this->assertEmpty($drivers); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function testPurgeWithTruncate() |
|
122
|
|
|
{ |
|
123
|
|
|
$loader = new YamlLoader($this->kernel, array('SomeBundle'), 'DataFixtures'); |
|
124
|
|
|
$loader->loadFixtures('with_drivers'); |
|
125
|
|
|
|
|
126
|
|
|
$cars = $this->doctrine->getEntityManager()->getRepository('Khepin\Fixture\Entity\Car')->findAll(); |
|
|
|
|
|
|
127
|
|
|
$firstCar = $cars[0]; |
|
128
|
|
|
$firstCarIdBefore = $firstCar->getId(); |
|
129
|
|
|
|
|
130
|
|
|
$loader->purgeDatabase('orm', null, true); |
|
131
|
|
|
|
|
132
|
|
|
$cars = $this->doctrine->getEntityManager()->getRepository('Khepin\Fixture\Entity\Car')->findAll(); |
|
|
|
|
|
|
133
|
|
|
$this->assertEmpty($cars); |
|
134
|
|
|
|
|
135
|
|
|
$loader->loadFixtures('with_drivers'); |
|
136
|
|
|
|
|
137
|
|
|
$cars = $this->doctrine->getEntityManager()->getRepository('Khepin\Fixture\Entity\Car')->findAll(); |
|
|
|
|
|
|
138
|
|
|
$firstCar = $cars[0]; |
|
139
|
|
|
$firstCarIdAfter = $firstCar->getId(); |
|
140
|
|
|
|
|
141
|
|
|
$this->assertEquals($firstCarIdBefore, $firstCarIdAfter); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function testNullValuesInAssociations() |
|
145
|
|
|
{ |
|
146
|
|
|
$loader = new YamlLoader($this->kernel, array('SomeBundle'), 'DataFixtures'); |
|
147
|
|
|
$loader->loadFixtures('with_drivers'); |
|
148
|
|
|
|
|
149
|
|
|
$repo = $this->doctrine->getEntityManager()->getRepository('Khepin\Fixture\Entity\Driver'); |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
$driver = $repo->findOneBy(array('name' => 'Mom')); |
|
152
|
|
|
$this->assertEquals(get_class($driver->getCar()), 'Khepin\Fixture\Entity\Car'); |
|
153
|
|
|
$this->assertEquals($driver->getCar()->getName(), 'Mercedes'); |
|
154
|
|
|
$this->assertEquals($driver->getSecondCar(), null); |
|
155
|
|
|
|
|
156
|
|
|
$driver = $repo->findOneBy(array('name' => 'Son')); |
|
157
|
|
|
$this->assertEquals(get_class($driver->getCar()), 'Khepin\Fixture\Entity\Car'); |
|
158
|
|
|
$this->assertEquals($driver->getCar()->getName(), 'BMW'); |
|
159
|
|
|
$this->assertEquals($driver->getSecondCar(), null); |
|
160
|
|
|
|
|
161
|
|
|
$driver = $repo->findOneBy(array('name' => 'Dad')); |
|
162
|
|
|
$this->assertEquals(get_class($driver->getCar()), 'Khepin\Fixture\Entity\Car'); |
|
163
|
|
|
$this->assertEquals($driver->getCar()->getName(), 'BMW'); |
|
164
|
|
|
$this->assertEquals(get_class($driver->getSecondCar()), 'Khepin\Fixture\Entity\Car'); |
|
165
|
|
|
$this->assertEquals($driver->getSecondCar()->getName(), 'Mercedes'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
View Code Duplication |
public function testServiceCalls() |
|
|
|
|
|
|
169
|
|
|
{ |
|
170
|
|
|
$loader = new YamlLoader($this->kernel, array('SomeBundle'), 'DataFixtures'); |
|
171
|
|
|
$loader->loadFixtures('service'); |
|
172
|
|
|
|
|
173
|
|
|
$repo = $this->doctrine->getManager()->getRepository('Khepin\Fixture\Entity\Car'); |
|
|
|
|
|
|
174
|
|
|
$car = $repo->findOneByName('toyota'); |
|
175
|
|
|
$this->assertTrue('toyota' === $car->getName()); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function testArrayAssociation() |
|
179
|
|
|
{ |
|
180
|
|
|
$loader = new YamlLoader($this->kernel, array('SomeBundle'), 'DataFixtures'); |
|
181
|
|
|
$loader->loadFixtures(); |
|
182
|
|
|
|
|
183
|
|
|
$em = $this->doctrine->getEntityManager(); |
|
|
|
|
|
|
184
|
|
|
$owner = $em->getRepository('Khepin\Fixture\Entity\Owner')->findOneById(1); |
|
185
|
|
|
$this->assertEquals(2, count($owner->getOwnedCars())); |
|
186
|
|
|
|
|
187
|
|
|
$car1 = $owner->getOwnedCars()->get(0); |
|
188
|
|
|
$this->assertEquals(get_class($car1), 'Khepin\Fixture\Entity\Car'); |
|
189
|
|
|
$this->assertEquals('Mercedes', $car1->getName()); |
|
190
|
|
|
|
|
191
|
|
|
$car2 = $owner->getOwnedCars()->get(1); |
|
192
|
|
|
$this->assertEquals(get_class($car2), 'Khepin\Fixture\Entity\Car'); |
|
193
|
|
|
$this->assertEquals('BMW', $car2->getName()); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
View Code Duplication |
public function testConstructor() |
|
|
|
|
|
|
197
|
|
|
{ |
|
198
|
|
|
$loader = new YamlLoader($this->kernel, array('SomeBundle'), 'DataFixtures'); |
|
199
|
|
|
$loader->loadFixtures('construct'); |
|
200
|
|
|
|
|
201
|
|
|
$repo = $this->doctrine->getManager()->getRepository('Khepin\Fixture\Entity\Car'); |
|
|
|
|
|
|
202
|
|
|
$car = $repo->findOneByName('Ford'); |
|
203
|
|
|
$this->assertEquals('Ford', $car->getName()); |
|
204
|
|
|
$this->assertInstanceOf('\DateTime', $car->getDatePurchased()); |
|
205
|
|
|
|
|
206
|
|
|
$repo = $this->doctrine->getManager()->getRepository('Khepin\Fixture\Entity\Owner'); |
|
|
|
|
|
|
207
|
|
|
$owner = $repo->findOneByName('Some Small Company'); |
|
208
|
|
|
$this->assertEquals(1, count($owner->getOwnedCars())); |
|
209
|
|
|
$this->assertEquals('Some Small Company', $owner->getName()); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.