Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save appkr/d65a1f9ce2a16f3cc7ea627bb90ca7ba to your computer and use it in GitHub Desktop.
Save appkr/d65a1f9ce2a16f3cc7ea627bb90ca7ba to your computer and use it in GitHub Desktop.
How to use Hamcrest Matcher with PHPUnit

1 Set up

<?php // tests/HamcrestTestCase.php

namespace Tests;

use Hamcrest\MatcherAssert;
use Hamcrest\Util;

class HamcrestTestCase extends \PHPUnit\Framework\TestCase
{
    public function runBare(): void
    {
        Util::registerGlobalFunctions();
        MatcherAssert::resetCount();

        try {
            parent::runBare();
        } finally {
            $this->addToAssertionCount(MatcherAssert::getCount());
        }
    }
}

2 Use

<?php // TrueTest.php
  
class TrueTest extends Tests\HamcrestTestCase
{
    public function testTrueIsTrue()
    {
        assertThat(true, is(true));
    }
}

3 Ref

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment