There are rules for startup ( http://www.php-fig.org/psr/psr-0/ ) and testing PHPunit ( https://phpunit.de/manual/current/en/organizing-tests.html ). Gradually, they are easy to implement, but combining them is not so.
I also read the article PHPUnit best practices for organizing tests , but applying the answer is not obvious to me. Having written my own project, I had several problems that probably have the same origin in the organization of the code. I wish I had recipes for a simple simple example. So I am making my question pretty big.
Project example
I use NetBeans IDE 7.4 because it supports PHPUnit, SVN, and Doxygen (but I lack experience using this IDE).
Project properties
- Project folder: C: \ xampp \ htdocs \ myapps \ PhpProject1
- Source folder: C: \ xampp \ htdocs \ myapps \ PhpProject1
- Test folder: C: \ xampp \ htdocs \ myapps \ PhpProject1 \ tests
There is a first problem. NetBeans blocks the mirror src / tests structure from the very beginning. There is no Browse button for the source folder. The Test folder should be different from the Source folder.
I am using PHPUnit and skelgen from PHAR files located outside the project. (Tools-> Settings-> PHP / PHPUnit)
- PHPUnit Script: C: \ xampp \ phar \ phpunit.phar
- Skeleton Generator Script: C: \ xampp \ phar \ phpunit-skelgen-1.2.1.phar
I have the following file structure in the project directory
index.php
src/
Client.php
srcAutoload.php
MyPack/
Foo/
Bar.php
Quu/
Baz.php
Where is srcnot a namespace since
- file
src/MyPack/Foo/Bar.php MyPack\Foo\Bar - file
src/MyPack/Quu/Baz.php MyPack\Qoo\Baz src/Client.php Client
srcAutoload.php PSR-0 https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md. , , require(), PSR-0 src/
$fileName = 'src' . DIRECTORY_SEPARATOR . $fileName;
Tools/Create Tests ( ) Bar Baz,
index.php
src/
Client.php
srcAutoload.php
MyPack/
Foo/
Bar.php
Quu/
Baz.php
tests/
src/
BarTest.php
BazTest.php
MyPack/
Foo/
Quu/
. , , , .
Fatal error: Class 'MyPack\Foo\Bar' not found in C:\xampp\htdocs\myapps\PhpProject1\tests\src\BarTest.php on line 20
, , . . PSR-0, tests/srcAutoloadFromTests.php
$fromTests = '..' . DIRECTORY_SEPARATOR;
$fileName = $fromTests . 'src' . DIRECTORY_SEPARATOR . $fileName;
if (is_file($fileName)) {
require $fileName;
}
Use Bootstrap (Project Properties β Testing/PHPUnit). tests/bootstrap.php :
require_once 'srcAutoloadFromTests.php';
PHPUnit , .
src/ tests/ ?
, :
index.php
src/
Client.php
srcAutoload.php
MyPack/
Foo/
Bar.php
Quu/
Baz.php
tests/
bootstrap.php
srcAutoloadFromTests.php
MyPack/
Foo/
BarTest.php
Quu/
BazTest.php
https://phpunit.de/manual/current/en/organizing-tests.html PHPUnit . . srcAutoloadFromTests.php , .
Fatal error: Class 'MyPack\Foo\Bar' not found in C:\xampp\htdocs\myapps\PhpProject1_1\tests\MyPack\Foo\BarTest.php on line 20
NetBeans , . , .
, . - .
, . ?
src/ tests/ vendor/
. , PSR-0 . PSR-4 src/ tests/ - .
vendor/. src/ tests/ .
src/
β¦ my code
tests/
β¦ my tests
vendor/
β¦ third-party code
PSR-0.
PHPUnit NetBeans
BazTest BarTest IDE NetBeans PHPUnit
\PHPUnit_Framework_TestCase
namespace MyPack\Foo;
class BarTest extends \PHPUnit_Framework_TestCase { }
, PHPUnit PHAR?
, , , . ,