mercredi 30 novembre 2016

PHPUnit in a legacy enviroment

I'm starting to setup PHPUnit (v4.8) to be used in my 'legacy' code (it's not so legacy, but it have bad programming pratices).

The structure of my folders is as follows

/home
  -phpunit.xml
  /folder1
  /folder2
  /folder3
  /vendor
  /tests
   -Test1.php
  /includes
   -functions.php
   /libs
    -User.php
    -TableClass.php
    ....

functions.php

<?php //require_once $_SERVER['DOCUMENT_ROOT'] . "/home/vendor/autoload.php" ; require_once $_SERVER['DOCUMENT_ROOT'] . "/home/includes/libs/User.php" ; ?>

I have commented that line, because I think composer automatically loads it. Question 1, Am I Rigth? (because phpunit get automatically recognized inside my Test class...)

Test1.php

<?php

class Test1 extends PHPUnit_Framework_TestCase
{
  public function testSomething()
  {
    // $something = getColNameByStatusId(1);
    $this->assertEquals(1,2);
  }
}
?>

phpunit.xml

<phpunit bootstrap="includes/functions.php" colors="true">
    <testsuite name="Test1" >
        <directory>./tests</directory>
    </testsuite>
</phpunit>

Then I Execute phpunit in command line

My functions.php works fine in my code, of course with no composer integration, but when It's loaded with phpunit it 'breaks', I get the following error:

Warning: require_once(/home/includes/libs/table_classes/User.php): failed to open stream: No such file or directory in C:\wamp\www\home\includes\functions.php on line 18

I think I'm missing the 'loading' stuff for phpunit. My code doesn't use namespaces and PSR-0 neither PSR-4.

Question 2- How to properly load files in this case?

My goal is to load functions.php then it will load all other 'table' classes for doing my tests

Aucun commentaire:

Enregistrer un commentaire