1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\Functional\Ticket; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Event\PreFlushEventArgs; |
6
|
|
|
use Doctrine\ORM\Events; |
7
|
|
|
|
8
|
|
|
class Ticket5848Test extends \Doctrine\Tests\OrmFunctionalTestCase |
9
|
|
|
{ |
10
|
|
|
private $collector; |
11
|
|
|
private $listener; |
12
|
|
|
|
13
|
|
|
protected function setUp() |
14
|
|
|
{ |
15
|
|
|
parent::setUp(); |
16
|
|
|
$this->collector = new Ticket5848EntityCollector(); |
17
|
|
|
$this->listener = new Ticket5848PreFlushListener(); |
18
|
|
|
|
19
|
|
|
$this->listener->collector = $this->collector; |
20
|
|
|
|
21
|
|
|
try { |
22
|
|
|
$this->_schemaTool->createSchema(array( |
23
|
|
|
$this->_em->getClassMetadata(__NAMESPACE__ . '\Ticket5848Authentication'), |
24
|
|
|
$this->_em->getClassMetadata(__NAMESPACE__ . '\Ticket5848Client'), |
25
|
|
|
)); |
26
|
|
|
} catch (\Exception $e) { |
27
|
|
|
// Swallow all exceptions. We do not test the schema tool here. |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
$this->_em->getEventManager()->addEventListener(Events::preFlush, $this->listener); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testExplicitFlushWithOne() |
35
|
|
|
{ |
36
|
|
|
$e = new Ticket5848Client(); |
37
|
|
|
|
38
|
|
|
$this->_em->persist($e); |
39
|
|
|
$this->_em->flush($e); |
40
|
|
|
|
41
|
|
|
self::assertSame($e, $this->collector->entities[0]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testExplicitFlushWithMany() |
45
|
|
|
{ |
46
|
|
|
$e1 = new Ticket5848Client(); |
47
|
|
|
$e2 = new Ticket5848Authentication(); |
48
|
|
|
$e3 = new Ticket5848Client(); |
49
|
|
|
|
50
|
|
|
$this->_em->persist($e1); |
51
|
|
|
$this->_em->persist($e2); |
52
|
|
|
$this->_em->persist($e3); |
53
|
|
|
$this->_em->flush([$e1, $e2]); |
54
|
|
|
|
55
|
|
|
self::assertSame($e1, $this->collector->entities[0]); |
56
|
|
|
self::assertSame($e2, $this->collector->entities[1]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testExplicitFlushWithConsecutiveFlushes() |
60
|
|
|
{ |
61
|
|
|
$e1 = new Ticket5848Client(); |
62
|
|
|
$e2 = new Ticket5848Authentication(); |
63
|
|
|
$e3 = new Ticket5848Client(); |
64
|
|
|
|
65
|
|
|
$this->_em->persist($e1); |
66
|
|
|
$this->_em->persist($e2); |
67
|
|
|
$this->_em->persist($e3); |
68
|
|
|
$this->_em->flush([$e1, $e2]); |
69
|
|
|
$this->_em->flush($e3); |
70
|
|
|
|
71
|
|
|
self::assertSame($e3, $this->collector->entities[0]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testImplicitFlush() |
75
|
|
|
{ |
76
|
|
|
$e1 = new Ticket5848Client(); |
77
|
|
|
$e2 = new Ticket5848Authentication(); |
78
|
|
|
$e3 = new Ticket5848Client(); |
79
|
|
|
|
80
|
|
|
$this->_em->persist($e1); |
81
|
|
|
$this->_em->persist($e2); |
82
|
|
|
$this->_em->persist($e3); |
83
|
|
|
$this->_em->flush(); |
84
|
|
|
|
85
|
|
|
self::assertSame($e1, $this->collector->entities[0]); |
86
|
|
|
self::assertSame($e2, $this->collector->entities[1]); |
87
|
|
|
self::assertSame($e3, $this->collector->entities[2]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testExplicitFlushAfterImplicitFlush() |
91
|
|
|
{ |
92
|
|
|
$e1 = new Ticket5848Client(); |
93
|
|
|
$e2 = new Ticket5848Authentication(); |
94
|
|
|
$e3 = new Ticket5848Client(); |
95
|
|
|
|
96
|
|
|
$this->_em->persist($e1); |
97
|
|
|
$this->_em->persist($e2); |
98
|
|
|
$this->_em->persist($e3); |
99
|
|
|
$this->_em->flush(); |
100
|
|
|
|
101
|
|
|
self::assertSame($e1, $this->collector->entities[0]); |
102
|
|
|
self::assertSame($e2, $this->collector->entities[1]); |
103
|
|
|
self::assertSame($e3, $this->collector->entities[2]); |
104
|
|
|
|
105
|
|
|
$e2->username = 'foo'; |
106
|
|
|
|
107
|
|
|
$this->_em->flush([$e2, $e3]); |
108
|
|
|
|
109
|
|
|
self::assertSame($e2, $this->collector->entities[0]); |
110
|
|
|
self::assertSame($e3, $this->collector->entities[1]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
class Ticket5848PreFlushListener |
115
|
|
|
{ |
116
|
|
|
public $collector; |
117
|
|
|
|
118
|
|
|
public function preFlush(PreFlushEventArgs $event) |
119
|
|
|
{ |
120
|
|
|
if (!$event->hasEntities()) { |
121
|
|
|
$uow = $event->getEntityManager()->getUnitOfWork(); |
122
|
|
|
$uow->computeChangeSets(); |
123
|
|
|
|
124
|
|
|
$entities = array_values(array_merge($uow->getScheduledEntityInsertions(), $uow->getScheduledEntityUpdates())); |
125
|
|
|
} else { |
126
|
|
|
$entities = $event->getEntities(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->collector->entities = $entities; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
class Ticket5848EntityCollector |
134
|
|
|
{ |
135
|
|
|
public $entities; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @Entity |
140
|
|
|
* @Table(name="ticket_2481_authentications") |
141
|
|
|
*/ |
142
|
|
|
class Ticket5848Authentication |
143
|
|
|
{ |
144
|
|
|
/** |
145
|
|
|
* @Id @Column(type="integer") |
146
|
|
|
* @GeneratedValue(strategy="AUTO") |
147
|
|
|
*/ |
148
|
|
|
public $id; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @column(type="string", nullable=true) |
152
|
|
|
*/ |
153
|
|
|
public $username; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @Entity |
158
|
|
|
* @Table(name="ticket_2481_clients") |
159
|
|
|
*/ |
160
|
|
|
class Ticket5848Client |
161
|
|
|
{ |
162
|
|
|
/** |
163
|
|
|
* @Id @Column(type="integer") |
164
|
|
|
* @GeneratedValue(strategy="AUTO") |
165
|
|
|
*/ |
166
|
|
|
public $id; |
167
|
|
|
} |
168
|
|
|
|