This is kinda a confusing subject to learn for me, especially when I am not very familiar with docker yet, nor with PHPUnit. Here is my note as I go along the study process.
Source Link:
Official documentation for PhpUnit
https://phpunit.de/getting-started/phpunit-6.html
Command to install PHPUnit:
composer require --dev phpunit/phpunit ^6
Source Link:
https://phpunit.de/manual/6.5/en/organizing-tests.html
Composing a Test Suite Using XML Configuration:
<phpunit bootstrap="src/autoload.php"> <testsuites> <testsuite name="money"> <file>tests/IntlFormatterTest.php</file> <file>tests/MoneyTest.php</file> <file>tests/CurrencyTest.php</file> <exclude>some file or directory name</exclude> </testsuite> </testsuites> </phpunit>
Command to run PHPunit test:
./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/testExample ./vendor/bin/phpunit --bootstrap vendor/autoload.php --testsuite msg
Yet, I am still a bit confused about this whole thing.
Run commands through Docker:
Source link:
https://www.shiphp.com/blog/2017/phpunit-docker
//Install PhpUnit: docker run --rm -v $(pwd):/app composer/composer:latest require --dev phpunit/phpunit ^6.0 //Run test suite: docker run -v $(pwd):/app --rm phpunit/phpunit:latest --bootstrap vendor/autoload.php --testsuite msg
* Note:
(above flags orders don’t matter)
– -rm is a flag of “clean up”.
Access to mysql from a container:
docker exec -t <container_name> mysql -u<username> -p docker exec -it dockie_db_1 mysql -uroot -p
PHPUnit mock usage:
use PHPUnit\Framework\TestCase; function quickTest($obj,$value=665){ return $obj->increaseOne($value); } class SomeClass{ public function increaseOne($value){ return $value + 1; } } class testExample extends TestCase { public function testSimple() { $this->assertEquals(2,2); } public function testMocks() { $someClass = $this->createMock('SomeClass'); $someClass->expects($this->any()) ->method('increaseOne') ->with(665) ->will($this->returnValue(666)); $b = quickTest($someClass); $this->assertEquals($b,666); } }
More info about mock usage:
https://codeutopia.net/blog/2009/06/26/unit-testing-4-mock-objects-and-testing-code-which-uses-the-database/
https://stackoverflow.com/questions/5837991/in-phpunit-how-do-i-indicate-different-with-on-successive-calls-to-a-mocked-m