Skip to content

Commit 8a25d5f

Browse files
committed
Added tests
Fixed phalcon 4 changes
1 parent 2eb0a24 commit 8a25d5f

File tree

9 files changed

+220
-10
lines changed

9 files changed

+220
-10
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"codeception/module-phpbrowser": "dev-master | ^1.0",
2424
"robmorgan/phinx": "^0.11.1",
2525
"squizlabs/php_codesniffer": "^3.4",
26-
"vimeo/psalm": "^3.6"
26+
"vimeo/psalm": "^3.6",
27+
"codeception/module-db": "^1.0"
2728
},
2829
"autoload":{
2930
"classmap": ["src/"]

docker-compose.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ services:
77
ports:
88
- 3306:3306
99
environment:
10-
- MYSQL_ALLOW_EMPTY_PASSWORD=true
11-
- MYSQL_DATABASE="phalcon_demo"
10+
- MYSQL_DATABASE=phalcon
11+
- MYSQL_ROOT_PASSWORD=password

src/Codeception/Module/Phalcon4.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Codeception\Exception\ModuleConfigException;
77
use Codeception\Exception\ModuleException;
88
use Codeception\Lib\Connector\Phalcon4 as PhalconConnector;
9+
use Codeception\Lib\Connector\Phalcon4\MemorySession as MemorySession;
910
use Codeception\Lib\Framework;
1011
use Codeception\Lib\Interfaces\ActiveRecord;
1112
use Codeception\Lib\Interfaces\PartedModule;
@@ -88,7 +89,7 @@ class Phalcon4 extends Framework implements ActiveRecord, PartedModule
8889
'bootstrap' => 'app/config/bootstrap.php',
8990
'cleanup' => true,
9091
'savepoints' => true,
91-
'session' => PhalconConnector\MemorySession::class
92+
'session' => MemorySession::class
9293
];
9394

