UtilTest.php 2.6KB

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