You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.9 KiB

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