Testing is easy. Or the story of one bike

One day I needed to test the server's response and decided that using heavy Swiss knives like PhpUnit was cumbersome. Everything was complicated by the fact that the infrastructure was split into many web microservices, which in turn worked on different backends (PHP, NodeJS, Python, GO). So I decided to sketch out a simple class, which surprisingly turned out to be a very handy tool for system acceptance testing.

- We need to be sure that with every push our entire infrastructure does not break, can you do it? -Easy!


Meet eXo-Test. A small php-cli class with which to run tests is really easy.



image



Installation
you can just read it on GitHub

or here



composer require overvoidjs/exotest:dev-master


<?php 
include_once 'vendor/autoload.php';




Let's say you need to check the availability of pages and the validity of the content on it, for example, make sure that there are products in the catalog. Nothing could be easier:




<?php 
include_once 'vendor/autoload.php';

$i = new Exo;

$url = 'http://localhost:7888/catalog/instrumentyi/';

$i->is_ok($url, '<div class="product-card-name">');


Now just run the script you created from the console:



php test.php


You will receive a message:



1) Success:



image



2) The response status is 200 (Ok), but the content was not found



image



3) The response status is not 200 The response



image



statuses are good, but now we are faced with the task of checking the functionality of the system. Again - just add water!



Here we check the addition of an item to the cart:




$payload = [
  'product_id'=>'3401',
  'count'=>'1'
];

@$cart_id = $i->post_it('http://localhost:7888/api/cart/add',$payload);

if(is_int($cart_id)){
    echo "API    ...  Ok \n";
} else {
   echo "API    ...  FAIL \n";
}


Highlighting answers
Green:




echo "API    ... \033[32m Ok\033[0m \n";


Red:


echo "API    ... \033[01;31m FAIL\033[0m \n";




Great, the functionality is almost checked. Nearly? Well, almost any system will have an API that works with files uploaded by the client. We can test this too:




//  CURL -      
$i = new Exo;

$url = 'https://site.com/api/sameimg';

$payload = [
  'data'=>'data'
];

$post_file_name = 'new_img';
$post_file_path = './new_img.jpg';

$test = $i->post_it_file($url,$payload,$post_file_name,$post_file_path);


Here, the payload + file will be sent to the specified address by POST request, just as if it were added to
 input type="file" name="new_img" 




That's all for now. I hope this tool will be useful, first of all, for those who still do not test their code.



All Articles