Skip to content

Commit 69fb06c

Browse files
authored
Merge pull request #4 from Codeception/develop
Fixed replacing service in DI from functional test not working
2 parents 7955917 + 0aa90f2 commit 69fb06c

File tree

4 files changed

+59
-19
lines changed

4 files changed

+59
-19
lines changed

src/Codeception/Module/Phalcon4.php

+1-19
Original file line numberDiff line numberDiff line change
@@ -165,25 +165,7 @@ public function _before(TestInterface $test)
165165
$this->di['db']->begin();
166166
$this->debugSection('Database', 'Transaction started');
167167
}
168-
169-
// localize
170-
$bootstrap = $this->bootstrapFile;
171-
$this->client->setApplication(function () use ($bootstrap) {
172-
$currentDi = Di::getDefault();
173-
/** @noinspection PhpIncludeInspection */
174-
$application = require $bootstrap;
175-
$di = $application->getDI();
176-
if ($currentDi->has('db')) {
177-
$di['db'] = $currentDi['db'];
178-
}
179-
if ($currentDi->has('session')) {
180-
$di['session'] = $currentDi['session'];
181-
}
182-
if ($di->has('cookies')) {
183-
$di['cookies']->useEncryption(false);
184-
}
185-
return $application;
186-
});
168+
$this->client->setApplication($application);
187169
}
188170

189171
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
use Phalcon\Mvc\Controller;
6+
7+
class DatetimeController extends Controller
8+
{
9+
public function indexAction()
10+
{
11+
echo "class: " . get_class($this->getDI()->get('datetime'));
12+
}
13+
14+
public function splAction()
15+
{
16+
echo spl_object_hash($this->getDI());
17+
}
18+
}

tests/_data/bootstrap.php

+16
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,27 @@ function ($view) use ($di) {
9797
});
9898

9999

100+
$di->set('datetime', function () {
101+
return new DateTime();
102+
});
103+
104+
100105
$router = $di->getRouter();
101106

102107
$router->add('/', [
103108
'controller' => 'App\Controllers\Index',
104109
'action' => 'index'
105110
])->setName('front.index');
106111

112+
$router->add('/datetime', [
113+
'controller' => 'App\Controllers\Datetime',
114+
'action' => 'index'
115+
])->setName('front.datetime');
116+
117+
$router->add('/datetime/spl', [
118+
'controller' => 'App\Controllers\Datetime',
119+
'action' => 'spl'
120+
])->setName('front.spl');
121+
122+
107123
return new Application($di);

tests/unit/Phalcon4ModuleTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,30 @@ public function testContainerMethods()
150150
$module->_after($test);
151151
}
152152

153+
public function testReplaceService()
154+
{
155+
$module = $this->getPhalconModule();
156+
$test = new Codeception\Test\Unit();
157+
$module->_before($test);
158+
$diHash = spl_object_hash($module->di);
159+
160+
$datetime = $module->grabServiceFromContainer('datetime');
161+
$this->assertInstanceOf('DateTime', $datetime);
162+
$this->assertEquals($diHash, spl_object_hash($module->di));
163+
164+
$std = $module->addServiceToContainer('datetime', function () {
165+
return new \stdClass();
166+
}, false);
167+
$this->assertInstanceOf('stdClass', $std);
168+
$this->assertInstanceOf('stdClass', $module->grabServiceFromContainer('datetime'));
169+
$this->assertEquals($diHash, spl_object_hash($module->di));
170+
$module->amOnPage('/datetime/spl');
171+
$module->see($diHash);
172+
$module->amOnPage('/datetime');
173+
$module->see('class: stdClass');
174+
$module->_after($test);
175+
}
176+
153177
public function testRoutes()
154178
{
155179
$module = $this->getPhalconModule();

0 commit comments

Comments
 (0)