Repository navigation
Expand file tree
/
Copy pathPhalcon1.php
More file actions
290 lines (266 loc) · 8.55 KB
/
Copy pathPhalcon1.php
File metadata and controls
290 lines (266 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
namespace Codeception\Module;
use Codeception\Exception\ModuleConfig;
use Codeception\Step;
use Codeception\Util\Connector\PhalconMemorySession;
/**
* This module provides integration with [Phalcon framework](http://www.phalconphp.com/) (1.x).
*
* ## Demo Project
*
* <https://github.com/phalcon/forum>
*
* The following configurations are required for this module:
* <ul>
* <li>boostrap - the path of the application bootstrap file</li>
* <li>cleanup - cleanup database (using transactions)</li>
* <li>savepoints - use savepoints to emulate nested transactions</li>
* </ul>
*
* The application bootstrap file must return Application object but not call its handle() method.
*
* Sample bootstrap (`app/config/bootstrap.php`):
*
* ``` php
* <?php
* $config = include __DIR__ . "/config.php";
* include __DIR__ . "/loader.php";
* $di = new \Phalcon\DI\FactoryDefault();
* include __DIR__ . "/services.php";
* return new \Phalcon\Mvc\Application($di);
* ?>
* ```
*
* You can use this module by setting params in your functional.suite.yml:
* <pre>
* class_name: TestGuy
* modules:
* enabled: [FileSystem, TestHelper, Phalcon1]
* config:
* Phalcon1
* bootstrap: 'app/config/bootstrap.php'
* cleanup: true
* savepoints: true
* </pre>
*
*
* ## Status
*
* Maintainer: **cujo**
* Stability: **alfa**
*
*
*/
class Phalcon1 extends \Codeception\Util\Framework
{
protected $config = array(
'bootstrap' => 'app/config/bootstrap.php',
'cleanup' => true,
'savepoints' => true,
);
/**
* @var \Phalcon\DiInterface
*/
public $di;
public function _initialize()
{
if (!file_exists(\Codeception\Configuration::projectDir() . $this->config['bootstrap'])) {
throw new ModuleConfig(__CLASS__,
"Bootstrap file does not exists in ".$this->config['bootstrap']."\n".
"Please create the bootstrap file that return Application object\n".
"And specify path to it with 'bootstrap' config\n\n".
"Sample bootstrap: \n\n<?php\n".
'$config = include __DIR__ . "/config.php";'."\n".
'include __DIR__ . "/loader.php";'."\n".
'$di = new \Phalcon\DI\FactoryDefault();'."\n".
'include __DIR__ . "/services.php";'."\n".
'return new \Phalcon\Mvc\Application($di);'
);
}
$this->client = new \Codeception\Util\Connector\Phalcon1();
}
public function _before(\Codeception\TestCase $test)
{
$bootstrap = \Codeception\Configuration::projectDir() . $this->config['bootstrap'];
$application = require $bootstrap;
if (!$application instanceof \Phalcon\DI\Injectable) {
throw new \Exception('Bootstrap must return \Phalcon\DI\Injectable object');
}
$this->di = $application->getDi();
\Phalcon\DI::reset();
\Phalcon\DI::setDefault($this->di);
if (isset($this->di['session'])) {
$this->di['session'] = new PhalconMemorySession();
}
if (isset($this->di['cookies'])) {
$this->di['cookies']->useEncryption(false);
}
if ($this->config['cleanup'] && isset($this->di['db'])) {
if ($this->config['savepoints']) {
$this->di['db']->setNestedTransactionsWithSavepoints(true);
}
$this->di['db']->begin();
}
$this->client->setApplication(function () use ($bootstrap) {
$currentDi = \Phalcon\DI::getDefault();
$application = require $bootstrap;
$di = $application->getDi();
if (isset($currentDi['db'])) {
$di['db'] = $currentDi['db'];
}
if (isset($currentDi['session'])) {
$di['session'] = $currentDi['session'];
}
if (isset($di['cookies'])) {
$di['cookies']->useEncryption(false);
}
return $application;
});
}
public function _after(\Codeception\TestCase $test)
{
if ($this->config['cleanup'] && isset($this->di['db'])) {
while ($this->di['db']->isUnderTransaction()) {
$level = $this->di['db']->getTransactionLevel();
try {
$this->di['db']->rollback(true);
} catch (\PDOException $e) {}
if ($level == $this->di['db']->getTransactionLevel()) {
break;
}
}
}
$this->di = null;
\Phalcon\DI::reset();
$_SESSION = array();
$_FILES = array();
$_GET = array();
$_POST = array();
$_COOKIE = array();
$_REQUEST = array();
}
/**
* Sets value to session. Use for authorization.
*
* @param $key
* @param $val
*/
public function haveInSession($key, $val)
{
$this->di->get('session')->set($key, (string)$val);
$this->debugSection('Session', json_encode($this->di['session']->getAll()));
}
/**
* Checks that session contains value.
* If value is `null` checks that session has key.
*
* @param $key
* @param null $value
*/
public function seeInSession($key, $value = null)
{
$this->debugSection('Session', json_encode($this->di['session']->getAll()));
if (is_null($value)) {
$this->assertTrue($this->di['session']->has($key));
return;
}
$this->assertEquals($value, $this->di['session']->get($key));
}
/**
* Inserts record into the database.
*
* ``` php
* <?php
* $user_id = $I->haveRecord('Phosphorum\Models\Users', array('name' => 'Phalcon'));
* $I->haveRecord('Phosphorum\Models\Categories', array('name' => 'Testing')');
* ?>
* ```
*
* @param $model
* @param array $attributes
* @return mixed
*/
public function haveRecord($model, $attributes = array())
{
$record = $this->getModelRecord($model);
$res = $record->save($attributes);
if (!$res) {
$this->fail("Record $model was not saved. Messages: ".implode(', ', $record->getMessages()));
}
$this->debugSection($model, json_encode($record));
return $record->id;
}
/**
* Checks that record exists in database.
*
* ``` php
* $I->seeRecord('Phosphorum\Models\Categories', array('name' => 'Testing'));
* ```
*
* @param $model
* @param array $attributes
*/
public function seeRecord($model, $attributes = array())
{
$record = $this->findRecord($model, $attributes);
if (!$record) {
$this->fail("Couldn't find $model with ".json_encode($attributes));
}
$this->debugSection($model, json_encode($record));
}
/**
* Checks that record does not exist in database.
*
* ``` php
* $I->dontSeeRecord('Phosphorum\Models\Categories', array('name' => 'Testing'));
* ```
*
* @param $model
* @param array $attributes
*/
public function dontSeeRecord($model, $attributes = array())
{
$record = $this->findRecord($model, $attributes);
$this->debugSection($model, json_encode($record));
if ($record) {
$this->fail("Unexpectedly managed to find $model with ".json_encode($attributes));
}
}
/**
* Retrieves record from database
*
* ``` php
* $category = $I->grabFromDatabase('Phosphorum\Models\Categories', array('name' => 'Testing'));
* ```
*
* @param $model
* @param array $attributes
* @return mixed
*/
public function grabRecord($model, $attributes = array())
{
return $this->findRecord($model, $attributes);
}
protected function findRecord($model, $attributes = array())
{
$this->getModelRecord($model);
$query = array();
foreach ($attributes as $key => $value) {
$query[] = "$key = '$value'";
}
$query = implode(' AND ', $query);
$this->debugSection('Query', $query);
return call_user_func_array(array($model, 'findFirst'), array($query));
}
protected function getModelRecord($model)
{
if (!class_exists($model)) {
throw new \RuntimeException("Model $model does not exist");
}
$record = new $model;
if (!$record instanceof \Phalcon\Mvc\Model) {
throw new \RuntimeException("Model $model is not instance of \Phalcon\Mvc\Model");
}
return $record;
}
}