Skip to content

Commit 03456c7

Browse files
committed
Refactored Tests
1 parent 80404ba commit 03456c7

12 files changed

+109
-50
lines changed

codeception.yml

+9
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ extensions:
1111
- Codeception\Extension\RunFailed
1212
params:
1313
- .env
14+
modules:
15+
enabled:
16+
- Db:
17+
dsn: 'mysql:host=%DB_HOST%;dbname=%DB_NAME%;port=%DB_PORT%'
18+
user: '%DB_USERNAME%'
19+
password: '%DB_PASSWORD%'
20+
populate: no
21+
cleanup: true
22+
dump: 'tests/_data/structure.sql'

tests/_bootstrap.php

+17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
use Codeception\Util\Autoload;
4+
25
// This is global bootstrap for autoloading
36

47
error_reporting(-1);
@@ -19,3 +22,17 @@
1922

2023
echo "ENV: " . APPLICATION_ENV . PHP_EOL;
2124
echo "DB_HOST: " . getenv('DB_HOST') . ":" . getenv('DB_PORT') . PHP_EOL;
25+
26+
27+
Autoload::addNamespace(
28+
'Codeception\Module',
29+
BASE_PATH . '/src/Codeception/Module'
30+
);
31+
Autoload::addNamespace(
32+
'Codeception\Lib\Connector\Phalcon4',
33+
BASE_PATH . '/src/Codeception/Lib/Connector/Phalcon4'
34+
);
35+
Autoload::addNamespace(
36+
'App',
37+
BASE_PATH . '/tests/_data/App'
38+
);

tests/_data/controllers/ContactController.php renamed to tests/_data/App/Controllers/IndexController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Phalcon\Mvc\Controller;
66

