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.

82 lines
2.6 KiB

3 years ago
  1. <?php
  2. namespace Hamcrest;
  3. use PHPUnit\Framework\TestCase;
  4. class UtilTest extends TestCase
  5. {
  6. public function testWrapValueWithIsEqualLeavesMatchersUntouched()
  7. {
  8. $matcher = new \Hamcrest\Text\MatchesPattern('/fo+/');
  9. $newMatcher = \Hamcrest\Util::wrapValueWithIsEqual($matcher);
  10. $this->assertSame($matcher, $newMatcher);
  11. }
  12. public function testWrapValueWithIsEqualWrapsPrimitive()
  13. {
  14. $matcher = \Hamcrest\Util::wrapValueWithIsEqual('foo');
  15. $this->assertInstanceOf('Hamcrest\Core\IsEqual', $matcher);
  16. $this->assertTrue($matcher->matches('foo'));
  17. }
  18. public function testCheckAllAreMatchersAcceptsMatchers()
  19. {
  20. \Hamcrest\Util::checkAllAreMatchers(array(
  21. new \Hamcrest\Text\MatchesPattern('/fo+/'),
  22. new \Hamcrest\Core\IsEqual('foo'),
  23. ));
  24. }
  25. /**
  26. * @expectedException InvalidArgumentException
  27. */
  28. public function testCheckAllAreMatchersFailsForPrimitive()
  29. {
  30. \Hamcrest\Util::checkAllAreMatchers(array(
  31. new \Hamcrest\Text\MatchesPattern('/fo+/'),
  32. 'foo',
  33. ));
  34. }
  35. private function callAndAssertCreateMatcherArray($items)
  36. {
  37. $matchers = \Hamcrest\Util::createMatcherArray($items);
  38. $this->assertInternalType('array', $matchers);
  39. $this->assertSameSize($items, $matchers);
  40. foreach ($matchers as $matcher) {
  41. $this->assertInstanceOf('\Hamcrest\Matcher', $matcher);
  42. }
  43. return $matchers;
  44. }
  45. public function testCreateMatcherArrayLeavesMatchersUntouched()
  46. {
  47. $matcher = new \Hamcrest\Text\MatchesPattern('/fo+/');
  48. $items = array($matcher);
  49. $matchers = $this->callAndAssertCreateMatcherArray($items);
  50. $this->assertSame($matcher, $matchers[0]);
  51. }
  52. public function testCreateMatcherArrayWrapsPrimitiveWithIsEqualMatcher()
  53. {
  54. $matchers = $this->callAndAssertCreateMatcherArray(array('foo'));
  55. $this->assertInstanceOf('Hamcrest\Core\IsEqual', $matchers[0]);
  56. $this->assertTrue($matchers[0]->matches('foo'));
  57. }
  58. public function testCreateMatcherArrayDoesntModifyOriginalArray()
  59. {
  60. $items = array('foo');
  61. $this->callAndAssertCreateMatcherArray($items);
  62. $this->assertSame('foo', $items[0]);
  63. }
  64. public function testCreateMatcherArrayUnwrapsSingleArrayElement()
  65. {
  66. $matchers = $this->callAndAssertCreateMatcherArray(array(array('foo')));
  67. $this->assertInstanceOf('Hamcrest\Core\IsEqual', $matchers[0]);
  68. $this->assertTrue($matchers[0]->matches('foo'));
  69. }
  70. }