定制 cosma/phest 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cosma/phest

Composer 安装命令:

composer require cosma/phest

包简介

Phest = Phalcon + Test

README 文档

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.

cosma/phest 适用场景与选型建议

cosma/phest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 09 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「unit test」 「phpunit」 「functional test」 「phalcon」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cosma/phest 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cosma/phest 我们能提供哪些服务?
定制开发 / 二次开发

基于 cosma/phest 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 21
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-09