One last test to make sure Dice maintains the same instance

pull/559/head
Austin Collier 10 months ago
parent 1b6c5b942b
commit 54403bede8

@ -8,7 +8,6 @@ use Closure;
use Exception; use Exception;
use flight\Engine; use flight\Engine;
use InvalidArgumentException; use InvalidArgumentException;
use ReflectionClass;
use TypeError; use TypeError;
/** /**

@ -277,21 +277,27 @@ class DispatcherTest extends TestCase
public function testExecuteStringClassDefaultContainer(): void public function testExecuteStringClassDefaultContainer(): void
{ {
$this->dispatcher->setEngine(new Engine()); $engine = new Engine();
$engine->set('test_me_out', 'You got it boss!');
$this->dispatcher->setEngine($engine);
$result = $this->dispatcher->execute(ContainerDefault::class . '->testTheContainer'); $result = $this->dispatcher->execute(ContainerDefault::class . '->testTheContainer');
$this->assertSame('You got it boss!', $result); $this->assertSame('You got it boss!', $result);
} }
public function testExecuteStringClassDefaultContainerDoubleColon(): void public function testExecuteStringClassDefaultContainerDoubleColon(): void
{ {
$this->dispatcher->setEngine(new Engine()); $engine = new Engine();
$engine->set('test_me_out', 'You got it boss!');
$this->dispatcher->setEngine($engine);
$result = $this->dispatcher->execute(ContainerDefault::class . '::testTheContainer'); $result = $this->dispatcher->execute(ContainerDefault::class . '::testTheContainer');
$this->assertSame('You got it boss!', $result); $this->assertSame('You got it boss!', $result);
} }
public function testExecuteStringClassDefaultContainerArraySyntax(): void public function testExecuteStringClassDefaultContainerArraySyntax(): void
{ {
$this->dispatcher->setEngine(new Engine()); $engine = new Engine();
$engine->set('test_me_out', 'You got it boss!');
$this->dispatcher->setEngine($engine);
$result = $this->dispatcher->execute([ ContainerDefault::class, 'testTheContainer' ]); $result = $this->dispatcher->execute([ ContainerDefault::class, 'testTheContainer' ]);
$this->assertSame('You got it boss!', $result); $this->assertSame('You got it boss!', $result);
} }

@ -13,6 +13,7 @@ use flight\util\Collection;
use PDOException; use PDOException;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use tests\classes\Container; use tests\classes\Container;
use tests\classes\ContainerDefault;
// phpcs:ignoreFile PSR2.Methods.MethodDeclaration.Underscore // phpcs:ignoreFile PSR2.Methods.MethodDeclaration.Underscore
class EngineTest extends TestCase class EngineTest extends TestCase
@ -719,6 +720,26 @@ class EngineTest extends TestCase
$this->expectOutputString('Yay! I injected a PdoWrapper, and it returned the number 5 from the database!'); $this->expectOutputString('Yay! I injected a PdoWrapper, and it returned the number 5 from the database!');
} }
public function testContainerDiceFlightEngine() {
$engine = new Engine();
$engine->set('test_me_out', 'You got it boss!');
$dice = new \Dice\Dice();
$dice = $dice->addRule('*', [
'substitutions' => [
Engine::class => $engine
]
]);
$engine->registerContainerHandler(function ($class, $params) use ($dice) {
return $dice->create($class, $params);
});
$engine->route('/container', ContainerDefault::class.'->echoTheContainer');
$engine->request()->url = '/container';
$engine->start();
$this->expectOutputString('You got it boss!');
}
public function testContainerDicePdoWrapperTestBadParams() { public function testContainerDicePdoWrapperTestBadParams() {
$engine = new Engine(); $engine = new Engine();
$dice = new \Dice\Dice(); $dice = new \Dice\Dice();

@ -12,7 +12,6 @@ class ContainerDefault
public function __construct(Engine $engine) public function __construct(Engine $engine)
{ {
$engine->set('test_me_out', 'You got it boss!');
$this->app = $engine; $this->app = $engine;
} }
@ -21,6 +20,11 @@ class ContainerDefault
return $this->app->get('test_me_out'); return $this->app->get('test_me_out');
} }
public function echoTheContainer()
{
echo $this->app->get('test_me_out');
}
public function testUi() public function testUi()
{ {
echo '<span id="infotext">Route text:</span> The container successfully injected a value into the engine! Engine class: <b>' . get_class($this->app) . '</b> test_me_out Value: <b>' . $this->app->get('test_me_out') . '</b>'; echo '<span id="infotext">Route text:</span> The container successfully injected a value into the engine! Engine class: <b>' . get_class($this->app) . '</b> test_me_out Value: <b>' . $this->app->get('test_me_out') . '</b>';

@ -144,6 +144,7 @@ Flight::group('', function () {
echo '<span id="infotext">Route text:</span> This route status is that it <span style="color:' . ($id === 'before/after' ? 'green' : 'red') . '; font-weight: bold;">' . ($id === 'before/after' ? 'succeeded' : 'failed') . ' URL Param: ' . $id . '</span>'; echo '<span id="infotext">Route text:</span> This route status is that it <span style="color:' . ($id === 'before/after' ? 'green' : 'red') . '; font-weight: bold;">' . ($id === 'before/after' ? 'succeeded' : 'failed') . ' URL Param: ' . $id . '</span>';
}); });
Flight::set('test_me_out', 'You got it boss!'); // used in /no-container route
Flight::route('/no-container', ContainerDefault::class . '->testUi'); Flight::route('/no-container', ContainerDefault::class . '->testUi');
Flight::route('/dice', Container::class . '->testThePdoWrapper'); Flight::route('/dice', Container::class . '->testThePdoWrapper');
}, [ new LayoutMiddleware() ]); }, [ new LayoutMiddleware() ]);

Loading…
Cancel
Save