Нема описа

MatcherAssertTest.php 7.2KB

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