How to organize PHPUnit tests and startup using NetBeans IDE

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

/// after PSR-0 implementation code 
/// I have PSR-0 $fileName

/* Escaping from tests subfolder by "../" prefix*/ 
$fromTests = '..' . DIRECTORY_SEPARATOR;

/* Prefixing full filename with "src/" */ 
$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?

, , , . ,

+4
2

, .:) , .

  • PHPUnit Skelgen

    True. ( , ) skelgen.

    * Test .

    : PHPUnit, Netbeans Symfony2

  • , :

    src/
       … my code
    tests/
       … my tests 
    vendor/
       … third-party code
    
  • :

    ( ): - src. , src. src .

    : src src test. src tests .

    ( ): , Composer. Composer require-dev. , Composer ( src boostrap). , , , .

  • PHPUnit NetBeans

    , PHAR , Netbeans PHPUnit.

    1. PHAR.

    2. PHP Netbeans. , . , , - .

    3: Composer: phpunit require-dev . Netbeans, .

+2

. . , .

Task

PHPUnit PHP IDE NetBeans 7.4.

composer.json
index.php
src/
   Client.php
   MyPack/
      Foo/
         Bar.php

  • src/Client.php ,
  • src/MyPack/Foo/Bar.php , MyPack\Foo
  • composer.json , PHPUnit, PHPUnit , src/ tests/

composer.json

{
    "name": "vendor/php-project-with-tests",
    "autoload": {
        "psr-4": { 
            "": ["src/", "tests/"]
        }
    },
    "require": {},
    "require-dev": {
        "phpunit/phpunit": "4.7.7",
        "phpunit/phpunit-skeleton-generator": "2.0.1"
    }
}

1. Set Tools

NetBeans, PHPUnit. PHAR, https://phar.phpunit.de/

  • β†’ ,
  • PHP PHPUnit
  • :
    • PHPUnit Script: MY_PATH\phpunit-4.7.7.phar
    • - Script: MY_PATH\phpunit-skelgen-1.2.1.phar

2.

  • , ""
  • :
    • tests

3.

  • , ""
  • :
    • : PHPUnit

4.

  • (, Bar.php), "" β†’ " "

5.

  • , β†’ (dev)

:  - , src  - PHPUnit-

6.

Bootstrap . /autoload.php .

  • , ""
  • : /PHPUnit
    • check Bootstrap
    • Bootstrap: PROJECT_MAIN_DIR\vendor\autoload.php

enter image description here

enter image description here

.

PHPUnit 4.8

PHPUnit 4.8.0 4.8.6 . unrecognized option --run

PHPUnit 4.7.7

PHPUnit Skeleton Generator 2.0.1

PHPUnit Skeleton Generator 2.0.1 . [InvalidArgumentException] Commans "MyPack\Foo\Bar"

PHPUnit Skeleton Generator 1.2.1

+2

All Articles