DoctrineEntityManagerTest   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 256
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 12
Bugs 0 Features 0
Metric Value
wmc 18
c 12
b 0
f 0
lcom 1
cbo 3
dl 0
loc 256
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A entityManager() 0 4 1
A getException() 0 5 1
A getNoEntity() 0 10 1
A getSuccess() 0 16 1
A getCollectionException() 0 4 1
A getCollectionNoEntities() 0 5 1
B getCollectionSuccess() 0 22 4
B getCollectionFilters() 0 27 1
A getCollectionOrder() 0 23 3
A create() 0 9 1
B update() 0 42 1
A remove() 0 19 1
A createDetachedEntity() 0 11 1
1
<?php
2
3
namespace Managlea\Tests\EntityManager;
4
5
6
use Managlea\Component\EntityManager\DoctrineEntityManager;
7
use Managlea\Component\EntityManagerInterface;
8
use Managlea\Tests\BaseTestCase;
9
10
/**
11
 * Class DoctrineEntityManagerTest
12
 * @package Managlea\Tests\EntityManager
13
 */
14
class DoctrineEntityManagerTest extends BaseTestCase
15
{
16
    /**
17
     * @test
18
     */
19
    public function entityManager()
20
    {
21
        $this->assertEquals(true, $this->entityManager instanceof EntityManagerInterface);
22
    }
23
24
    /**
25
     * @test
26
     * @expectedException \Exception
27
     */
28
    public function getException()
29
    {
30
        $product = $this->createProduct();
31
        $this->entityManager->get('foo', $product->getId());
32
    }
33
34
    /**
35
     * @test
36
     */
37
    public function getNoEntity()
38
    {
39
        $entity = $this->entityManager->get(self::SCHEMA_PRODUCT, -1);
40
        $this->assertEquals(false, $entity);
41
42
        $product = $this->createProduct();
43
        $criteria = array('dateOfBirth' => '1970-01-02');
44
        $entity = $this->entityManager->get(self::SCHEMA_PRODUCT, $product->getId(), $criteria);
45
        $this->assertEquals(false, $entity);
46
    }
47
48
    /**
49
     * @test
50
     */
51
    public function getSuccess()
52
    {
53
        $product = $this->createProduct();
54
        $entity = $this->entityManager->get(self::SCHEMA_PRODUCT, $product->getId());
55
        $this->assertEquals($product->getName(), $entity->getName());
56
57
        $product = $this->createProduct();
58
        $criteria = array();
59
        $entity = $this->entityManager->get(self::SCHEMA_PRODUCT, $product->getId(), $criteria);
60
        $this->assertEquals($product->getName(), $entity->getName());
61
62
        $product = $this->createProduct();
63
        $criteria = array('dateOfBirth' => '1970-01-01');
64
        $entity = $this->entityManager->get(self::SCHEMA_PRODUCT, $product->getId(), $criteria);
65
        $this->assertEquals($product->getName(), $entity->getName());
66
    }
67
68
    /**
69
     * @test
70
     * @expectedException \Exception
71
     */
72
    public function getCollectionException()
73
    {
74
        $this->entityManager->getCollection('foo');
75
    }
76
77
    /**
78
     * @test
79
     */
80
    public function getCollectionNoEntities()
81
    {
82
        $collection = $this->entityManager->getCollection(self::SCHEMA_PRODUCT);
83
        $this->assertEquals(0, count($collection));
84
    }
85
86
    /**
87
     * @test
88
     *
89
     * Will generate new objects, save them into DB and later
90
     * try to retrieve them by using offset and limit
91
     */
92
    public function getCollectionSuccess()
93
    {
94
        $products = $this->createProductsCollection();
95
        $noOfProducts = count($products);
96
97
        $limit = rand(4, 8);
98
        $iterations = ceil($noOfProducts / $limit);
99
100
        for ($i = 1; $i <= $iterations; $i++) {
101
            $offset = ($i > 1) ? ($i - 1) * $limit : 0;
102
            $collection = $this->entityManager->getCollection(self::SCHEMA_PRODUCT, array(), $limit,
103
                $offset);
104
105
            $first = current($collection);
106
            $firstPos = $offset;
107
            $this->assertEquals($first->getName(), $products[$firstPos]->getName());
108
109
            $last = end($collection);
110
            $lastPos = (count($collection) < $limit) ? $offset + count($collection) - 1 : ($offset + $limit - 1);
111
            $this->assertEquals($last->getName(), $products[$lastPos]->getName());
112
        }
113
    }
114
115
    /**
116
     * @test
117
     */
118
    public function getCollectionFilters()
119
    {
120
        $filters = array(
121
            'name' => 'random'
122
        );
123
        $collection = $this->entityManager->getCollection(self::SCHEMA_PRODUCT, $filters, 1, 0);
124
        $this->assertEquals(0, count($collection));
125
126
        $products = $this->createProductsCollection();
127
        $noOfProducts = count($products);
128
129
        // Get random offset
130
        $productOffset = rand(0, $noOfProducts - 1);
131
        $product = $products[$productOffset];
132
133
        // Set filter based on random object
134
        $filters = array(
135
            'name' => $product->getName()
136
        );
137
138
        $collection = $this->entityManager->getCollection(self::SCHEMA_PRODUCT, $filters, 10, 0);
139
        $this->assertEquals(1, count($collection));
140
141
        $result = current($collection);
142
        $this->assertEquals($result->getName(), $product->getName());
143
144
    }
145
146
    /**
147
     * @test
148
     */
149
    public function getCollectionOrder()
150
    {
151
        $orderTypes = array('ASC' => 'sort', 'DESC' => 'rsort');
152
        foreach ($orderTypes as $orderType => $sortMethod) {
153
            $products = $this->createProductsCollection();
154
            $ordered = array();
155
156
            foreach ($products as $product) {
157
                $ordered[] = $product->getName();
158
            }
159
160
            $sortMethod($ordered);
161
            $order = array(
162
                'name' => $orderType
163
            );
164
165
            $collection = $this->entityManager->getCollection(self::SCHEMA_PRODUCT, array(), 1, 0,
166
                $order);
167
            $result = current($collection);
168
169
            $this->assertEquals($result->getName(), $ordered[0]);
170
        }
171
    }
172
173
    /**
174
     * @test
175
     */
176
    public function create()
177
    {
178
        $data = array('name' => uniqid());
179
        $entity = $this->entityManager->create(self::SCHEMA_PRODUCT, $data);
180
        $this->assertEquals($entity->getName(), $data['name']);
181
182
        $product = $this->entityManager->get(self::SCHEMA_PRODUCT, $entity->getId());
183
        $this->assertEquals($product->getName(), $entity->getName());
184
    }
185
186
    /**
187
     * @test
188
     */
189
    public function update()
190
    {
191
        /**
192
         * Update entity which does not exist
193
         */
194
        $updateResult = $this->entityManager->update(self::SCHEMA_PRODUCT, 1, array());
195
        $this->assertFalse($updateResult);
196
197
        /**
198
         * Create entity
199
         */
200
        $product = $this->createProduct();
201
202
        /**
203
         * Search entity by name
204
         */
205
        $filters = array(
206
            'name' => $product->getName()
207
        );
208
        $collection = $this->entityManager->getCollection(self::SCHEMA_PRODUCT, $filters);
209
        $this->assertTrue(count($collection) == 1);
210
211
        /**
212
         * Update the name
213
         */
214
        $newName = uniqid();
215
        $entity = $this->entityManager->update(self::SCHEMA_PRODUCT, $product->getId(),
216
            array('name' => $newName));
217
        $this->assertEquals($newName, $entity->getName());
218
219
        /**
220
         * Search for original name
221
         */
222
        $collection = $this->entityManager->getCollection(self::SCHEMA_PRODUCT, $filters);
223
        $this->assertTrue(count($collection) == 0);
224
225
        /**
226
         * Search for id and check for updated name
227
         */
228
        $entity = $this->entityManager->get(self::SCHEMA_PRODUCT, $product->getId());
229
        $this->assertEquals($entity->getName(), $newName);
230
    }
231
232
    /**
233
     * @test
234
     */
235
    public function remove()
236
    {
237
        $removeResult = $this->entityManager->delete(self::SCHEMA_PRODUCT, rand(1, 9999));
238
        $this->assertFalse($removeResult);
239
240
        $data = array('name' => uniqid());
241
        $newEntity = $this->entityManager->create(self::SCHEMA_PRODUCT, $data);
242
243
        $entityId = $newEntity->getId();
244
245
        $entity = $this->entityManager->get(self::SCHEMA_PRODUCT, $entityId);
246
        $this->assertEquals($data['name'], $entity->getName());
247
248
        $removeResult = $this->entityManager->delete(self::SCHEMA_PRODUCT, $entityId);
249
        $this->assertTrue($removeResult);
250
251
        $entity = $this->entityManager->get(self::SCHEMA_PRODUCT, $entityId);
252
        $this->assertFalse($entity);
253
    }
254
255
    /**
256
     * @test
257
     */
258
    public function createDetachedEntity()
259
    {
260
        $data = array(
261
            'namE' => 'foo',
262
            'dAte_of_bIrth' => "bar"
263
        );
264
265
        $entity = DoctrineEntityManager::createDetachedEntity(self::SCHEMA_PRODUCT, $data);
266
        $this->assertEquals($data['namE'], $entity->getName());
267
        $this->assertEquals($data['dAte_of_bIrth'], $entity->getDateOfBirth());
268
    }
269
}
270