0

so firstly, I am very new to coding. This question pertains to unit testing using php. Should be obvious, I know, but my question is, in order to actually use the unit tests i will eventually create, will I have to also make up some code apply the tests? All of the tutorials explain how to configure the IDE i am using (phpstorm) but do not give any sample code to actually perform the tests. Is there a feature built into phpstorm which allows you to try the unit tests you create? If i do in fact require sample code, what kind of code should i use and where could i get it from? Any help is appreciated, thank you.

1 Answer 1

2

Before you start testing, need to take some steps. It`s worked for ubuntu 18.04

  • install php globally

    ~$ sudo add-apt-repository -y ppa:ondrej/php \  
       && sudo apt-get update \
       && sudo apt-get install -y php \
       && sudo apt-get install -y php-xml \
       && sudo apt-get install -y php-mbstring
    

    for checking installation success run ~$ php -v

  • install composer globally

    ~$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php'); " \  
     . "if (hash_file('sha384', 'composer-setup.php') === " \
     . "e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') " \
     . "{ echo 'Installer verified'; } " \
     . "else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
     && php composer-setup.php \
     && php -r "unlink('composer-setup.php');" \
     && sudo mv composer.phar /bin/composer
    

    for checking installation success run ~$ composer -V (the relevant hash is always on composer official documentation)

  • install phpunit - unit testing framework for php

    project_folder$ composer require --dev phpunit/phpunit

    this command will create composer.json and composer.lock files, also vendor directory at the root of your project.


Follow to some agreements I created two files - Math.php and MathTest.php and added autoload section to composer.json

Math.php

<?php  

namespace Source;  

class Math  
{  
     public function square($number)
     {
         return $number * $number;
     }
}  

MathTest.php

<?php  

use PHPUnit\Framework\TestCase;  
use Source\Math;  

class MathTest extends TestCase  
{  
     public $math;

     public function setUp(): void
     {
         $this->math = new Math();
     }

     public function testSquareEquals()
     {
         $this->assertEquals(4, $this->math->square(2));
         $this->assertEquals(16, $this->math->square(4));
     }

     public function testSquareNotEquals()
     {
         $this->assertNotEquals(3, $this->math->square(2));
         $this->assertNotEquals(9, $this->math->square(4));
     }
}  

composer.json

{  
    "autoload": {  
        "psr-4": {  
            "Source\\": "src"  
        }  
    },  
    "require-dev": {  
        "phpunit/phpunit": "^9.0"  
    }  
}

the resulting project structure

project_folder  
├─── src  
│    └── Math.php  
├─── tests  
│    └── MathTest.php  
├── vendor  
├── composer.json  
└── composer.lock  

to create autoload files:

project_folder$ composer dump-autoload

It`s all. Now you can start testing by running the command

project_folder$ vendor/bin/phpunit tests  
PHPUnit 9.0.1 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 28 ms, Memory: 4.00 MB

OK (2 tests, 4 assertions)

where tests is the directory with your tests.

More examples in phpunit official documentation

Sign up to request clarification or add additional context in comments.

3 Comments

Great tutorial. We might note that these describe ONE way of doing things, but neither composer nor a globally installed php is strictly required.
Thank you @amlagoda I will read up on what these things mean because I honestly have no idea what these things are, or what the project_folder$ vendor/bin/phpunit tests command is actually testing for. I really appreciate you taking the time to post this tutorial, thanks again.
@AnthonyLopez Happy to help. Possible I can supplement this answer if there are more specific questions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.