PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Codeception/Util/Shared/Asserts.php

https://github.com/pmcjury/Codeception
PHP | 189 lines | 84 code | 20 blank | 85 comment | 4 complexity | 92d137df95120c894e77c285f2690f51 MD5 | raw file
  1. <?php
  2. namespace Codeception\Util\Shared;
  3. trait Asserts
  4. {
  5. protected function assert($arguments, $not = false)
  6. {
  7. $not = $not ? 'Not' : '';
  8. $method = ucfirst(array_shift($arguments));
  9. if (($method === 'True') && $not) {
  10. $method = 'False';
  11. $not = '';
  12. }
  13. if (($method === 'False') && $not) {
  14. $method = 'True';
  15. $not = '';
  16. }
  17. call_user_func_array(array('\PHPUnit_Framework_Assert', 'assert' . $not . $method), $arguments);
  18. }
  19. protected function assertNot($arguments)
  20. {
  21. $this->assert($arguments, true);
  22. }
  23. /**
  24. * Checks that two variables are equal.
  25. *
  26. * @param $expected
  27. * @param $actual
  28. * @param string $message
  29. *
  30. * @return mixed
  31. */
  32. protected function assertEquals($expected, $actual, $message = '')
  33. {
  34. \PHPUnit_Framework_Assert::assertEquals($expected, $actual, $message);
  35. }
  36. /**
  37. * Checks that two variables are not equal
  38. *
  39. * @param $expected
  40. * @param $actual
  41. * @param string $message
  42. */
  43. protected function assertNotEquals($expected, $actual, $message = '')
  44. {
  45. \PHPUnit_Framework_Assert::assertNotEquals($expected, $actual, $message);
  46. }
  47. /**
  48. * Checks that expected is greater then actual
  49. *
  50. * @param $expected
  51. * @param $actual
  52. * @param string $message
  53. */
  54. protected function assertGreaterThen($expected, $actual, $message = '')
  55. {
  56. \PHPUnit_Framework_Assert::assertGreaterThan($expected, $actual, $message);
  57. }
  58. /**
  59. * Checks that expected is greater or equal then actual
  60. *
  61. * @param $expected
  62. * @param $actual
  63. * @param string $message
  64. */
  65. protected function assertGreaterThenOrEqual($expected, $actual, $message = '')
  66. {
  67. \PHPUnit_Framework_Assert::assertGreaterThanOrEqual($expected, $actual, $message);
  68. }
  69. /**
  70. * Checks that haystack contains needle
  71. *
  72. * @param $needle
  73. * @param $haystack
  74. * @param string $message
  75. */
  76. protected function assertContains($needle, $haystack, $message = '')
  77. {
  78. \PHPUnit_Framework_Assert::assertContains($needle, $haystack, $message);
  79. }
  80. /**
  81. * Checks that haystack doesn't contain needle.
  82. *
  83. * @param $needle
  84. * @param $haystack
  85. * @param string $message
  86. */
  87. protected function assertNotContains($needle, $haystack, $message = '')
  88. {
  89. \PHPUnit_Framework_Assert::assertNotContains($needle, $haystack, $message);
  90. }
  91. /**
  92. * Checks that variable is empty.
  93. *
  94. * @param $actual
  95. * @param string $message
  96. */
  97. protected function assertEmpty($actual, $message = '')
  98. {
  99. \PHPUnit_Framework_Assert::assertEmpty($actual, $message);
  100. }
  101. /**
  102. * Checks that variable is not empty.
  103. *
  104. * @param $actual
  105. * @param string $message
  106. */
  107. protected function assertNotEmpty($actual, $message = '')
  108. {
  109. \PHPUnit_Framework_Assert::assertNotEmpty($actual, $message);
  110. }
  111. /**
  112. * Checks that variable is NULL
  113. *
  114. * @param $actual
  115. * @param string $message
  116. */
  117. protected function assertNull($actual, $message = '')
  118. {
  119. \PHPUnit_Framework_Assert::assertNull($actual, $message);
  120. }
  121. /**
  122. * Checks that variable is not NULL
  123. *
  124. * @param $actual
  125. * @param string $message
  126. */
  127. protected function assertNotNull($actual, $message = '')
  128. {
  129. \PHPUnit_Framework_Assert::assertNotNull($actual, $message);
  130. }
  131. /**
  132. * Checks that condition is positive.
  133. *
  134. * @param $condition
  135. * @param string $message
  136. */
  137. protected function assertTrue($condition, $message = '')
  138. {
  139. \PHPUnit_Framework_Assert::assertTrue($condition, $message);
  140. }
  141. /**
  142. * Checks that condition is negative.
  143. *
  144. * @param $condition
  145. * @param string $message
  146. */
  147. protected function assertFalse($condition, $message = '')
  148. {
  149. \PHPUnit_Framework_Assert::assertFalse($condition, $message);
  150. }
  151. protected function assertThat($haystack, $constraint, $message)
  152. {
  153. \PHPUnit_Framework_Assert::assertThat($haystack, $constraint, $message);
  154. }
  155. protected function assertThatItsNot($haystack, $constraint, $message)
  156. {
  157. $constraint = new \PHPUnit_Framework_Constraint_Not($constraint);
  158. \PHPUnit_Framework_Assert::assertThat($haystack, $constraint, $message);
  159. }
  160. /**
  161. * Fails the test with message.
  162. *
  163. * @param $message
  164. */
  165. protected function fail($message)
  166. {
  167. \PHPUnit_Framework_Assert::fail($message);
  168. }
  169. }