php - Zend Framework & PHPunit path issues? -


when extend zf class, path errors. here test:

<?php require_once ('zend/acl.php'); // gives error w/o require_once ('../application/models/acl.php');  class model_acltest extends controllertestcase {      /**         * @var model_acl         */     protected $acl;      public function setup()     {         parent::setup();         $this->acl = new model_acl(2, 'sims');     }      public function testhasusername()     {         echo $this->acl->username;         $this->assert($this->acl->username);     } } 

here model_acl class:

class model_acl extends zend_acl {     private $username;     private $userid;     private $userroles;     private $allroles;      public function __construct($userid, $username)     {         if (method_exists(parent, '__construct')) parent::__construct();         $this->username = $username;         $this->userid = $userid;         $this->setroles();         $this->setresources();         $this->setprivilages();         return $this;     } ... } 

i error, unless require 'zend/acl.php'.

php fatal error:  class 'zend_acl' not found in /mnt/dev/mragent/application/models/acl.php on line 6 

so figure maybe not quite snuff yet, , continue slight inconvenience. following error phpunit itself:

1) model_acltest::testhasusername use of undefined constant parent - assumed 'parent'  /mnt/dev/mragent/application/models/acl.php:14 /mnt/dev/mragent/tests/application/models/acltest.php:18 

so wrong think. or i'm doing wrong.

how set path(s) correctly?

the first thing you'll want @ following line:

class model_acl extends zend_acl 

this reason need following:

require_once ('zend/acl.php'); // gives error w/o 

in order things working without adding above line, you'll need setup include paths autoloader can find acl.php file directly under 'zend' directory. include path needs include directory parent of 'zend' directory. "library" directory.

as "parent" issue:

use of undefined constant parent - assumed 'parent' 

you seeing because have following code:

if (method_exists(parent, '__construct')) parent::__construct(); 

which should be:

if (method_exists(get_parent_class($this), '__construct')) parent::__construct(); 

you alternatively write following:

method_exists(get_parent_class($this), '__construct') && parent::__construct(); 

this ever more concise (as 1 can php) , little bit less noise.

the reason this?

the first thing on line (far left) "method_exists" quick indicator care about. end of line (far left, besides semicolon), end result. also, && indicator there should ever 1 statement following it. if changes, easy see entire statement needs re-factored rather attaching statement after parent::__construct();

edit: further information regarding autoloading:

create autoload.php.dist file in project root (replace '/path/to/your/php5/lib/dir'):

<?php /* autoload.php.dist */  $includepaths = array( realpath('/path/to/your/php5/lib/dir'), get_include_path()); set_include_path( join(\path_separator, $includepaths) );  spl_autoload_register(function($classname) {     $classname = ltrim($classname, '\\');     $filename  = '';     $namespace = '';     if ($lastnspos = strripos($classname, '\\')) {         $namespace = substr($classname, 0, $lastnspos);         $classname = substr($classname, $lastnspos + 1);         $filename  = str_replace('\\', directory_separator, $namespace) . directory_separator;     }     $filename .= str_replace('_', directory_separator, $classname) . '.php';     require $filename; }, true, true); 

create bootstrap can re-used (bootstrap.php)

<?php /* bootstrap.php */ if (file_exists($file = __dir__.'/autoload.php')) {     require_once $file; } elseif (file_exists($file = __dir__.'/autoload.php.dist')) {     require_once $file; } 

create phpunit.xml.dist file utilizes bootstrap.php (replace './your/test/directory/here'):

<?xml version="1.0" encoding="utf-8"?> <!-- phpunit.xml.dist --> <phpunit backupglobals="false"          backupstaticattributes="false"          colors="false"          converterrorstoexceptions="true"          convertnoticestoexceptions="true"          convertwarningstoexceptions="true"          processisolation="false"          stoponfailure="false"          syntaxcheck="false"          bootstrap="./bootstrap.php" >     <testsuites>         <testsuite name="test suite">             <directory>./your/test/directory/here</directory>         </testsuite>     </testsuites> </phpunit> 

now can run phpunit root project directory.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -