Nette Tester namůže načíst třídu
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- tttpapi
- Člen | 100
Ahoj,
našel jsem návod, jak si otestovat presentery a byla u něj přidána i obecná třída, která mi to všechno usnadní.
Má struktura vypadá následnovně:
tests
- presenters
- … (testy pro presentery
- bootstrap.php
- Presenter.php (obecná třída pro otestování presenteru)
V testech mám napsáno
<?php
function __construct(Nette\DI\Container $container) {
$this->tester = new Presenter($container);
}
?>
A na tomhle už to spadne, že to nezná třídu Test/Presenter.
Ta vypadá následnovně:
<?php
namespace Test;
class Presenter extends \Nette\Object {
private $container;
private $presenter;
private $presName;
public function __construct(\Nette\DI\Container $container) {
$this->container = $container;
}
/**
* @param $presName string Fully qualified presenter name.
*/
public function init($presName) {
$presenterFactory = $this->container->getByType('Nette\Application\IPresenterFactory');
$this->presenter = $presenterFactory->createPresenter($presName);
$this->presenter->autoCanonicalize = FALSE;
$this->presName = $presName;
}
public function test($action, $method = 'GET', $params = array(), $post = array()) {
$params['action'] = $action;
$request = new \Nette\Application\Request($this->presName, $method, $params, $post);
$response = $this->presenter->run($request);
return $response;
}
public function testAction($action, $method = 'GET', $params = array(), $post = array()) {
$response = $this->test($action, $method, $params, $post);
\Tester\Assert::true($response instanceof \Nette\Application\Responses\TextResponse);
\Tester\Assert::true($response->getSource() instanceof \Nette\Templating\ITemplate);
$html = (string) $response->getSource();
$dom = \Tester\DomQuery::fromHtml($html);
\Tester\Assert::true($dom->has('title'));
return $response;
}
public function testForm($action, $method = 'POST', $post = array()) {
$response = $this->test($action, $method, $post);
\Tester\Assert::true($response instanceof \Nette\Application\Responses\RedirectResponse);
return $response;
}
}
?>
Můj bootstrap.php
<?php
require __DIR__ . '/../vendor/autoload.php';
if (!class_exists('Tester\Assert')) {
echo "Install Nette Tester using `composer update --dev`\n";
exit(1);
}
Tester\Environment::setup();
function id($val) {
return $val;
}
$configurator = new Nette\Configurator;
$configurator->setDebugMode(FALSE);
$configurator->setTempDirectory(__DIR__ . '/../temp');
$configurator->createRobotLoader()
->addDirectory(__DIR__ . '/../app')
->register();
$configurator->addConfig(__DIR__ . '/../app/config/config.neon');
$configurator->addConfig(__DIR__ . '/../app/config/config.tests.neon');
$container = $configurator->createContainer();
return $container;
?>
Nějakej nápad, jak mu tam donutit tu třídu?
Díky moc.
- tttpapi
- Člen | 100
Ještě mám jeden problém. Pokud testuju komponentu, tak mi to hlásí Component '' is not attached to ‚Nette\Application\UI\Presenter‘
<?php
namespace Test;
use Nette,
Tester;
$container = require __DIR__ . '/../bootstrap.php';
class ListingTest extends Tester\TestCase {
const LIST_TYPE = "tasks_categories";
private $listing;
function __construct(Nette\DI\Container $container) {
$this->container = $container;
}
function setUp() {
/*$presenterFactory = $this->container->getByType('Nette\Application\IPresenterFactory');
//var_dump($presenterFactory);
$this->presenter = $presenterFactory->createPresenter("Lists");
$this->presenter->autoCanonicalize = FALSE*/
$this->listing = new \Listing(Nette\Database\Connection::getReflection(),
self::LIST_TYPE_1, 0);
}
function testGetForm() {
$form = $this->listing->getForm();
Tester\Assert::true($form instanceof Nette\Application\UI\Form);
}
}
id(new ListingTest($container))->run();
?>