Skip to content

Latest commit

 

History

History
95 lines (67 loc) · 1.94 KB

03-PHPUnit-Wrappers.md

File metadata and controls

95 lines (67 loc) · 1.94 KB

Assert and Should

Prefer a video introduction?

This package also includes two wrappers (you can add more) around PHPUnit's assertion library. For example, rather than typing:

$this->assertEquals(4, 2 + 2);

You can instead do:

Assert::equals(4, 2 + 2);

# or
Should::equal(4, 2 + 2);

To allow for more readability, when using Should, you may prepend be or have, like so:

Should::beTrue(true);
Should::haveCount(2, ['a', 'b']);

Aliases

Additionally, you can register your own aliases.

Should::getInstance()->registerAliases([
  'beCakesAndPies' => 'assertTrue'
]);

# or

Assert::getInstance()->registerAliases([
  'eq' => 'assertEquals'
]);

Now, you can use Should::beCakesAndPies and Assert::eq in your tests, and they will map to assertTrue and assertEquals, respectively.

Usage

Within your test file, use your desired assertion wrapper (or both).

<?php

use Way\Tests\Should;

class DemoTest extends PHPUnit_Framework_TestCase {
  public function testItWorks()
  {
    Should::beTrue(true);
  }
}

And that's it! Here's a few examples:

<?php

use Way\Tests\Should;
use Way\Tests\Assert;

class DemoTest extends PHPUnit_Framework_TestCase {
  public function testItWorks()
  {
    Should::beTrue(true);
    Assert::true(true);

    Should::equal(4, 2 + 2);
    Should::eq(4, 2 + 2);
    Assert::equals(4, 2 + 2);
    Assert::eq(4, 2 + 2);

    Should::beGreaterThan(20, 21);
    Assert::greaterThan(20, 21);

    Should::contain('Joe', ['John', 'Joe']);
    Should::have('Joe', ['John', 'Joe']);
    Assert::has('Joe', ['John', 'Joe']);
    Assert::has('Joe', 'Joey');

    Should::haveCount(2, ['1', '2']);
    Assert::count(2, ['1', '2']);
  }
}

Remember: these are simple wrappers around PHPUnit's assertions. Refer to the sidebar here for a full list.