承接 gong023/assert_chain 相关项目开发

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

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

gong023/assert_chain

最新稳定版本:0.2.0

Composer 安装命令:

composer require gong023/assert_chain

包简介

enable you to use phpunit assert with method chain

README 文档

README

Build Status

AssertChain is PHPUnit helper. It enables you to write PHPUnit assert with method chain.

Setup

Install AssertChain by composer.

composer require --dev gong023/assert_chain:dev-master
# install phpunit if you do not install yet.
# composer require --dev phpunit/phpunit:4.*

Import AssertChain trait to your test class.

class YourTestClass extends \PHPUnit_Framework_TestCase
{
    use AssertChain\AssertChain;
}

Usage

First you call assert, and keep asserting as you like.

You can use all assertions in PHPUnit_Framework_Assert.

public function testWithArray()
{
    $arr = [
        'intKey'    => 1,
        'stringKey' => 'foo',
        'boolKey'   => true,
    ];

    $this->assert()
      ->notEmpty($arr)
      ->arrayHasKey('intKey', $arr)
      ->same(1, $arr['intKey'])
      ->arrayHasKey('stringKey', $arr)
      ->same('foo', $arr['stringKey'])
      ->arrayHasKey('boolKey', $arr)
      ->true($arr['boolKey']);

    /*
     * above code is equal to this.
     *
     * $this->assertNotEmpty($arr);
     * $this->assertArrayHasKey('intKey', $arr);
     * $this->assertSame(1, $arr['intKey']);
     * $this->assertArrayHasKey('stringKey', $arr);
     * $this->assertSame('foo', $arr['stringKey']);
     * $this->assertArrayHasKey('boolKey', $arr);
     * $this->assertTrue($arr['boolKey']);
     */
}

If you are sick of writing $actual value too many times, you can use centralizedAssert.

public function testWithArray()
{
    $arr = ['key' => 'value'];

    $this->centralizedAssert($arr)
      ->notNull()
      ->notEmpty()
      ->notCount(0)
      ->count(1)
      ->arrayNotHasKey('no existing key')
      ->arrayHasKey('key')
      ->notContains('no existing value')
      ->contains('value')
      ->equals(['key' => 'value']);

    /*
     * above code is equal to this.
     *
     * $this->assertNotNull($arr);
     * $this->assertNotEmpty($arr);
     * $this->assertNotCount(0, $arr);
     * $this->assertCount(1, $arr);
     * $this->assertArrayNotHasKey('no existing key', $arr);
     * $this->assertNotContains('no existing value', $arr);
     * $this->assertContains('value', $arr);
     * $this->assertEquals(['key' => 'value']);
     */
}

centralizedAssert automatically sets $actual value to every assertions in PHPUnit_Framework_Assert.

You need not type $actual value too many times.

If you want to get more idea with centralizedAssert, AssertChain test class will help you.

IDE friendly

AssertChain is IDE friendly. You can get method autocomplete.

统计信息

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

GitHub 信息

  • Stars: 14
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-12-24