AbstractMatcherTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Hamcrest;
  3. class UnknownType {
  4. }
  5. abstract class AbstractMatcherTest extends \PHPUnit_Framework_TestCase
  6. {
  7. const ARGUMENT_IGNORED = "ignored";
  8. const ANY_NON_NULL_ARGUMENT = "notnull";
  9. abstract protected function createMatcher();
  10. public function assertMatches(\Hamcrest\Matcher $matcher, $arg, $message)
  11. {
  12. $this->assertTrue($matcher->matches($arg), $message);
  13. }
  14. public function assertDoesNotMatch(\Hamcrest\Matcher $matcher, $arg, $message)
  15. {
  16. $this->assertFalse($matcher->matches($arg), $message);
  17. }
  18. public function assertDescription($expected, \Hamcrest\Matcher $matcher)
  19. {
  20. $description = new \Hamcrest\StringDescription();
  21. $description->appendDescriptionOf($matcher);
  22. $this->assertEquals($expected, (string) $description, 'Expected description');
  23. }
  24. public function assertMismatchDescription($expected, \Hamcrest\Matcher $matcher, $arg)
  25. {
  26. $description = new \Hamcrest\StringDescription();
  27. $this->assertFalse(
  28. $matcher->matches($arg),
  29. 'Precondtion: Matcher should not match item'
  30. );
  31. $matcher->describeMismatch($arg, $description);
  32. $this->assertEquals(
  33. $expected,
  34. (string) $description,
  35. 'Expected mismatch description'
  36. );
  37. }
  38. public function testIsNullSafe()
  39. {
  40. //Should not generate any notices
  41. $this->createMatcher()->matches(null);
  42. $this->createMatcher()->describeMismatch(
  43. null,
  44. new \Hamcrest\NullDescription()
  45. );
  46. }
  47. public function testCopesWithUnknownTypes()
  48. {
  49. //Should not generate any notices
  50. $this->createMatcher()->matches(new UnknownType());
  51. $this->createMatcher()->describeMismatch(
  52. new UnknownType(),
  53. new NullDescription()
  54. );
  55. }
  56. }