📜 ⬆️ ⬇️

A simple tool for testing PHP applications

Who is this article for?


Most likely for those who have not yet begun to test, but has such a desire. Experienced developers in this case will not be able to surprise, but those who have not yet moved to the side of the world, I will try to push on this step.

Prehistory


I decided to deal with automatic testing. Previously, this was not necessary, and even then it was not particularly necessary. But there was free time, which I decided to spend with benefit for the future.
After reading the theory, I began to look for a tool for this. Predictably PhpUnit appeared first on the horizon. But he seemed somehow cumbersome, or something.
Codeception seemed more convenient - different types of tests, expressive syntax. But, after looking at the dependencies, I realized that I did not need so much.
Moving towards simplicity, I found atoum , and then generally a cool thing called Testify.php . It was then that I thought that I had finally found what I needed.

But I was happy early. Testify.php did not match when writing the first test. A caching class was tested, which, depending on whether debugging is enabled or not, could process or ignore calls. Since the debugging mode assumed the presence of a DEBUG constant with a value of true / false , it will not be possible to redefine it in one process.

Requirements


After these searches, I realized that I needed a simple tool that:

Implementation


A small script was written that opened the tests for http, collected the results, and issued it in presentable HTML. In order to make the testing process similar for the console version, it was decided to use the built-in PHP 5.4+ web server. It starts like this:
')
// $web_server_pid = exec("php -S localhost:$this->port >/dev/null 2>/dev/null & echo $!"); // //... // exec("kill $web_server_pid"); 

And it worked as it should. So, I did not find a similar tool - I decided to arrange it in the form of an independent composer package, added interactivity (the progress of test sets execution is displayed in real time both in HTML and in the console version).

Running tests:

 // test.php require __DIR__.'/vendor/autoload.php'; (new \nazarpc\CSTester(__DIR__.'/tests'))->run(); 

Now this file can be opened either in the browser or in the console.

 php test.php 

The tests themselves are not much harder:

 return 5 != 3 ? 0 : 'Strange PHP'; 

That is, each individual test is a file that returns 0 after a successful test or an error text , if any.

Example of the result of the console version:



It is possible to execute general commands both before all test suites, and before all tests of one test kit.

I am sure that it can be done even better, therefore I am waiting for constructive criticism and suggestions.
No dependencies, bare PHP 5.4+ will do.

GitHub repository: github.com/nazar-pc/CSTester
Composer package: packagist.org/packages/nazar-pc/cstester

Source: https://habr.com/ru/post/188342/


All Articles