7-
class ContactController extends Controller
7+
class IndexController extends Controller
88
{
99
public function indexAction()
1010
{

tests/_data/App/Models/Articles.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Phalcon\Mvc\Model;
6+
7+
class Articles extends Model
8+
{
9+
10+
}

tests/_data/App/Views/index.volt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
layout
2+
{{ content() }}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Index
2+
{{ content() }}

tests/_data/bootstrap.php

+43-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Phalcon\DI\FactoryDefault;
88
use Phalcon\Db\Adapter\Pdo\Mysql;
99
use Phalcon\Url as UrlProvider;
10+
use Phalcon\Mvc\View\Engine\Volt;
11+
use Phalcon\Loader;
1012

1113
$di = new FactoryDefault();
1214
$di->setShared(
@@ -43,11 +45,48 @@ function () {
4345
/**
4446
* Setting the View
4547
*/
46-
$di->setShared('view', function () {
48+
$di->setShared('view', function () use ($di) {
4749
$view = new View();
50+
// $view->setViewsDir(BASE_PATH . '/_data/App/Views/');
51+
// $view->registerEngines(
52+
// [
53+
// ".volt" => "voltService"
54+
// ]
55+
// );
56+
// $eventsManager = $di->get('eventsManager');
57+
// $eventsManager->attach('view', function ($event, $view) use ($di) {
58+
// /**
59+
// * @var \Phalcon\Events\Event $event
60+
// * @var \Phalcon\Mvc\View $view
61+
// */
62+
// if ($event->getType() == 'notFoundView') {
63+
// $message = sprintf('View not found - %s', $view->getActiveRenderPath());
64+
// throw new Exception($message);
65+
// }
66+
// });
67+
// $view->setEventsManager($eventsManager);
4868
return $view;
4969
});
5070

71+
/**
72+
* Volt Service
73+
*/
74+
$di->set(
75+
'voltService',
76+
function ($view) use ($di) {
77+
$volt = new Volt($view, $di);
78+
79+
$volt->setOptions(
80+
[
81+
'compiledPath' => BASE_PATH . '/_output/compiled-templates/',
82+
'compiledExtension' => '.compiled',
83+
]
84+
);
85+
86+
return $volt;
87+
}
88+
);
89+
5190
/**
5291
* The URL component is used to generate all kind of urls in the application
5392
*/
@@ -60,10 +99,9 @@ function () {
6099

61100
$router = $di->getRouter();
62101

63-
$router->add('/contact', [
64-
'controller' => 'App\Controllers\Contact',
102+
$router->add('/', [
103+
'controller' => 'App\Controllers\Index',
65104
'action' => 'index'
66-
])->setName('front.contact');
67-
105+
])->setName('front.index');
68106

69107
return new Application($di);

tests/_data/models/test.php

-8
This file was deleted.

tests/_data/structure.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
CREATE DATABASE IF NOT EXISTS phalcon CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
2-
create table test
2+
create table articles
33
(
44
id int auto_increment,
5-
name varchar(255) null,
5+
title varchar(255) null,
66
constraint test_pk
77
primary key (id)
88
);

tests/functional.suite.yml

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,15 @@ modules:
1010
enabled:
1111
# add a framework module here
1212
- \Helper\Functional
13-
step_decorators: ~
13+
- Db:
14+
dsn: 'mysql:host=%DB_HOST%;dbname=%DB_NAME%;port=%DB_PORT%'
15+
user: '%DB_USERNAME%'
16+
password: '%DB_PASSWORD%'
17+
populate: no
18+
cleanup: true
19+
dump: 'tests/_data/structure.sql'
20+
- Phalcon4:
21+
bootstrap: tests/_data/bootstrap.php
22+
cleanup: true
23+
savepoints: true
24+
step_decorators: ~

tests/unit.suite.yml

-7
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,4 @@ modules:
77
enabled:
88
- Asserts
99
- \Helper\Unit
10-
- Db:
11-
dsn: 'mysql:host=%DB_HOST%;dbname=%DB_NAME%;port=%DB_PORT%'
12-
user: '%DB_USERNAME%'
13-
password: '%DB_PASSWORD%'
14-
populate: no
15-
cleanup: true
16-
dump: 'tests/_data/structure.sql'
1710
step_decorators: ~

tests/unit/Phalcon4ModuleTest.php

+11-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use Codeception\Util\Autoload;
43
use Codeception\Module\Phalcon4;
54
use Codeception\Exception\ModuleConfigException;
65

@@ -13,16 +12,6 @@ class Phalcon4ModuleTest extends \Codeception\Test\Unit
1312

1413
protected function _setUp()
1514
{
16-
Autoload::addNamespace(
17-
'Codeception\Module',
18-
BASE_PATH . '/src/Codeception/Module'
19-
);
20-
Autoload::addNamespace(
21-
'Codeception\Lib\Connector\Phalcon4',
22-
BASE_PATH . '/src/Codeception/Lib/Connector/Phalcon4'
23-
);
24-
require_once BASE_PATH . '/src/Codeception/Lib/Connector/Phalcon4.php';
25-
require_once BASE_PATH . '/src/Codeception/Lib/Connector/Phalcon4/MemorySession.php';
2615
}
2716

2817
protected function _before()
@@ -127,20 +116,18 @@ public function testSession()
127116

128117
public function testRecords()
129118
{
130-
require_once codecept_data_dir('models/test.php');
131-
132119
$module = $this->getPhalconModule();
133120
$test = new Codeception\Test\Unit();
134121
$module->_before($test);
135122

136-
$module->haveRecord('App\Models\Test', ['name' => 'phalcon']);
137-
$module->seeRecord('App\Models\Test', ['name' => 'phalcon']);
138-
$module->seeNumberOfRecords('App\Models\Test', 1);
139-
$module->haveRecord('App\Models\Test', ['name' => 'phalcon']);
140-
$module->seeNumberOfRecords('App\Models\Test', 2);
141-
$module->dontSeeRecord('App\Models\Test', ['name' => 'wordpress']);
123+
$module->haveRecord('App\Models\Articles', ['title' => 'phalcon']);
124+
$module->seeRecord('App\Models\Articles', ['title' => 'phalcon']);
125+
$module->seeNumberOfRecords('App\Models\Articles', 1);
126+
$module->haveRecord('App\Models\Articles', ['title' => 'phalcon']);
127+
$module->seeNumberOfRecords('App\Models\Articles', 2);
128+
$module->dontSeeRecord('App\Models\Articles', ['title' => 'wordpress']);
142129

143-
$record = $module->grabRecord('App\Models\Test', ['name' => 'phalcon']);
130+
$record = $module->grabRecord('App\Models\Articles', ['title' => 'phalcon']);
144131
$this->assertInstanceOf('Phalcon\Mvc\Model', $record);
145132

146133
$module->_after($test);
@@ -155,24 +142,22 @@ public function testContainerMethods()
155142
$session = $module->grabServiceFromContainer('session');
156143
$this->assertInstanceOf('Codeception\Lib\Connector\Phalcon4\MemorySession', $session);
157144

158-
$testService = $module->addServiceToContainer('App\Models\Test', function () {
145+
$testService = $module->addServiceToContainer('std', function () {
159146
return new \stdClass();
160147
}, true);
161-
$this->assertInstanceOf('stdClass', $module->grabServiceFromContainer('App\Models\Test'));
148+
$this->assertInstanceOf('stdClass', $module->grabServiceFromContainer('std'));
162149
$this->assertInstanceOf('stdClass', $testService);
163150
$module->_after($test);
164151
}
165152

166153
public function testRoutes()
167154
{
168-
require_once codecept_data_dir('controllers/ContactController.php');
169-
170155
$module = $this->getPhalconModule();
171156
$test = new Codeception\Test\Unit();
172157
$module->_before($test);
173158

174-
$module->amOnRoute('front.contact');
175-
$module->seeCurrentRouteIs('front.contact');
159+
$module->amOnRoute('front.index');
160+
$module->seeCurrentRouteIs('front.index');
176161
$module->_after($test);
177162
}
178163
}

0 commit comments

Comments
 (0)