cosma/phest

Phest = Phalcon + Test

0.0.3 2016-09-15 14:55 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:03:59 UTC


README

Circle CI

Phalcon + Test = Phest. 是 Phalcon 框架的测试库。 Phalcon 框架

目录

安装

1. 将 Phest 添加到 composer.json

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

跟踪 'dev-master' 分支以获取最新开发版本。如果可能,请使用更稳定的版本标签。

2. 在 Phalcon 项目测试目录中添加一个 bootstrap.php 文件

# /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';

例如 bootstrap.php

3. 将启动文件添加到 phpunit 配置 XML 中

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

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

例如 phpunit.xml

可选,您可以添加一个 config.php 文件,该文件将与您的 Phalcon 项目配置合并

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

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

例如 config.php

依赖项

此测试库旨在用于使用 Phalcon 框架版本 2.9 的项目。因此,必须安装 PHP 扩展 2.0.13。 Phalcon 扩展

测试用例

支持以下测试用例

单元测试用例

此用例用于单元测试,是 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 测试用例

此用例用于功能测试和控制台测试,并具有以下方法

  • 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 注解为类或方法重试测试,如果失败。方法注释将覆盖类注释。

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 是一个简单而灵活的 PHP 模拟对象框架,用于单元测试

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());
    }
}    

运行测试

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

许可协议

该软件包采用 MIT 许可。