petrknap / nette-bootstrap
Nette Bootstrap and Test-Case for PHPUnit
Requires
- php: >=5.3.6
- nette/bootstrap: ^2.3
- nette/di: ^2.3
Requires (Dev)
Conflicts
- nette/application: <2.3 || >=3
- nette/robot-loader: <2.3 || >=3
- phpunit/phpunit: <4.8 || >=5
This package is not auto-updated.
Last update: 2022-02-01 13:02:32 UTC
README
Container
Base bootstrap class is self-explanatory, just use it naturally.
Bootstrap::getContainer()->getByType("Nette\\Application\\Application")->run();
Testing
Special configuration for unit testing
If you need special configuration for unit testing (f.e. different database connection) you can simply modify Bootstrap::getConfigFiles
method.
<?php class Bootstrap extends \PetrKnap\Nette\Bootstrap\Bootstrap { protected function getConfigFiles() { return array( __DIR__ . "/cfg/config.neon", self::getOption(self::OPTION_IS_TEST_RUN) ? __DIR__ . "/cfg/test.neon" : __DIR__ . "/cfg/local.neon" ); } }
You can modify any other method the same way if you need different log dir, etc.
Test Case
There is prepared NetteTestCase, base class for your unit tests. This class requires defined constant NETTE_BOOTSTRAP_CLASS
, you can defined it in your phpunit.xml
:
<phpunit> <php> <const name="NETTE_BOOTSTRAP_CLASS" value="Bootstrap"/> </php> </phpunit>
or in your own test case:
<?php class NetteTestCase extends \PetrKnap\Nette\Bootstrap\PhpUnit\NetteTestCase { const NETTE_BOOTSTRAP_CLASS = "Bootstrap"; }
Container access
For access to application container in test use method NetteTestCase::getContainer
.
Presenter testing
Test case simulates Application\Request
instead of Http\Request
.
For presenter testing use method NetteTestCase::runPresenter
.
<?php class NetteTestCase extends \PetrKnap\Nette\Bootstrap\PhpUnit\NetteTestCase { public function testHelloWorld() { /** @var \Nette\Application\Responses\TextResponse $response */ $response = $this->runPresenter("World", "hello"); // calls WorldPresenter::actionHello $html = (string) $response->getSource(); // renders output $this->assertContains("Hello, world!", $html); } }
How to install
Run composer require petrknap/nette-bootstrap
or merge this JSON code with your project composer.json
file manually and run composer install
. Instead of dev-master
you can use one of released versions.
{ "require": { "petrknap/nette-bootstrap": "dev-master" } }
Or manually clone this repository via git clone https://github.com/petrknap/nette-bootstrap.git
or download this repository as ZIP and extract files into your project.