/daisymtw/vendor/codeception/base/src/Codeception/Module/Asserts.php

https://gitlab.com/panace/public · PHP · 396 lines · 156 code · 33 blank · 207 comment · 5 complexity · b94fa8729170457dcfdfb51d7b3f0e89 MD5 · raw file

  1. <?php
  2. namespace Codeception\Module;
  3. use Codeception\Module as CodeceptionModule;
  4. /**
  5. * Special module for using asserts in your tests.
  6. */
  7. class Asserts extends CodeceptionModule
  8. {
  9. /**
  10. * Checks that two variables are equal.
  11. *
  12. * @param $expected
  13. * @param $actual
  14. * @param string $message
  15. */
  16. public function assertEquals($expected, $actual, $message = '')
  17. {
  18. parent::assertEquals($expected, $actual, $message);
  19. }
  20. /**
  21. * Checks that two variables are not equal
  22. *
  23. * @param $expected
  24. * @param $actual
  25. * @param string $message
  26. */
  27. public function assertNotEquals($expected, $actual, $message = '')
  28. {
  29. parent::assertNotEquals($expected, $actual, $message);
  30. }
  31. /**
  32. * Checks that two variables are same
  33. *
  34. * @param $expected
  35. * @param $actual
  36. * @param string $message
  37. * @return mixed|void
  38. */
  39. public function assertSame($expected, $actual, $message = '')
  40. {
  41. parent::assertSame($expected, $actual, $message);
  42. }
  43. /**
  44. * Checks that two variables are not same
  45. *
  46. * @param $expected
  47. * @param $actual
  48. * @param string $message
  49. */
  50. public function assertNotSame($expected, $actual, $message = '')
  51. {
  52. parent::assertNotSame($expected, $actual, $message);
  53. }
  54. /**
  55. * Checks that actual is greater than expected
  56. *
  57. * @param $expected
  58. * @param $actual
  59. * @param string $message
  60. */
  61. public function assertGreaterThan($expected, $actual, $message = '')
  62. {
  63. parent::assertGreaterThan($expected, $actual, $message);
  64. }
  65. /**
  66. * Checks that actual is greater or equal than expected
  67. *
  68. * @param $expected
  69. * @param $actual
  70. * @param string $message
  71. */
  72. public function assertGreaterThanOrEqual($expected, $actual, $message = '')
  73. {
  74. parent::assertGreaterThanOrEqual($expected, $actual, $message);
  75. }
  76. /**
  77. * Checks that actual is less than expected
  78. *
  79. * @param $expected
  80. * @param $actual
  81. * @param string $message
  82. */
  83. public function assertLessThan($expected, $actual, $message = '')
  84. {
  85. parent::assertLessThan($expected, $actual, $message);
  86. }
  87. /**
  88. * Checks that actual is less or equal than expected
  89. *
  90. * @param $expected
  91. * @param $actual
  92. * @param string $message
  93. */
  94. public function assertLessThanOrEqual($expected, $actual, $message = '')
  95. {
  96. parent::assertLessThanOrEqual($expected, $actual, $message);
  97. }
  98. /**
  99. * Checks that haystack contains needle
  100. *
  101. * @param $needle
  102. * @param $haystack
  103. * @param string $message
  104. */
  105. public function assertContains($needle, $haystack, $message = '')
  106. {
  107. parent::assertContains($needle, $haystack, $message);
  108. }
  109. /**
  110. * Checks that haystack doesn't contain needle.
  111. *
  112. * @param $needle
  113. * @param $haystack
  114. * @param string $message
  115. */
  116. public function assertNotContains($needle, $haystack, $message = '')
  117. {
  118. parent::assertNotContains($needle, $haystack, $message);
  119. }
  120. /**
  121. * Checks that string match with pattern
  122. *
  123. * @param string $pattern
  124. * @param string $string
  125. * @param string $message
  126. */
  127. public function assertRegExp($pattern, $string, $message = '')
  128. {
  129. parent::assertRegExp($pattern, $string, $message);
  130. }
  131. /**
  132. * Checks that string not match with pattern
  133. *
  134. * @param string $pattern
  135. * @param string $string
  136. * @param string $message
  137. */
  138. public function assertNotRegExp($pattern, $string, $message = '')
  139. {
  140. parent::assertNotRegExp($pattern, $string, $message);
  141. }
  142. /**
  143. * Checks that variable is empty.
  144. *
  145. * @param $actual
  146. * @param string $message
  147. */
  148. public function assertEmpty($actual, $message = '')
  149. {
  150. parent::assertEmpty($actual, $message);
  151. }
  152. /**
  153. * Checks that variable is not empty.
  154. *
  155. * @param $actual
  156. * @param string $message
  157. */
  158. public function assertNotEmpty($actual, $message = '')
  159. {
  160. parent::assertNotEmpty($actual, $message);
  161. }
  162. /**
  163. * Checks that variable is NULL
  164. *
  165. * @param $actual
  166. * @param string $message
  167. */
  168. public function assertNull($actual, $message = '')
  169. {
  170. parent::assertNull($actual, $message);
  171. }
  172. /**
  173. * Checks that variable is not NULL
  174. *
  175. * @param $actual
  176. * @param string $message
  177. */
  178. public function assertNotNull($actual, $message = '')
  179. {
  180. parent::assertNotNull($actual, $message);
  181. }
  182. /**
  183. * Checks that condition is positive.
  184. *
  185. * @param $condition
  186. * @param string $message
  187. */
  188. public function assertTrue($condition, $message = '')
  189. {
  190. parent::assertTrue($condition, $message);
  191. }
  192. /**
  193. * Checks that condition is negative.
  194. *
  195. * @param $condition
  196. * @param string $message
  197. */
  198. public function assertFalse($condition, $message = '')
  199. {
  200. parent::assertFalse($condition, $message);
  201. }
  202. /**
  203. * Checks if file exists
  204. *
  205. * @param string $filename
  206. * @param string $message
  207. */
  208. public function assertFileExists($filename, $message = '')
  209. {
  210. parent::assertFileExists($filename, $message);
  211. }
  212. /**
  213. * Checks if file doesn't exist
  214. *
  215. * @param string $filename
  216. * @param string $message
  217. */
  218. public function assertFileNotExists($filename, $message = '')
  219. {
  220. parent::assertFileNotExists($filename, $message);
  221. }
  222. /**
  223. * @param $expected
  224. * @param $actual
  225. * @param $description
  226. */
  227. public function assertGreaterOrEquals($expected, $actual, $description = '')
  228. {
  229. $this->assertGreaterThanOrEqual($expected, $actual, $description);
  230. }
  231. /**
  232. * @param $expected
  233. * @param $actual
  234. * @param $description
  235. */
  236. public function assertLessOrEquals($expected, $actual, $description = '')
  237. {
  238. $this->assertLessThanOrEqual($expected, $actual, $description);
  239. }
  240. /**
  241. * @param $actual
  242. * @param $description
  243. */
  244. public function assertIsEmpty($actual, $description = '')
  245. {
  246. $this->assertEmpty($actual, $description);
  247. }
  248. /**
  249. * @param $key
  250. * @param $actual
  251. * @param $description
  252. */
  253. public function assertArrayHasKey($key, $actual, $description = '')
  254. {
  255. parent::assertArrayHasKey($key, $actual, $description);
  256. }
  257. /**
  258. * @param $key
  259. * @param $actual
  260. * @param $description
  261. */
  262. public function assertArrayNotHasKey($key, $actual, $description = '')
  263. {
  264. parent::assertArrayNotHasKey($key, $actual, $description);
  265. }
  266. /**
  267. * @param $class
  268. * @param $actual
  269. * @param $description
  270. */
  271. public function assertInstanceOf($class, $actual, $description = '')
  272. {
  273. parent::assertInstanceOf($class, $actual, $description);
  274. }
  275. /**
  276. * @param $class
  277. * @param $actual
  278. * @param $description
  279. */
  280. public function assertNotInstanceOf($class, $actual, $description = '')
  281. {
  282. parent::assertNotInstanceOf($class, $actual, $description);
  283. }
  284. /**
  285. * @param $type
  286. * @param $actual
  287. * @param $description
  288. */
  289. public function assertInternalType($type, $actual, $description = '')
  290. {
  291. parent::assertInternalType($type, $actual, $description);
  292. }
  293. /**
  294. * Fails the test with message.
  295. *
  296. * @param $message
  297. */
  298. public function fail($message)
  299. {
  300. parent::fail($message);
  301. }
  302. /**
  303. * Handles and checks exception called inside callback function.
  304. * Either exception class name or exception instance should be provided.
  305. *
  306. * ```php
  307. * <?php
  308. * $I->expectException(MyException::class, function() {
  309. * $this->doSomethingBad();
  310. * });
  311. *
  312. * $I->expectException(new MyException(), function() {
  313. * $this->doSomethingBad();
  314. * });
  315. * ```
  316. * If you want to check message or exception code, you can pass them with exception instance:
  317. * ```php
  318. * <?php
  319. * // will check that exception MyException is thrown with "Don't do bad things" message
  320. * $I->expectException(new MyException("Don't do bad things"), function() {
  321. * $this->doSomethingBad();
  322. * });
  323. * ```
  324. *
  325. * @param $exception string or \Exception
  326. * @param $callback
  327. */
  328. public function expectException($exception, $callback)
  329. {
  330. $code = null;
  331. $msg = null;
  332. if (is_object($exception)) {
  333. /** @var $exception \Exception **/
  334. $class = get_class($exception);
  335. $msg = $exception->getMessage();
  336. $code = $exception->getCode();
  337. } else {
  338. $class = $exception;
  339. }
  340. try {
  341. $callback();
  342. } catch (\Exception $e) {
  343. if (!$e instanceof $class) {
  344. $this->fail(sprintf("Exception of class $class expected to be thrown, but %s caught", get_class($e)));
  345. }
  346. if (null !== $msg and $e->getMessage() !== $msg) {
  347. $this->fail(sprintf(
  348. "Exception of $class expected to be '$msg', but actual message was '%s'",
  349. $e->getMessage()
  350. ));
  351. }
  352. if (null !== $code and $e->getCode() !== $code) {
  353. $this->fail(sprintf(
  354. "Exception of $class expected to have code $code, but actual code was %s",
  355. $e->getCode()
  356. ));
  357. }
  358. $this->assertTrue(true); // increment assertion counter
  359. return;
  360. }
  361. $this->fail("Expected exception to be thrown, but nothing was caught");
  362. }
  363. }