1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ddeboer\DataImport\Tests\Writer; |
4
|
|
|
|
5
|
|
|
use Ddeboer\DataImport\Writer\DoctrineWriter; |
6
|
|
|
use Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity; |
7
|
|
|
|
8
|
|
|
class DoctrineWriterTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
public function testWriteItem() |
11
|
|
|
{ |
12
|
|
|
$em = $this->getEntityManager(); |
13
|
|
|
|
14
|
|
|
$em->expects($this->once()) |
15
|
|
|
->method('persist'); |
16
|
|
|
|
17
|
|
|
$writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity'); |
18
|
|
|
|
19
|
|
|
$association = new TestEntity(); |
20
|
|
|
$item = array( |
21
|
|
|
'firstProperty' => 'some value', |
22
|
|
|
'secondProperty' => 'some other value', |
23
|
|
|
'firstAssociation'=> $association |
24
|
|
|
); |
25
|
|
|
$writer->writeItem($item); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function getEntityManager() |
29
|
|
|
{ |
30
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
31
|
|
|
->setMethods(array('getRepository', 'getClassMetadata', 'persist', 'flush', 'clear', 'getConnection', 'getReference')) |
32
|
|
|
->disableOriginalConstructor() |
33
|
|
|
->getMock(); |
34
|
|
|
|
35
|
|
|
$repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository') |
36
|
|
|
->disableOriginalConstructor() |
37
|
|
|
->getMock(); |
38
|
|
|
|
39
|
|
|
$metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata') |
40
|
|
|
->setMethods(array('getName', 'getFieldNames', 'getAssociationNames', 'setFieldValue', 'getAssociationMappings')) |
41
|
|
|
->disableOriginalConstructor() |
42
|
|
|
->getMock(); |
43
|
|
|
|
44
|
|
|
$metadata->expects($this->any()) |
45
|
|
|
->method('getName') |
46
|
|
|
->will($this->returnValue('Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity')); |
47
|
|
|
|
48
|
|
|
$metadata->expects($this->any()) |
49
|
|
|
->method('getFieldNames') |
50
|
|
|
->will($this->returnValue(array('firstProperty', 'secondProperty'))); |
51
|
|
|
|
52
|
|
|
$metadata->expects($this->any()) |
53
|
|
|
->method('getAssociationNames') |
54
|
|
|
->will($this->returnValue(array('firstAssociation'))); |
55
|
|
|
|
56
|
|
|
$metadata->expects($this->any()) |
57
|
|
|
->method('getAssociationMappings') |
58
|
|
|
->will($this->returnValue(array(array('fieldName' => 'firstAssociation','targetEntity' => 'Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity')))); |
59
|
|
|
|
60
|
|
|
$configuration = $this->getMockBuilder('Doctrine\DBAL\Configuration') |
61
|
|
|
->setMethods(array('getConnection')) |
62
|
|
|
->disableOriginalConstructor() |
63
|
|
|
->getMock(); |
64
|
|
|
|
65
|
|
|
$connection = $this->getMockBuilder('Doctrine\DBAL\Connection') |
66
|
|
|
->setMethods(array('getConfiguration', 'getDatabasePlatform', 'getTruncateTableSQL', 'executeQuery')) |
67
|
|
|
->disableOriginalConstructor() |
68
|
|
|
->getMock(); |
69
|
|
|
|
70
|
|
|
$connection->expects($this->any()) |
71
|
|
|
->method('getConfiguration') |
72
|
|
|
->will($this->returnValue($configuration)); |
73
|
|
|
|
74
|
|
|
$connection->expects($this->any()) |
75
|
|
|
->method('getDatabasePlatform') |
76
|
|
|
->will($this->returnSelf()); |
77
|
|
|
|
78
|
|
|
$connection->expects($this->any()) |
79
|
|
|
->method('getTruncateTableSQL') |
80
|
|
|
->will($this->returnValue('TRUNCATE SQL')); |
81
|
|
|
|
82
|
|
|
$connection->expects($this->any()) |
83
|
|
|
->method('executeQuery') |
84
|
|
|
->with('TRUNCATE SQL'); |
85
|
|
|
|
86
|
|
|
$em->expects($this->once()) |
87
|
|
|
->method('getRepository') |
88
|
|
|
->will($this->returnValue($repo)); |
89
|
|
|
|
90
|
|
|
$em->expects($this->once()) |
91
|
|
|
->method('getClassMetadata') |
92
|
|
|
->will($this->returnValue($metadata)); |
93
|
|
|
|
94
|
|
|
$em->expects($this->any()) |
95
|
|
|
->method('getConnection') |
96
|
|
|
->will($this->returnValue($connection)); |
97
|
|
|
|
98
|
|
|
$self = $this; |
99
|
|
|
$em->expects($this->any()) |
100
|
|
|
->method('persist') |
101
|
|
|
->will($this->returnCallback(function ($argument) use ($self) { |
102
|
|
|
$self->assertNotNull($argument->getFirstAssociation()); |
103
|
|
|
return true; |
104
|
|
|
})); |
105
|
|
|
|
106
|
|
|
return $em; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testLoadAssociationWithoutObject() |
110
|
|
|
{ |
111
|
|
|
$em = $this->getEntityManager(); |
112
|
|
|
|
113
|
|
|
$em->expects($this->once()) |
114
|
|
|
->method('persist'); |
115
|
|
|
|
116
|
|
|
$em->expects($this->once()) |
117
|
|
|
->method('getReference'); |
118
|
|
|
|
119
|
|
|
$writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity'); |
120
|
|
|
|
121
|
|
|
$item = array( |
122
|
|
|
'firstProperty' => 'some value', |
123
|
|
|
'secondProperty' => 'some other value', |
124
|
|
|
'firstAssociation' => 'firstAssociationId' |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$writer->writeItem($item); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testLoadAssociationWithPresetObject() |
131
|
|
|
{ |
132
|
|
|
$em = $this->getEntityManager(); |
133
|
|
|
|
134
|
|
|
$em->expects($this->once()) |
135
|
|
|
->method('persist'); |
136
|
|
|
|
137
|
|
|
$em->expects($this->never()) |
138
|
|
|
->method('getReference'); |
139
|
|
|
|
140
|
|
|
$writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity'); |
141
|
|
|
|
142
|
|
|
$association = new TestEntity(); |
143
|
|
|
$item = array( |
144
|
|
|
'firstProperty' => 'some value', |
145
|
|
|
'secondProperty' => 'some other value', |
146
|
|
|
'firstAssociation' => $association, |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
$writer->writeItem($item); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Test to make sure that we are clearing the write entity |
154
|
|
|
*/ |
155
|
|
|
public function testFlushAndClear() |
156
|
|
|
{ |
157
|
|
|
$em = $this->getEntityManager(); |
158
|
|
|
|
159
|
|
|
$em->expects($this->once()) |
160
|
|
|
->method('clear') |
161
|
|
|
->with($this->equalTo('Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity')); |
162
|
|
|
|
163
|
|
|
$writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity'); |
164
|
|
|
$writer->finish(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|