2017 © Pedro Peláez
 

library assert_chain

enable you to use phpunit assert with method chain

image

gong023/assert_chain

enable you to use phpunit assert with method chain

  • Friday, October 13, 2017
  • by gong023
  • Repository
  • 2 Watchers
  • 14 Stars
  • 19,367 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 3 Versions
  • 2 % Grown

The README.md

AssertChain

Build Status, (*1)

AssertChain is PHPUnit helper. It enables you to write PHPUnit assert with method chain., (*2)

Setup

Install AssertChain by composer., (*3)

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., (*4)

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

Usage

First you call assert, and keep asserting as you like., (*5)

You can use all assertions in PHPUnit_Framework_Assert., (*6)

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., (*7)

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., (*8)

You need not type $actual value too many times., (*9)

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

IDE friendly

AssertChain is IDE friendly. You can get method autocomplete., (*11)

The Versions

13/10 2017

dev-master

9999999-dev

enable you to use phpunit assert with method chain

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Avatar gong023

13/10 2017

0.2.0

0.2.0.0

enable you to use phpunit assert with method chain

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Avatar gong023

24/12 2014

0.1.0

0.1.0.0

enable you to use phpunit assert with method chain

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Avatar gong023