Categories
PHPUnit

What do you verify before deploying your code ?

vendor/bin/phpcs --standard=PSR2 --colors -p src/ && vendor/bin/phpunit --stop-on-failure  

PSR-2 Code Styling

This command returns all PSR-2 (code styling errors)

vendor/bin/phpcs --standard=PSR2 ~/directory_to_scan

vendor/bin/phpcs –standard=PSR2 –colors -pv src/ApiBundle

with more information

This command fix all fixable PSR-2 (code styling errors) issues

vendor/bin/phpcbf --standard=PSR2 --report=json ~/directory_to_scan

Run the Functional and Unit Tests

vendor/bin/phpunit –stop-on-failure

Categories
PHPUnit

Phpunit stops unexpectedly without any message or error

My solution for that issue is to first identify which phpUnit test is failing this way

Then set a try-catch methodology in your code to analyze the error

From experience it is likely due to php or phpUnit running out of memory probably cause by a recursive pattern in the code

Encapsulate the code with a try catch see below

try {

      [Code to encapsulate]

} catch (\Throwable $e) {

var_dump($e->getCode());
var_dump($e->getMessage());

}

An \Exception is not likely to be caught in this type of error

Categories
BEST PHP CODE PHP PHPUnit

Useful catch (\Throwable $e)

catch (\Throwable $e) turned out to be very useful while coding in PHP.

When Exception were caught in specific instances, catch throwable would catch anything that would impede the code to run

Example below in a phpUnit scenario :

        try {
            $r = $this->checkKeys($data);
        } catch (\Throwable $e) {
            $this->assertEquals($e->getCode(), 404);
        }
Categories
PHPUnit

phpUnit Debug Technics Guzzle

$body = json_decode($response->getBody());

 fwrite(STDERR, json_encode($json));
 fwrite(STDERR, $body->message);
 public function testSuccesssfulJobCancel()
{

    $this->postOrderSuccesful();

    $headers = [
        'Authorization' => QATemplate::$deleteKey,
    ];
    $url = '/v1/jobId/M111111123-001';

    $response = QATemplate::$http->request(
        'DELETE',
        $url,
        [
            'verify' =>false,
            'http_errors' => false,
            'headers' =>$headers
        ]
    );

    $body = json_decode($response->getBody());

    fwrite(STDERR, json_encode($json));
    fwrite(STDERR, $body->message);


    $this->assertEquals(200, $response->getStatusCode());
    $this->assertContains("Success Job Deleted", $body->message);
}

Categories
PHP PHPUnit Unix

Developer Helper – Command Cine

PSR-2 Code Styling

vendor/bin/phpcs --standard=PSR2  app src
vendor/bin/phpcbf --standard=PSR2  app src
vendor/bin/phpcbf --standard=PSR2 --report=json app src

phpunit

vendor/bin/phpunit
vendor/bin/phpunit --log-junit web/phpunit/phpunit.xml --coverage-clover web/phpunit/coverage.xml --coverage-html web/phpunit/

Swagger Generate swagger documentation Swagger endpoint

vendor/bin/openapi -o "web/swagger.json" app/ src/ web/
Categories
BEST PHP CODE PHPUnit

PHPUnit (truncated…) – SOLUTION

Hi Team,

If you experienced the difficult task to debug through PhpUnit find here a global solution !

The exception caught is related to the guzzle version

The solution is a try-catch around your phpunit test and expose the GuzzleException $e

 $client = new GuzzleHttp\Client(['base_uri' => 'https://foo.com/api/']);

try {

       $response = $client->request('GET','/v1/testYourEndpoint');

} catch (\GuzzleHttp\Exception\ClientErrorResponseException  $e) {
    var_dump($e->getResponse()->getBody()->getContents());

} catch (\GuzzleHttp\Exception\RequestException $e) {
    var_dump($e->getResponse()->getBody()->getContents());

} catch (\GuzzleHttp\Exception\ClientException  $e) {
    var_dump($e->getResponse()->getBody()->getContents());
}

Enjoy !
David Raleche