i'm integrating phpunit in phalcon project. had running correctly in mamp, when run phpunit
on server, keep getting errors.
this unittestcase:
<?php use \phalcon\di; use \phalcon\di\factorydefault; use \phalcon\test\unittestcase phalcontestcase; use \phalcon\mvc\view; use \phalcon\crypt; use \phalcon\mvc\dispatcher; use \phalcon\mvc\dispatcher phdispatcher; use \phalcon\mvc\url urlresolver; use \phalcon\db\adapter\pdo\mysql dbadapter; use \phalcon\mvc\view\engine\volt voltengine; use \phalcon\mvc\model\metadata\files metadataadapter; use \phalcon\session\adapter\files sessionadapter; use \phalcon\flash\direct flash; use \phalcon\logger; use \phalcon\events\manager eventsmanager; use \phalcon\logger\adapter\file loggerfile; use \phalcon\mvc\model\manager modelsmanager; abstract class unittestcase extends phalcontestcase { /** * @var \voice\cache */ protected $_cache; /** * @var \phalcon\config */ protected $_config; /** * @var bool */ private $_loaded = false; public function setup(phalcon\diinterface $di = null, phalcon\config $config = null) { // load additional services might required during testing $di = new factorydefault(); di::reset(); $config = include app_dir . '/config/config.php'; /** * url component used generate kind of urls in application */ $di->set('url', function () use ($config) { $url = new urlresolver(); $url->setbaseuri($config->application->baseuri); return $url; }, true); /** * setting view component */ $di->set('view', function () use ($config) { $view = new view(); $view->setviewsdir($config->application->viewsdir); $view->registerengines(array( '.volt' => function ($view, $di) use ($config) { $volt = new voltengine($view, $di); $volt->setoptions(array( 'compiledpath' => $config->application->cachedir . 'volt/', 'compiledseparator' => '_' )); return $volt; } )); return $view; }, true); ...and more... $di->set( 'modelsmanager', function() { return new \phalcon\mvc\model\manager(); } ); parent::setup($di, $config); $this->_loaded = true; } /** * check if test case setup * * @throws \phpunit_framework_incompletetesterror; */ public function __destruct() { if (!$this->_loaded) { throw new \phpunit_framework_incompletetesterror('please run parent::setup().'); } } }
so, when run on local machine, works fine. when run on server, throws:
phalcon\di\exception: service 'modelsmanager' wasn't found in dependency injection container
what doing wrong?
instead of setting separately in test, should rather go kind of bootstrap solution.
my bootstrap file encloses in short testhelper.php:
<?php ini_set('display_errors',1); error_reporting(e_all); defined('application_env') || define('application_env', getenv('application_env') ?: 'developer'); defined('application_dir') || define('application_dir', 'app'); defined('application_path') || define('application_path', realpath(dirname(__file__) . directory_separator . '..' . directory_separator . application_dir) . directory_separator); use phalcon\di; $config = include_once(application_path . 'config' . directory_separator . 'config.php'); $di = new phalcon\di\factorydefault(); $di->set('config', $config); $application = new \phalcon\mvc\application($di); include_once(application_path . 'autoload.php'); di::setdefault($di); $_session = [];
supported proper configuration in phpunit.xml:
<?xml version="1.0" encoding="utf-8"?> <phpunit bootstrap="./testhelper.php" backupglobals="false" backupstaticattributes="true" verbose="false" colors="true" converterrorstoexceptions="true" convertnoticestoexceptions="true" convertwarningstoexceptions="true" processisolation="false" stoponfailure="false" syntaxcheck="true"> <testsuites> <testsuite name="application - testsuite"> <directory>tests</directory> </testsuite> </testsuites> <filter> <blacklist> <directory>../vendor</directory> </blacklist> </filter> <logging> <log type="coverage-html" target="../public/build/coverage" title="php code coverage" charset="utf-8" yui="true" highlight="true" lowupperbound="35" highlowerbound="70"/> <log type="coverage-clover" target="../public/build/logs/clover.xml"/> <log type="junit" target="../public/build/logs/junit.xml" logincompleteskipped="false"/> </logging> </phpunit>
bootstrap file should have necessary full application run. remember, testing things, should not replace logic, define should defined @ beggining of standard index.php
file , include has bu included, keep away running related $application->handle()
. warming app, building dependencies it. final index.php
differ bootsrap 1 try/catch block, custom error2exception handler , and echo $application->handle()->getcontent();
.
all enclosed in structure this:
* app/ | |- config/ | |- ... | '- autoload.php * public/ | |- css/ | |- ... | '- index php * vendor/ * tests/ * app/ |- // whole app/ structure |- testhelper.php '- phpunit.xml
once app - guess - working except tests, should try go proper way inherit original struture, instead of declaring each test.
exampleous test /tests/app/models/services/deviationtest.php
/app/models/services/deviation.php
:
<?php namespace application\services; use \application\entities\keywords; /** * generated phpunit_skeletongenerator on 2015-09-18 @ 10:24:22. */ class deviationtest extends \phpunit_framework_testcase { /** * @var deviation */ protected $object; protected $keyword; protected function setup() { global $di; $this->object = new deviation($di); $this->keyword = keywords::findfirst('enabled = true , text = "biura podróży"'); } protected function teardown() { } public function testgetdi() { $this->object->getdi(); } public function testget() { $this->object->get($this->keyword); $this->object->get($this->keyword, date('y-m-d', strtotime('-1 day'))); $this->object->get($this->keyword, date('y-m-d')); return $this->object; } public function testgetondate() { $result = $this->object->get($this->keyword, date('y-m-d', strtotime('-1 day'))); print_r($result); } /** * @depends testget */ public function testcache($object) { $result = $object->get($this->keyword, date('y-m-d', strtotime('-1 day'))); $result = $object->get($this->keyword, date('y-m-d', strtotime('-1 day'))); $result = $object->get($this->keyword, date('y-m-d', strtotime('-1 day'))); } }
as methods made in way if goes wrong throw exceptions, don't have need make assertions - now, it's acceptance test.
Comments
Post a Comment