StringContainsTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Hamcrest\Text;
  3. class StringContainsTest extends \Hamcrest\AbstractMatcherTest
  4. {
  5. const EXCERPT = 'EXCERPT';
  6. private $_stringContains;
  7. public function setUp()
  8. {
  9. $this->_stringContains = \Hamcrest\Text\StringContains::containsString(self::EXCERPT);
  10. }
  11. protected function createMatcher()
  12. {
  13. return $this->_stringContains;
  14. }
  15. public function testEvaluatesToTrueIfArgumentContainsSubstring()
  16. {
  17. $this->assertTrue(
  18. $this->_stringContains->matches(self::EXCERPT . 'END'),
  19. 'should be true if excerpt at beginning'
  20. );
  21. $this->assertTrue(
  22. $this->_stringContains->matches('START' . self::EXCERPT),
  23. 'should be true if excerpt at end'
  24. );
  25. $this->assertTrue(
  26. $this->_stringContains->matches('START' . self::EXCERPT . 'END'),
  27. 'should be true if excerpt in middle'
  28. );
  29. $this->assertTrue(
  30. $this->_stringContains->matches(self::EXCERPT . self::EXCERPT),
  31. 'should be true if excerpt is repeated'
  32. );
  33. $this->assertFalse(
  34. $this->_stringContains->matches('Something else'),
  35. 'should not be true if excerpt is not in string'
  36. );
  37. $this->assertFalse(
  38. $this->_stringContains->matches(substr(self::EXCERPT, 1)),
  39. 'should not be true if part of excerpt is in string'
  40. );
  41. }
  42. public function testEvaluatesToTrueIfArgumentIsEqualToSubstring()
  43. {
  44. $this->assertTrue(
  45. $this->_stringContains->matches(self::EXCERPT),
  46. 'should be true if excerpt is entire string'
  47. );
  48. }
  49. public function testEvaluatesToFalseIfArgumentContainsSubstringIgnoringCase()
  50. {
  51. $this->assertFalse(
  52. $this->_stringContains->matches(strtolower(self::EXCERPT)),
  53. 'should be false if excerpt is entire string ignoring case'
  54. );
  55. $this->assertFalse(
  56. $this->_stringContains->matches('START' . strtolower(self::EXCERPT) . 'END'),
  57. 'should be false if excerpt is contained in string ignoring case'
  58. );
  59. }
  60. public function testIgnoringCaseReturnsCorrectMatcher()
  61. {
  62. $this->assertTrue(
  63. $this->_stringContains->ignoringCase()->matches('EXceRpT'),
  64. 'should be true if excerpt is entire string ignoring case'
  65. );
  66. }
  67. public function testHasAReadableDescription()
  68. {
  69. $this->assertDescription(
  70. 'a string containing "'
  71. . self::EXCERPT . '"',
  72. $this->_stringContains
  73. );
  74. }
  75. }