cosma/phest

Phest = Phalcon + Test

0.0.3 2016-09-15 14:55 UTC

This package is not auto-updated.

Last update: 2024-11-09 20:50:42 UTC


README

Circle CI

Phalcon + Test = Phest. A test library for Phalcon Framework.

Table of Contents

Installation

1. Add Phest to composer.json

    $   php composer.phar require cosma/phest '0.0.*'

Follow the 'dev-master' branch for latest dev version. I recommend to use more stable version tags if available.

2. Add a bootstrap.php file to Phalcon project tests directory

# /path/to/phalcon/project/tests/bootstrap.php

 /**
 * Define TEST_PATH to point to path/to/phalcon/project/tests/
 */
define('TEST_PATH', __DIR__ );

/**
 * Require Phest environment.php file
 */
require_once '/path/to/vendor/cosma/phest/src/environment.php';

/**
 * Get your application from your phalcon project
 */    
/** @var \Phalcon\Mvc\Micro|\Phalcon\Mvc\Application $app */
$app = require_once __DIR__ . '/../src/init.php';


 /**
 * Require Phest library bootstrap.php file
 */
require_once '/path/to/vendor/cosma/phest/src/bootstrap.php';

An example for bootstrap.php

3. Add to phpunit configuration XML the bootstrap file

<!--  /path/to/phalcon/project/tests/phpunit.xml -->

<phpunit  .....
         bootstrap="path/tophalcon/project/bootstrap.php"
         .....
        >
    ........
</phpunit>

An example for phpunit.xml

Optionally, you can add a config.php that is merged with you Phalcon project configs

# /path/to/phalcon/project/tests/config.php

return new \Phalcon\Config([
    'someConfigVariable' => 'some value',
]);

An example for config.php

Dependencies

This test library is intended for projects using Phalcon Framework version version 2.9. Therefore, PHP extension 2.0.13 must be installed. Phalcon Extension

Test Cases

Supports the following Test Cases:

Unit Test Case

This case is used for unit testing is an extension of PHPUnit_Framework_TestCase:

use Cosma\Phest\TestCase\UnitTestCase;
 
class SomeVerySimpleUnitTest extends UnitTestCase
{
    public function testSomething()
    {
        $additionClass = new AdditionCLass();
        
        $this->assertEquals(3, $additionClass->calculate(1, 2)); 
    }
}

Web Test Case

This case is used for functional and controller tests and has the following methods:

  • mockService ($serviceName, $mock)
  • sendRequest ($url = '', $requestMethod = 'GET', $parameters = [], $headers = [])
use Cosma\Phest\TestCase\WebTestCase;

class SomeWebFunctionalTest extends WebTestCase
{
    public function setUp()
    {
        /**
        * Required call
        */
        parent::setUp();
        
        $db = $this->getMockBuilder('Phalcon\Db\Adapter\Pdo\Mysql')
            ->disableOriginalConstructor()
            ->setMethods(['query'])
            ->getMock();
        $this->mockService('db', $db);
        
    }
    
    public function testSomething()
    {
        /** @var \Phalcon\Http\Response $response */
        $response = $this->sendRequest(
            '/test_endpoint', 'POST', ['test_var' => 'value'], ['Header1' => 223456789, 'Header2' => 'value2']);

        $this->assertInstanceOf('Phalcon\Http\Response', $response);

        $this->assertEquals('200 OK', $response->getStatusCode());
        $this->assertEquals('value', $response->getContent());
        
    }
    
    public function testGetHealthCheck()
    {
        /** @var \Phalcon\Http\Response $response */
        $response = $this->sendRequest(
            '/healthCheck', 'GET', [], []);

        $this->assertInstanceOf('Phalcon\Http\Response', $response);

        $this->assertEquals('200 OK', $response->getStatusCode());
    }
}

Retry Tests

Use the @retry annotation for a Class or Method to retry tests in case of failure. Method annotations are overwriting Class annotation.

use Cosma\Phest\TestCase\UnitTestCase;

/**
* Will retry 10 times all the Class tests that are failing
*
* @retry 10 
*/ 
class SomeVerySimpleUnitTest extends UnitTestCase
{
    /**
    * Will retry 10 times this test if is failing because of the class annotation from above
    */
    public function testFirst()
    {
        // ...
    }
    
    /**
    * Will retry 4 times this test if is failing because of the method annotation from below
    *
    * @retry 4 
    */
    public function testSecond()
    {
        // ...
    }
}

Mockery

Mockery is a simple yet flexible PHP mock object framework for use in unit testing

use Cosma\Phest\TestCase\UnitTestCase;
       
class SomeUnitTest extends UnitTestCase
{
    public function testGetsAverageTemperatureFromThreeServiceReadings()
    {
        $service = \Mockery::mock('service');
        $service->shouldReceive('readTemp')->times(3)->andReturn(10, 12, 14);

        $temperature = new Temperature($service);

        $this->assertEquals(12, $temperature->average());
    }
}    

Run Tests

vendor/bin/phpunit -c phpunit.xml --coverage-text --coverage-html=tests/coverage tests

License

The bundle is licensed under MIT.