/daisymtw/vendor/codeception/base/src/Codeception/Util/Shared/Asserts.php

https://gitlab.com/panace/public · PHP · 395 lines · 156 code · 40 blank · 199 comment · 4 complexity · f6c73accc128a99f9828ae7cf26f7a55 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(['\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 two variables are same
  49. *
  50. * @param $expected
  51. * @param $actual
  52. * @param string $message
  53. *
  54. * @return mixed
  55. */
  56. protected function assertSame($expected, $actual, $message = '')
  57. {
  58. \PHPUnit_Framework_Assert::assertSame($expected, $actual, $message);
  59. }
  60. /**
  61. * Checks that two variables are not same
  62. *
  63. * @param $expected
  64. * @param $actual
  65. * @param string $message
  66. */
  67. protected function assertNotSame($expected, $actual, $message = '')
  68. {
  69. \PHPUnit_Framework_Assert::assertNotSame($expected, $actual, $message);
  70. }
  71. /**
  72. * Checks that actual is greater than expected
  73. *
  74. * @param $expected
  75. * @param $actual
  76. * @param string $message
  77. */
  78. protected function assertGreaterThan($expected, $actual, $message = '')
  79. {
  80. \PHPUnit_Framework_Assert::assertGreaterThan($expected, $actual, $message);
  81. }
  82. /**
  83. * @deprecated
  84. */
  85. protected function assertGreaterThen($expected, $actual, $message = '')
  86. {
  87. \PHPUnit_Framework_Assert::assertGreaterThan($expected, $actual, $message);
  88. }
  89. /**
  90. * Checks that actual is greater or equal than expected
  91. *
  92. * @param $expected
  93. * @param $actual
  94. * @param string $message
  95. */
  96. protected function assertGreaterThanOrEqual($expected, $actual, $message = '')
  97. {
  98. \PHPUnit_Framework_Assert::assertGreaterThanOrEqual($expected, $actual, $message);
  99. }
  100. /**
  101. * @deprecated
  102. */
  103. protected function assertGreaterThenOrEqual($expected, $actual, $message = '')
  104. {
  105. \PHPUnit_Framework_Assert::assertGreaterThanOrEqual($expected, $actual, $message);
  106. }
  107. /**
  108. * Checks that actual is less than expected
  109. *
  110. * @param $expected
  111. * @param $actual
  112. * @param string $message
  113. */
  114. protected function assertLessThan($expected, $actual, $message = '')
  115. {
  116. \PHPUnit_Framework_Assert::assertLessThan($expected, $actual, $message);
  117. }
  118. /**
  119. * Checks that actual is less or equal than expected
  120. *
  121. * @param $expected
  122. * @param $actual
  123. * @param string $message
  124. */
  125. protected function assertLessThanOrEqual($expected, $actual, $message = '')
  126. {
  127. \PHPUnit_Framework_Assert::assertLessThanOrEqual($expected, $actual, $message);
  128. }
  129. /**
  130. * Checks that haystack contains needle
  131. *
  132. * @param $needle
  133. * @param $haystack
  134. * @param string $message
  135. */
  136. protected function assertContains($needle, $haystack, $message = '')
  137. {
  138. \PHPUnit_Framework_Assert::assertContains($needle, $haystack, $message);
  139. }
  140. /**
  141. * Checks that haystack doesn't contain needle.
  142. *
  143. * @param $needle
  144. * @param $haystack
  145. * @param string $message
  146. */
  147. protected function assertNotContains($needle, $haystack, $message = '')
  148. {
  149. \PHPUnit_Framework_Assert::assertNotContains($needle, $haystack, $message);
  150. }
  151. /**
  152. * Checks that string match with pattern
  153. *
  154. * @param string $pattern
  155. * @param string $string
  156. * @param string $message
  157. */
  158. protected function assertRegExp($pattern, $string, $message = '')
  159. {
  160. \PHPUnit_Framework_Assert::assertRegExp($pattern, $string, $message);
  161. }
  162. /**
  163. * Checks that string not match with pattern
  164. *
  165. * @param string $pattern
  166. * @param string $string
  167. * @param string $message
  168. */
  169. protected function assertNotRegExp($pattern, $string, $message = '')
  170. {
  171. \PHPUnit_Framework_Assert::assertNotRegExp($pattern, $string, $message);
  172. }
  173. /**
  174. * Checks that variable is empty.
  175. *
  176. * @param $actual
  177. * @param string $message
  178. */
  179. protected function assertEmpty($actual, $message = '')
  180. {
  181. \PHPUnit_Framework_Assert::assertEmpty($actual, $message);
  182. }
  183. /**
  184. * Checks that variable is not empty.
  185. *
  186. * @param $actual
  187. * @param string $message
  188. */
  189. protected function assertNotEmpty($actual, $message = '')
  190. {
  191. \PHPUnit_Framework_Assert::assertNotEmpty($actual, $message);
  192. }
  193. /**
  194. * Checks that variable is NULL
  195. *
  196. * @param $actual
  197. * @param string $message
  198. */
  199. protected function assertNull($actual, $message = '')
  200. {
  201. \PHPUnit_Framework_Assert::assertNull($actual, $message);
  202. }
  203. /**
  204. * Checks that variable is not NULL
  205. *
  206. * @param $actual
  207. * @param string $message
  208. */
  209. protected function assertNotNull($actual, $message = '')
  210. {
  211. \PHPUnit_Framework_Assert::assertNotNull($actual, $message);
  212. }
  213. /**
  214. * Checks that condition is positive.
  215. *
  216. * @param $condition
  217. * @param string $message
  218. */
  219. protected function assertTrue($condition, $message = '')
  220. {
  221. \PHPUnit_Framework_Assert::assertTrue($condition, $message);
  222. }
  223. /**
  224. * Checks that condition is negative.
  225. *
  226. * @param $condition
  227. * @param string $message
  228. */
  229. protected function assertFalse($condition, $message = '')
  230. {
  231. \PHPUnit_Framework_Assert::assertFalse($condition, $message);
  232. }
  233. /**
  234. *
  235. * @param $haystack
  236. * @param $constraint
  237. * @param string $message
  238. */
  239. protected function assertThat($haystack, $constraint, $message = '')
  240. {
  241. \PHPUnit_Framework_Assert::assertThat($haystack, $constraint, $message);
  242. }
  243. /**
  244. * Checks that haystack doesn't attend
  245. *
  246. * @param $haystack
  247. * @param $constraint
  248. * @param string $message
  249. */
  250. protected function assertThatItsNot($haystack, $constraint, $message = '')
  251. {
  252. $constraint = new \PHPUnit_Framework_Constraint_Not($constraint);
  253. \PHPUnit_Framework_Assert::assertThat($haystack, $constraint, $message);
  254. }
  255. /**
  256. * Checks if file exists
  257. *
  258. * @param string $filename
  259. * @param string $message
  260. */
  261. protected function assertFileExists($filename, $message = '')
  262. {
  263. \PHPUnit_Framework_Assert::assertFileExists($filename, $message);
  264. }
  265. /**
  266. * Checks if file doesn't exist
  267. *
  268. * @param string $filename
  269. * @param string $message
  270. */
  271. protected function assertFileNotExists($filename, $message = '')
  272. {
  273. \PHPUnit_Framework_Assert::assertFileNotExists($filename, $message);
  274. }
  275. /**
  276. * @param $expected
  277. * @param $actual
  278. * @param $description
  279. */
  280. protected function assertGreaterOrEquals($expected, $actual, $description = '')
  281. {
  282. \PHPUnit_Framework_Assert::assertGreaterThanOrEqual($expected, $actual, $description);
  283. }
  284. /**
  285. * @param $expected
  286. * @param $actual
  287. * @param $description
  288. */
  289. protected function assertLessOrEquals($expected, $actual, $description = '')
  290. {
  291. \PHPUnit_Framework_Assert::assertLessThanOrEqual($expected, $actual, $description);
  292. }
  293. /**
  294. * @param $actual
  295. * @param $description
  296. */
  297. protected function assertIsEmpty($actual, $description = '')
  298. {
  299. \PHPUnit_Framework_Assert::assertEmpty($actual, $description);
  300. }
  301. /**
  302. * @param $key
  303. * @param $actual
  304. * @param $description
  305. */
  306. protected function assertArrayHasKey($key, $actual, $description = '')
  307. {
  308. \PHPUnit_Framework_Assert::assertArrayHasKey($key, $actual, $description);
  309. }
  310. /**
  311. * @param $key
  312. * @param $actual
  313. * @param $description
  314. */
  315. protected function assertArrayNotHasKey($key, $actual, $description = '')
  316. {
  317. \PHPUnit_Framework_Assert::assertArrayNotHasKey($key, $actual, $description);
  318. }
  319. /**
  320. * @param $class
  321. * @param $actual
  322. * @param $description
  323. */
  324. protected function assertInstanceOf($class, $actual, $description = '')
  325. {
  326. \PHPUnit_Framework_Assert::assertInstanceOf($class, $actual, $description);
  327. }
  328. /**
  329. * @param $class
  330. * @param $actual
  331. * @param $description
  332. */
  333. protected function assertNotInstanceOf($class, $actual, $description = '')
  334. {
  335. \PHPUnit_Framework_Assert::assertNotInstanceOf($class, $actual, $description);
  336. }
  337. /**
  338. * @param $type
  339. * @param $actual
  340. * @param $description
  341. */
  342. protected function assertInternalType($type, $actual, $description = '')
  343. {
  344. \PHPUnit_Framework_Assert::assertInternalType($type, $actual, $description);
  345. }
  346. /**
  347. * Fails the test with message.
  348. *
  349. * @param $message
  350. */
  351. protected function fail($message)
  352. {
  353. \PHPUnit_Framework_Assert::fail($message);
  354. }
  355. }