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
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-mbstringfor checking installation success run
~$ php -vinstall 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/composerfor 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/phpunitthis command will create
composer.jsonandcomposer.lockfiles, alsovendordirectory 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