Dieses Blog durchsuchen

Sonntag, 11. September 2016

unittesting: install and use phpunit from scratch.

If you start a new php project, you will find it hard to implement phpunit in your project and in your local environment, because after a while you simply forget how the setup is working.

So let's repeat the installation and configuration of composer and phpunit.

We want:
  • running composer
  • running phpunit
  • a sample project skelleton
  • a sample class
  • a simple test for a class
We need some things installed on the local machine. We are webprogrammers who love unix and linux. So we are using a ubuntu xenial 16.04

     

    Install composer 

    With composer the hard task of outloading your sourcefiles is a little bit simpler. Because it makes some work for us, we really dont want to do, like setup autoloading manually. PHP isnt very comfortable here. So we have to use a tool.

    Composer install
    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php composer-setup.php
    php -r "unlink('composer-setup.php');"
    sudo mv composer.phar /usr/local/bin/
    sed -i "$ aalias composer='php /usr/local/bin/composer.phar' " ~/.bashrc . ~/.profile
    source ~/.bashrc
    

    This will install composer and add it to your console, so that you can simply type "composer"
     

    Install phpunit

    To make sure, we have the lastest stable version installed we use composer to istall phpunit.


    phpunit install
    composer  global require "phpunit/phpunit"
    add it to the console
    sudo ln -s  ~/.composer/vendor/phpunit/phpunit/phpunit   /usr/bin/ 
    test it
    composer install 
    phpunit --version
     

    Configure psr-4 autoloading

    We want to use the latest standard to load our classes with namespacing and psr-4.

    To do that, we have to tell composer to use psr-4

    open composer.json and add
    "autoload": {
        "psr-4": {"TestPhpProject\\": "src/"}
    },

    This will search for all Classes with Namespace "TestPhpProject" in a folder "src"

    Now we have to run a scanning for all classes in class src initialy to create the psr-4 classmap

    recreate psr-4 autoloading classmap
    composer dump-autoload

    Create a testproject

    Now that we have composer ready to load our classes automaticly, we have to setup our corresponding  projectstructure:

    Create folderstructure like that
    TestPhpProject
    |_src
    |_test

    Create a phpunit test class under "test" named MathTest.php with this content:
    <?php
    
    use TestPhpProject\Math;
    
    class MathTest extends \PHPUnit\Framework\TestCase
    {
        // test the talk method
        public function testAdd() {
            // make an instance of the user
            $math = new Math();
    
            // use assertEquals to ensure the greeting is what you
            $expected = 1;
            $actual = $math->add(1,2);
            $this->assertEquals($expected, $actual);
        }
    }

    We always create our test before we implement the class.

    This simple testcase uses the TestPhpProject\Math class , we created before.

    The class MathTest extends the UnitTest Framework class "TestCase" and implements a method testAdd(). This method refences a Mathobject and comares trhe result from the method add() with an expected result 1. This will fail.

    If your first test fails, ist greate, because it must fail for the first time. All things fail first!

    Learn to accept! It makes things very easy.



    Create a simple class under "src" named Math.php with this content:
    <?php
    
    namespace TestPhpProject;
    
    Class Math
    {
        public function __construct()
        {
            $this->_test = '1';
        }
    
        public function add($x, $y)
        {
            return $x+$y;
        }
    }

    As you can see we have defined a namespace TestPhpProject, wich corresponds to our psr-4 autoloader we defined before. It has a constructor which sets the property test to 1 and a function add, which simply add 2 sumands and returns the result.

    Pretty simple.


    Tell phpunit to use our test directory and our composer autoloader to load our testsfiles automaticly.
    
    
    Add a file phpunit.xml to your projectroot with this content
    <?xml version="1.0" encoding="utf-8" ?><phpunit bootstrap="./vendor/autoload.php"> 
     <testsuites> 
       <testsuite name="The project's test suite"> 
         <directory>./tests</directory> 
       </testsuite> 
     </testsuites>
    </phpunit>

    Run our first test

    phpunit 
    
    




    Keine Kommentare:

    Kommentar veröffentlichen