MatcherAssertTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace Hamcrest;
  3. class MatcherAssertTest extends \PhpUnit_Framework_TestCase
  4. {
  5. protected function setUp()
  6. {
  7. \Hamcrest\MatcherAssert::resetCount();
  8. }
  9. public function testResetCount()
  10. {
  11. \Hamcrest\MatcherAssert::assertThat(true);
  12. self::assertEquals(1, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
  13. \Hamcrest\MatcherAssert::resetCount();
  14. self::assertEquals(0, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
  15. }
  16. public function testAssertThatWithTrueArgPasses()
  17. {
  18. \Hamcrest\MatcherAssert::assertThat(true);
  19. \Hamcrest\MatcherAssert::assertThat('non-empty');
  20. \Hamcrest\MatcherAssert::assertThat(1);
  21. \Hamcrest\MatcherAssert::assertThat(3.14159);
  22. \Hamcrest\MatcherAssert::assertThat(array(true));
  23. self::assertEquals(5, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
  24. }
  25. public function testAssertThatWithFalseArgFails()
  26. {
  27. try {
  28. \Hamcrest\MatcherAssert::assertThat(false);
  29. self::fail('expected assertion failure');
  30. } catch (\Hamcrest\AssertionError $ex) {
  31. self::assertEquals('', $ex->getMessage());
  32. }
  33. try {
  34. \Hamcrest\MatcherAssert::assertThat(null);
  35. self::fail('expected assertion failure');
  36. } catch (\Hamcrest\AssertionError $ex) {
  37. self::assertEquals('', $ex->getMessage());
  38. }
  39. try {
  40. \Hamcrest\MatcherAssert::assertThat('');
  41. self::fail('expected assertion failure');
  42. } catch (\Hamcrest\AssertionError $ex) {
  43. self::assertEquals('', $ex->getMessage());
  44. }
  45. try {
  46. \Hamcrest\MatcherAssert::assertThat(0);
  47. self::fail('expected assertion failure');
  48. } catch (\Hamcrest\AssertionError $ex) {
  49. self::assertEquals('', $ex->getMessage());
  50. }
  51. try {
  52. \Hamcrest\MatcherAssert::assertThat(0.0);
  53. self::fail('expected assertion failure');
  54. } catch (\Hamcrest\AssertionError $ex) {
  55. self::assertEquals('', $ex->getMessage());
  56. }
  57. try {
  58. \Hamcrest\MatcherAssert::assertThat(array());
  59. self::fail('expected assertion failure');
  60. } catch (\Hamcrest\AssertionError $ex) {
  61. self::assertEquals('', $ex->getMessage());
  62. }
  63. self::assertEquals(6, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
  64. }
  65. public function testAssertThatWithIdentifierAndTrueArgPasses()
  66. {
  67. \Hamcrest\MatcherAssert::assertThat('identifier', true);
  68. \Hamcrest\MatcherAssert::assertThat('identifier', 'non-empty');
  69. \Hamcrest\MatcherAssert::assertThat('identifier', 1);
  70. \Hamcrest\MatcherAssert::assertThat('identifier', 3.14159);
  71. \Hamcrest\MatcherAssert::assertThat('identifier', array(true));
  72. self::assertEquals(5, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
  73. }
  74. public function testAssertThatWithIdentifierAndFalseArgFails()
  75. {
  76. try {
  77. \Hamcrest\MatcherAssert::assertThat('identifier', false);
  78. self::fail('expected assertion failure');
  79. } catch (\Hamcrest\AssertionError $ex) {
  80. self::assertEquals('identifier', $ex->getMessage());
  81. }
  82. try {
  83. \Hamcrest\MatcherAssert::assertThat('identifier', null);
  84. self::fail('expected assertion failure');
  85. } catch (\Hamcrest\AssertionError $ex) {
  86. self::assertEquals('identifier', $ex->getMessage());
  87. }
  88. try {
  89. \Hamcrest\MatcherAssert::assertThat('identifier', '');
  90. self::fail('expected assertion failure');
  91. } catch (\Hamcrest\AssertionError $ex) {
  92. self::assertEquals('identifier', $ex->getMessage());
  93. }
  94. try {
  95. \Hamcrest\MatcherAssert::assertThat('identifier', 0);
  96. self::fail('expected assertion failure');
  97. } catch (\Hamcrest\AssertionError $ex) {
  98. self::assertEquals('identifier', $ex->getMessage());
  99. }
  100. try {
  101. \Hamcrest\MatcherAssert::assertThat('identifier', 0.0);
  102. self::fail('expected assertion failure');
  103. } catch (\Hamcrest\AssertionError $ex) {
  104. self::assertEquals('identifier', $ex->getMessage());
  105. }
  106. try {
  107. \Hamcrest\MatcherAssert::assertThat('identifier', array());
  108. self::fail('expected assertion failure');
  109. } catch (\Hamcrest\AssertionError $ex) {
  110. self::assertEquals('identifier', $ex->getMessage());
  111. }
  112. self::assertEquals(6, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
  113. }
  114. public function testAssertThatWithActualValueAndMatcherArgsThatMatchPasses()
  115. {
  116. \Hamcrest\MatcherAssert::assertThat(true, is(true));
  117. self::assertEquals(1, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
  118. }
  119. public function testAssertThatWithActualValueAndMatcherArgsThatDontMatchFails()
  120. {
  121. $expected = 'expected';
  122. $actual = 'actual';
  123. $expectedMessage =
  124. 'Expected: "expected"' . PHP_EOL .
  125. ' but: was "actual"';
  126. try {
  127. \Hamcrest\MatcherAssert::assertThat($actual, equalTo($expected));
  128. self::fail('expected assertion failure');
  129. } catch (\Hamcrest\AssertionError $ex) {
  130. self::assertEquals($expectedMessage, $ex->getMessage());
  131. self::assertEquals(1, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
  132. }
  133. }
  134. public function testAssertThatWithIdentifierAndActualValueAndMatcherArgsThatMatchPasses()
  135. {
  136. \Hamcrest\MatcherAssert::assertThat('identifier', true, is(true));
  137. self::assertEquals(1, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
  138. }
  139. public function testAssertThatWithIdentifierAndActualValueAndMatcherArgsThatDontMatchFails()
  140. {
  141. $expected = 'expected';
  142. $actual = 'actual';
  143. $expectedMessage =
  144. 'identifier' . PHP_EOL .
  145. 'Expected: "expected"' . PHP_EOL .
  146. ' but: was "actual"';
  147. try {
  148. \Hamcrest\MatcherAssert::assertThat('identifier', $actual, equalTo($expected));
  149. self::fail('expected assertion failure');
  150. } catch (\Hamcrest\AssertionError $ex) {
  151. self::assertEquals($expectedMessage, $ex->getMessage());
  152. self::assertEquals(1, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
  153. }
  154. }
  155. public function testAssertThatWithNoArgsThrowsErrorAndDoesntIncrementCount()
  156. {
  157. try {
  158. \Hamcrest\MatcherAssert::assertThat();
  159. self::fail('expected invalid argument exception');
  160. } catch (\InvalidArgumentException $ex) {
  161. self::assertEquals(0, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
  162. }
  163. }
  164. public function testAssertThatWithFourArgsThrowsErrorAndDoesntIncrementCount()
  165. {
  166. try {
  167. \Hamcrest\MatcherAssert::assertThat(1, 2, 3, 4);
  168. self::fail('expected invalid argument exception');
  169. } catch (\InvalidArgumentException $ex) {
  170. self::assertEquals(0, \Hamcrest\MatcherAssert::getCount(), 'assertion count');
  171. }
  172. }
  173. }