9495
/**
@@ -120,14 +121,11 @@ public function _initialize()
120121
if (!file_exists($this->bootstrapFile)) {
121122
throw new ModuleConfigException(
122123
__CLASS__,
123-
"Bootstrap file does not exist in " . $this->config['bootstrap'] . "\n"
124+
"Bootstrap file does not exist in " . $this->bootstrapFile . "\n"
124125
. "Please create the bootstrap file that returns Application object\n"
125126
. "And specify path to it with 'bootstrap' config\n\n"
126127
. "Sample bootstrap: \n\n<?php\n"
127-
. '$config = include __DIR__ . "/config.php";' . "\n"
128-
. 'include __DIR__ . "/loader.php";' . "\n"
129128
. '$di = new \Phalcon\DI\FactoryDefault();' . "\n"
130-
. 'include __DIR__ . "/services.php";' . "\n"
131129
. 'return new \Phalcon\Mvc\Application($di);'
132130
);
133131
}
@@ -322,7 +320,8 @@ public function seeSessionHasValues(array $bindings)
322320
public function haveRecord($model, $attributes = [])
323321
{
324322
$record = $this->getModelRecord($model);
325-
$res = $record->save($attributes);
323+
$record->assign($attributes);
324+
$res = $record->save();
326325
$field = function ($field) {
327326
if (is_array($field)) {
328327
return implode(', ', $field);

tests/_data/bootstrap-micro.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
$di = new \Phalcon\DI\FactoryDefault();
3+
return new \Phalcon\Mvc\Micro($di);

tests/_data/bootstrap.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
use Phalcon\Session\Manager;
3+
use Phalcon\Session\Adapter\Stream;
4+
use Phalcon\Mvc\Application;
5+
use Phalcon\DI\FactoryDefault;
6+
use Phalcon\Db\Adapter\Pdo\Mysql;
7+
8+
$di = new FactoryDefault();
9+
$di->setShared(
10+
'session',
11+
function () {
12+
$session = new Manager();
13+
$files = new Stream(
14+
[
15+
'savePath' => '/tmp',
16+
]
17+
);
18+
$session->setAdapter($files);
19+
$session->start();
20+
21+
return $session;
22+
}
23+
);
24+
25+
$di->set(
26+
'db',
27+
function () {
28+
return new Mysql(
29+
[
30+
'host' => '127.0.0.1',
31+
'username' => 'root',
32+
'password' => 'password',
33+
'dbname' => 'phalcon',
34+
]
35+
);
36+
}
37+
);
38+
return new Application($di);

tests/_data/models/test.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
class Test extends \Phalcon\Mvc\Model {
4+
5+
}

tests/_data/structure.sql

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE DATABASE IF NOT EXISTS phalcon CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
2+
create table test
3+
(
4+
id int auto_increment,
5+
name varchar(255) null,
6+
constraint test_pk
7+
primary key (id)
8+
);

tests/unit.suite.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@ modules:
77
enabled:
88
- Asserts
99
- \Helper\Unit
10-
step_decorators: ~
10+
- Db:
11+
dsn: 'mysql:host=127.0.0.1;dbname=phalcon'
12+
user: 'root'
13+
password: 'password'
14+
populate: no
15+
cleanup: true
16+
dump: 'tests/_data/structure.sql'
17+
step_decorators: ~

tests/unit/Phalcon4ModuleTest.php

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
use Codeception\Util\Autoload;
4+
use Codeception\Module\Phalcon4;
5+
use Codeception\Exception\ModuleConfigException;
6+
7+
class Phalcon4ModuleTest extends \Codeception\Test\Unit
8+
{
9+
/**
10+
* @var \UnitTester
11+
*/
12+
protected $tester;
13+
14+
protected function _setUp()
15+
{
16+
Autoload::addNamespace('Codeception\Module', BASE_PATH . '/src/Codeception/Module');
17+
Autoload::addNamespace('Codeception\Lib\Connector\Phalcon4', BASE_PATH . '/src/Codeception/Lib/Connector/Phalcon4');
18+
require_once BASE_PATH . '/src/Codeception/Lib/Connector/Phalcon4.php';
19+
require_once BASE_PATH . '/src/Codeception/Lib/Connector/Phalcon4/MemorySession.php';
20+
}
21+
22+
protected function _before()
23+
{
24+
25+
}
26+
27+
protected function _after()
28+
{
29+
}
30+
31+
protected function getPhalconModule()
32+
{
33+
$container = \Codeception\Util\Stub::make('Codeception\Lib\ModuleContainer');
34+
$module = new Phalcon4($container);
35+
$module->_setConfig([
36+
'bootstrap' => 'tests/_data/bootstrap.php',
37+
'cleanup' => true,
38+
'savepoints' => true,
39+
'session' => 'Codeception\Lib\Connector\Phalcon4\MemorySession'
40+
]);
41+
$module->_initialize();
42+
return $module;
43+
}
44+
45+
protected function getPhalconModuleMicro()
46+
{
47+
$container = \Codeception\Util\Stub::make('Codeception\Lib\ModuleContainer');
48+
$module = new Phalcon4($container);
49+
$module->_setConfig([
50+
'bootstrap' => 'tests/_data/bootstrap-micro.php',
51+
'cleanup' => true,
52+
'savepoints' => true,
53+
'session' => PhalconConnector\MemorySession::class
54+
]);
55+
$module->_initialize();
56+
return $module;
57+
}
58+
59+
public function testConstruct()
60+
{
61+
$container = \Codeception\Util\Stub::make('Codeception\Lib\ModuleContainer');
62+
$module = new Phalcon4($container);
63+
$this->assertInstanceOf('Codeception\Module\Phalcon4', $module);
64+
}
65+
66+
public function testInitialize()
67+
{
68+
$module = $this->getPhalconModule();
69+
$this->assertInstanceOf('Codeception\Lib\Connector\Phalcon4', $module->client);
70+
}
71+
72+
public function testBefore()
73+
{
74+
$module = $this->getPhalconModule();
75+
$test = new Codeception\Test\Unit();
76+
$module->_before($test);
77+
$this->assertInstanceOf('Phalcon\Di', $module->di);
78+
$this->assertInstanceOf('Phalcon\Di', $module->di);
79+
}
80+
81+
public function testAfter()
82+
{
83+
$module = $this->getPhalconModule();
84+
$test = new Codeception\Test\Unit();
85+
$module->_before($test);
86+
$module->_after($test);
87+
$this->assertNull($module->di);
88+
}
89+
90+
public function testParts()
91+
{
92+
$module = $this->getPhalconModule();
93+
$this->assertEquals(['orm', 'services'], $module->_parts());
94+
}
95+
96+
public function testGetApplication()
97+
{
98+
$module = $this->getPhalconModule();
99+
$test = new Codeception\Test\Unit();
100+
$module->_before($test);
101+
$this->assertInstanceOf('Phalcon\Mvc\Application', $module->getApplication());
102+
103+
$module = $this->getPhalconModuleMicro();
104+
$test = new Codeception\Test\Unit();
105+
$module->_before($test);
106+
$this->assertInstanceOf('Phalcon\Mvc\Micro', $module->getApplication());
107+
}
108+
109+
public function testSession()
110+
{
111+
$module = $this->getPhalconModule();
112+
$test = new Codeception\Test\Unit();
113+
$module->_before($test);
114+
$key = "phalcon";
115+
$value = "Rocks!";
116+
$module->haveInSession($key, $value);
117+
$module->seeInSession($key, $value );
118+
$module->seeSessionHasValues([$key => $value]);
119+
}
120+
121+
public function testRecords()
122+
{
123+
require_once codecept_data_dir('models/test.php');
124+
125+
$module = $this->getPhalconModule();
126+
$test = new Codeception\Test\Unit();
127+
$module->_before($test);
128+
129+
$module->haveRecord('Test', ['name' => 'phalcon']);
130+
$module->seeRecord('Test', ['name' => 'phalcon']);
131+
$module->seeNumberOfRecords('Test', 1);
132+
$module->haveRecord('Test', ['name' => 'phalcon']);
133+
$module->seeNumberOfRecords('Test', 2);
134+
$module->dontSeeRecord('Test', ['name' => 'wordpress']);
135+
136+
$record = $module->grabRecord('Test', ['name' => 'phalcon']);
137+
$this->assertInstanceOf('Phalcon\Mvc\Model', $record);
138+
}
139+
140+
public function testContainerMethods()
141+
{
142+
$module = $this->getPhalconModule();
143+
$test = new Codeception\Test\Unit();
144+
$module->_before($test);
145+
146+
$session = $module->grabServiceFromContainer('session');
147+
$this->assertInstanceOf('session', $session);
148+
}
149+
}

0 commit comments

Comments
 (0)