1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fwk\Db; |
4
|
|
|
use Fwk\Db\Events\AfterQueryEvent; |
5
|
|
|
use Fwk\Db\Events\BeforeQueryEvent; |
6
|
|
|
use Fwk\Db\Events\ConnectEvent; |
7
|
|
|
use Fwk\Db\Events\ConnectionErrorEvent; |
8
|
|
|
use Fwk\Db\Events\ConnectionStateChangeEvent; |
9
|
|
|
use Fwk\Db\Events\DisconnectEvent; |
10
|
|
|
use Fwk\Db\Exceptions\ConnectionErrorException; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class TestConnectionListener |
14
|
|
|
{ |
15
|
|
|
public $disconnected = true; |
16
|
|
|
public $hasErrored = false; |
17
|
|
|
public $states = 0; |
18
|
|
|
public $currentState; |
19
|
|
|
|
20
|
|
|
public $currentQuery; |
21
|
|
|
public $currentQueryParams; |
22
|
|
|
public $currentQueryOpts; |
23
|
|
|
public $currentQueryResults; |
24
|
|
|
|
25
|
|
|
public $queries = 0; |
26
|
|
|
|
27
|
|
|
public function onConnect(ConnectEvent $event) |
28
|
|
|
{ |
29
|
|
|
$this->currentState = $event->getConnection()->getState(); |
30
|
|
|
$this->disconnected = false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function onDisconnect(DisconnectEvent $event) |
34
|
|
|
{ |
35
|
|
|
$this->disconnected = true; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function onConnectionError(ConnectionErrorEvent $event) |
39
|
|
|
{ |
40
|
|
|
$this->hasErrored = $event->getException(); |
|
|
|
|
41
|
|
|
if($event->getConnection()->isConnected()) { |
42
|
|
|
$event->getConnection()->disconnect(); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function onConnectionStateChange(ConnectionStateChangeEvent $event) |
47
|
|
|
{ |
48
|
|
|
if ($event->getNewState() != $event->getPreviousState()) { |
49
|
|
|
$this->states++; |
50
|
|
|
} |
51
|
|
|
$this->currentState = $event->getConnection()->getState(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function onBeforeQuery(BeforeQueryEvent $event) |
55
|
|
|
{ |
56
|
|
|
$opts = $event->getQueryOptions(); |
57
|
|
|
$event->setQueryOptions($opts + array('testopt' => true)); |
58
|
|
|
$results = $event->getResults(); |
|
|
|
|
59
|
|
|
|
60
|
|
|
if (!isset($this->currentQueryResults)) { |
61
|
|
|
$this->currentQuery = $event->getQuery(); |
62
|
|
|
$this->currentQueryParams = $event->getQueryParameters(); |
63
|
|
|
$this->currentQueryOpts = $event->getQueryOptions(); |
64
|
|
|
|
65
|
|
|
$event->setResults(666); |
66
|
|
|
$event->stop(); |
67
|
|
|
$this->currentQueryResults = true; |
68
|
|
|
} else { |
69
|
|
|
$event->setQuery(Query::factory()); |
70
|
|
|
$event->setQueryParameters(array('driver_opt' => 'one')); |
71
|
|
|
$event->setResults(null); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function onAfterQuery(AfterQueryEvent $event) |
76
|
|
|
{ |
77
|
|
|
$this->queries++; |
78
|
|
|
$this->currentQueryResults = $event->getResults(); |
79
|
|
|
$this->currentState = $event->getConnection()->getState(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
/** |
83
|
|
|
* Test class for EventDispatcher. |
84
|
|
|
*/ |
85
|
|
|
class ConnectionEventsTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
/** |
88
|
|
|
* @var Connection |
89
|
|
|
*/ |
90
|
|
|
protected $object; |
91
|
|
|
protected $listener; |
92
|
|
|
protected $listener2; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Sets up the fixture, for example, opens a network connection. |
96
|
|
|
* This method is called before a test is executed. |
97
|
|
|
*/ |
98
|
|
|
protected function setUp() |
99
|
|
|
{ |
100
|
|
|
$this->object = new Connection(array( |
101
|
|
|
'memory' => true, |
102
|
|
|
'driver' => 'pdo_sqlite' |
103
|
|
|
)); |
104
|
|
|
|
105
|
|
|
$this->object->addListener($this->listener = new TestConnectionListener()); |
106
|
|
|
|
107
|
|
|
\FwkDbTestUtil::createTestDb($this->object); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function tearDown() |
111
|
|
|
{ |
112
|
|
|
try { |
113
|
|
|
\FwkDbTestUtil::dropTestDb($this->object); |
114
|
|
|
} catch(ConnectionErrorException $exp) { |
|
|
|
|
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testConnectAndDisconnectEvents() |
119
|
|
|
{ |
120
|
|
|
$this->assertFalse($this->listener->disconnected); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testConnectionErrorEvent() |
124
|
|
|
{ |
125
|
|
|
$this->object = new Connection(array( |
126
|
|
|
'driver' => 'pdo_mysql', |
127
|
|
|
'host' => 'inexistant.example.com-no', |
128
|
|
|
'user' => 'testEUH', |
129
|
|
|
'autoConnect' => false |
130
|
|
|
)); |
131
|
|
|
|
132
|
|
|
$this->object->addListener($this->listener = new TestConnectionListener()); |
133
|
|
|
|
134
|
|
|
$this->assertFalse($this->object->isConnected()); |
135
|
|
|
$this->assertFalse($this->listener->hasErrored); |
136
|
|
|
|
137
|
|
|
$this->setExpectedException('\Fwk\Db\Exceptions\ConnectionErrorException'); |
138
|
|
|
$this->object->connect(); |
139
|
|
|
|
140
|
|
|
$this->assertTrue(($this->listener->hasErrored instanceof Exception)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testConnectionStateChangeEvent() |
144
|
|
|
{ |
145
|
|
|
$connx = new Connection(array( |
146
|
|
|
'driver' => 'pdo_mysql', |
147
|
|
|
'host' => 'inexistant.example.com-no', |
148
|
|
|
'user' => 'testEUH', |
149
|
|
|
'autoConnect' => false |
150
|
|
|
)); |
151
|
|
|
|
152
|
|
|
$connx->addListener($this->listener2 = new TestConnectionListener()); |
153
|
|
|
|
154
|
|
|
$this->assertEquals(0, $this->listener2->states); |
155
|
|
|
$this->setExpectedException('\Fwk\Db\Exceptions\ConnectionErrorException'); |
156
|
|
|
$connx->connect(); |
157
|
|
|
|
158
|
|
|
$this->assertEquals(2 /* connect + error */, $this->listener2->states); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testBeforeAndAfterQueryEvents() |
162
|
|
|
{ |
163
|
|
|
$query = Query::factory() |
164
|
|
|
->insert('fwkdb_test_users') |
165
|
|
|
->set('username', 'joeBar'); |
166
|
|
|
|
167
|
|
|
$this->object->execute($query); |
168
|
|
|
$this->assertEquals(0, $this->listener->queries); // should have been stopped |
169
|
|
|
|
170
|
|
|
$query2 = Query::factory() |
171
|
|
|
->insert('fwkdb_test_users') |
172
|
|
|
->set('username', 'joeBar'); |
173
|
|
|
|
174
|
|
|
$this->object->execute($query2); |
175
|
|
|
$this->assertEquals(1, $this->listener->queries); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..