承接 friendsofcake/cakephp-test-utilities 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

friendsofcake/cakephp-test-utilities

最新稳定版本:3.0.0

Composer 安装命令:

composer require friendsofcake/cakephp-test-utilities

包简介

Package with support traits to ease unit testing.

README 文档

README

This package contains support traits to ease unit testing.

Installing via composer

You should install this package into your project using composer. To do so you can run the following command:

composer require friendsofcake/cakephp-test-utilities

Traits

At this point there are two traits:

  1. AccessibilityHelperTrait : Gain access protected properties and methods.
  2. CompareTrait : Assert methods, comparing to files for: HTML, JSON, XML

AccessibilityHelperTrait

This trait gains you access to protected properties and methods. You don't need of a new class with pass-through methods. It uses reflection to achieve this.

Setup

Add the trait at the top of your test case:

use \FriendsOfCake\TestUtilities\AccessibilityHelperTrait;

Now that you have the trait you need to set which object you want to access. You can do this globally for the entire test in setUp() or in your test methods:

$object = new ObjectIAmGoingToTest();
$this->setReflectionClassInstance($object);
$this->defaultReflectionTarget = $object; // (optional)

Protected properties

You can get and set the protected properties:

$data = 'FriendsOfCake';
$this->setProtectedProperty('_myProperty', $data, $object);

$expected = $data;
$actual = $this->getProtectedProperty('_myProperty', $object);
$this->assertEquals($expected, $actual);

Protected methods

You can directly call protected methods:

$parameters = [$argument1, $argument2];

$expected = $expectedReturnValue;
$actual = $this->callProtectedMethod('_myMethod', $parameters, $object);
$this->assertEquals($expected, $actual);

CompareTrait

This trait helps with comparing test results as string

Setup

Add the trait at the top of your test case and define the _compareBasePath property so the trait knows where to look for comparison files:

...
use \FriendsOfCake\TestUtilities\CompareTrait;

class MyTest extends TestCase
{
    use CompareTrait;

    public function setUp(): void
    {
        parent::setUp();

        $this->_compareBasePath = 'comparisons/MyTest/';
    }
}

Usage

Each of the methods acts similar to the core assertSameAsFile method:

public function testExample()
{
    $html = '<p>Some html</p>';
    $xml = '<?xml version="1.0" encoding="UTF-8"?><thing>...</thing>';
    $json = ['actually' => 'this is an array'];

    $this->assertHtmlSameAsFile('some.html', $html);
    $this->assertXmlSameAsFile('some.xml', $xml);
    $this->assertJsonSameAsFile('some.json', $json);
}

See Cake's docs for more details on usage of assertSameAsFile on which these methods are based.

统计信息

  • 总下载量: 71.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 1
  • 依赖项目数: 20
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 5
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-05-03