phpunit - How do I load the file I need? PHP -
while might basic question, 1 assume use require_once
, more complicated - @ least me.
the issue having have project structure package structure in java. how ever use global name space. project structure looks like:
projectname/ moduleone/ class.php composer.json tests/ phpunit.xml bootstrap.php class_test.php
in above example class.php
instantiated such: projectname\moduleone\class();
now issue how set our tests directory, because class_test.php
use class.php
, other classes 1 depends on, need way allow me instantiate in test, class in question. when ever attempt error saying cant find class in question trying instantiate.
while might unusual way of doing things, project such each module can exported out using composer , auto loaded project using namespace have chosen, in case projectname
.
the issue seem having need each class outside tests/
directory auto loaded , ready go, such if class.php
extends other class when class.php
called know, "ok need go here see if other class exists well."
now each module have composer file , inside have like:
"autoload": { "psr-0": { "projectname\\moduleone\\": "" } },
so use how in bootstrap.php auto load whole module testing purposes?
i wouldn't try reinvent wheel , go package structure suggested composer.
a quick google search gave me link blog post: how create psr-4 php package. pretty describes, how file structure of composer package should , how set , organize tests of package.
hope helps.
edit:
assuming have structure suggested in post above, need tell phpunit find autoload file created composer. if need more complex bootstrapping, can following. create file tests/bootstrap.php
, add following content it:
require_once 'path/to/composer/autoload.php'; // set stubs, mocks etc.
in phpunit.xml file need point phpunit correct bootstrap file, e.g.:
<?xml version="1.0" encoding="utf-8"?> <phpunit backupglobals="false" backupstaticattributes="false" bootstrap="tests/bootstrap.php" colors="true" converterrorstoexceptions="true" convertnoticestoexceptions="true" convertwarningstoexceptions="true" processisolation="false" stoponfailure="false" syntaxcheck="false"> <testsuites> <testsuite name="nacho test suite"> <directory suffix=".php">./tests/</directory> </testsuite> </testsuites> </phpunit>
alternatively can run phpunit --bootstrap
flag, e.g.:
vendor/bin/phpunit --bootstrap path/to/bootstrap.php
Comments
Post a Comment