1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\ORM\PersistentCollection; |
7
|
|
|
use Doctrine\Tests\Mocks\ConnectionMock; |
8
|
|
|
use Doctrine\Tests\Mocks\DriverMock; |
9
|
|
|
use Doctrine\Tests\Mocks\EntityManagerMock; |
10
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceCart; |
11
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceProduct; |
12
|
|
|
use Doctrine\Tests\OrmTestCase; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Tests the lazy-loading capabilities of the PersistentCollection and the initialization of collections. |
16
|
|
|
* @author Giorgio Sironi <[email protected]> |
17
|
|
|
* @author Austin Morris <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class PersistentCollectionTest extends OrmTestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var PersistentCollection |
23
|
|
|
*/ |
24
|
|
|
protected $collection; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Doctrine\ORM\EntityManagerInterface |
28
|
|
|
*/ |
29
|
|
|
private $_emMock; |
30
|
|
|
|
31
|
|
|
protected function setUp() |
32
|
|
|
{ |
33
|
|
|
parent::setUp(); |
34
|
|
|
|
35
|
|
|
$this->_emMock = EntityManagerMock::create(new ConnectionMock([], new DriverMock())); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set up the PersistentCollection used for collection initialization tests. |
40
|
|
|
*/ |
41
|
|
|
public function setUpPersistentCollection() |
42
|
|
|
{ |
43
|
|
|
$classMetaData = $this->_emMock->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCart'); |
44
|
|
|
$this->collection = new PersistentCollection($this->_emMock, $classMetaData, new ArrayCollection); |
45
|
|
|
$this->collection->setInitialized(false); |
46
|
|
|
$this->collection->setOwner(new ECommerceCart(), $classMetaData->getAssociationMapping('products')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testCanBePutInLazyLoadingMode() |
50
|
|
|
{ |
51
|
|
|
$class = $this->_emMock->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct'); |
52
|
|
|
$collection = new PersistentCollection($this->_emMock, $class, new ArrayCollection); |
53
|
|
|
$collection->setInitialized(false); |
54
|
|
|
$this->assertFalse($collection->isInitialized()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Test that PersistentCollection::current() initializes the collection. |
59
|
|
|
*/ |
60
|
|
|
public function testCurrentInitializesCollection() |
61
|
|
|
{ |
62
|
|
|
$this->setUpPersistentCollection(); |
63
|
|
|
$this->collection->current(); |
64
|
|
|
$this->assertTrue($this->collection->isInitialized()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Test that PersistentCollection::key() initializes the collection. |
69
|
|
|
*/ |
70
|
|
|
public function testKeyInitializesCollection() |
71
|
|
|
{ |
72
|
|
|
$this->setUpPersistentCollection(); |
73
|
|
|
$this->collection->key(); |
74
|
|
|
$this->assertTrue($this->collection->isInitialized()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Test that PersistentCollection::next() initializes the collection. |
79
|
|
|
*/ |
80
|
|
|
public function testNextInitializesCollection() |
81
|
|
|
{ |
82
|
|
|
$this->setUpPersistentCollection(); |
83
|
|
|
$this->collection->next(); |
84
|
|
|
$this->assertTrue($this->collection->isInitialized()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @group DDC-3382 |
89
|
|
|
*/ |
90
|
|
|
public function testNonObjects() |
91
|
|
|
{ |
92
|
|
|
$this->setUpPersistentCollection(); |
93
|
|
|
|
94
|
|
|
$this->assertEmpty($this->collection); |
95
|
|
|
|
96
|
|
|
$this->collection->add("dummy"); |
97
|
|
|
|
98
|
|
|
$this->assertNotEmpty($this->collection); |
99
|
|
|
|
100
|
|
|
$product = new ECommerceProduct(); |
101
|
|
|
|
102
|
|
|
$this->collection->set(1, $product); |
103
|
|
|
$this->collection->set(2, "dummy"); |
104
|
|
|
$this->collection->set(3, null); |
105
|
|
|
|
106
|
|
|
$this->assertSame($product, $this->collection->get(1)); |
107
|
|
|
$this->assertSame("dummy", $this->collection->get(2)); |
108
|
|
|
$this->assertSame(null, $this->collection->get(3)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Test that PersistentCollection::clear() clear elements, and reset keys |
113
|
|
|
*/ |
114
|
|
|
public function testClear() |
115
|
|
|
{ |
116
|
|
|
$this->setUpPersistentCollection(); |
117
|
|
|
|
118
|
|
|
$this->collection->add('dummy'); |
119
|
|
|
$this->assertEquals([0], array_keys($this->collection->toArray())); |
120
|
|
|
|
121
|
|
|
$this->collection->removeElement('dummy'); |
122
|
|
|
$this->assertEquals([], array_keys($this->collection->toArray())); |
123
|
|
|
|
124
|
|
|
$this->collection->add('dummy'); |
125
|
|
|
$this->collection->clear(); |
126
|
|
|
$this->assertEquals([], array_keys($this->collection->toArray())); |
127
|
|
|
|
128
|
|
|
// fix clear doesn't reset collection keys when collection is empty |
129
|
|
|
$this->collection->add('dummy'); |
130
|
|
|
$this->collection->removeElement('dummy'); |
131
|
|
|
$this->collection->clear(); |
132
|
|
|
$this->collection->add('dummy'); |
133
|
|
|
$this->assertEquals([0], array_keys($this->collection->toArray())); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|