PolyfillAssertTrait.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\PhpUnit\Legacy;
  11. use PHPUnit\Framework\Constraint\IsEqual;
  12. use PHPUnit\Framework\Constraint\LogicalNot;
  13. use PHPUnit\Framework\Constraint\StringContains;
  14. use PHPUnit\Framework\Constraint\TraversableContains;
  15. /**
  16. * This trait is @internal.
  17. */
  18. trait PolyfillAssertTrait
  19. {
  20. /**
  21. * @param float $delta
  22. * @param string $message
  23. *
  24. * @return void
  25. */
  26. public static function assertEqualsWithDelta($expected, $actual, $delta, $message = '')
  27. {
  28. $constraint = new IsEqual($expected, $delta);
  29. static::assertThat($actual, $constraint, $message);
  30. }
  31. /**
  32. * @param iterable $haystack
  33. * @param string $message
  34. *
  35. * @return void
  36. */
  37. public static function assertContainsEquals($needle, $haystack, $message = '')
  38. {
  39. $constraint = new TraversableContains($needle, false, false);
  40. static::assertThat($haystack, $constraint, $message);
  41. }
  42. /**
  43. * @param iterable $haystack
  44. * @param string $message
  45. *
  46. * @return void
  47. */
  48. public static function assertNotContainsEquals($needle, $haystack, $message = '')
  49. {
  50. $constraint = new LogicalNot(new TraversableContains($needle, false, false));
  51. static::assertThat($haystack, $constraint, $message);
  52. }
  53. /**
  54. * @param string $message
  55. *
  56. * @return void
  57. */
  58. public static function assertIsArray($actual, $message = '')
  59. {
  60. static::assertInternalType('array', $actual, $message);
  61. }
  62. /**
  63. * @param string $message
  64. *
  65. * @return void
  66. */
  67. public static function assertIsBool($actual, $message = '')
  68. {
  69. static::assertInternalType('bool', $actual, $message);
  70. }
  71. /**
  72. * @param string $message
  73. *
  74. * @return void
  75. */
  76. public static function assertIsFloat($actual, $message = '')
  77. {
  78. static::assertInternalType('float', $actual, $message);
  79. }
  80. /**
  81. * @param string $message
  82. *
  83. * @return void
  84. */
  85. public static function assertIsInt($actual, $message = '')
  86. {
  87. static::assertInternalType('int', $actual, $message);
  88. }
  89. /**
  90. * @param string $message
  91. *
  92. * @return void
  93. */
  94. public static function assertIsNumeric($actual, $message = '')
  95. {
  96. static::assertInternalType('numeric', $actual, $message);
  97. }
  98. /**
  99. * @param string $message
  100. *
  101. * @return void
  102. */
  103. public static function assertIsObject($actual, $message = '')
  104. {
  105. static::assertInternalType('object', $actual, $message);
  106. }
  107. /**
  108. * @param string $message
  109. *
  110. * @return void
  111. */
  112. public static function assertIsResource($actual, $message = '')
  113. {
  114. static::assertInternalType('resource', $actual, $message);
  115. }
  116. /**
  117. * @param string $message
  118. *
  119. * @return void
  120. */
  121. public static function assertIsString($actual, $message = '')
  122. {
  123. static::assertInternalType('string', $actual, $message);
  124. }
  125. /**
  126. * @param string $message
  127. *
  128. * @return void
  129. */
  130. public static function assertIsScalar($actual, $message = '')
  131. {
  132. static::assertInternalType('scalar', $actual, $message);
  133. }
  134. /**
  135. * @param string $message
  136. *
  137. * @return void
  138. */
  139. public static function assertIsCallable($actual, $message = '')
  140. {
  141. static::assertInternalType('callable', $actual, $message);
  142. }
  143. /**
  144. * @param string $message
  145. *
  146. * @return void
  147. */
  148. public static function assertIsIterable($actual, $message = '')
  149. {
  150. static::assertInternalType('iterable', $actual, $message);
  151. }
  152. /**
  153. * @param string $needle
  154. * @param string $haystack
  155. * @param string $message
  156. *
  157. * @return void
  158. */
  159. public static function assertStringContainsString($needle, $haystack, $message = '')
  160. {
  161. $constraint = new StringContains($needle, false);
  162. static::assertThat($haystack, $constraint, $message);
  163. }
  164. /**
  165. * @param string $needle
  166. * @param string $haystack
  167. * @param string $message
  168. *
  169. * @return void
  170. */
  171. public static function assertStringContainsStringIgnoringCase($needle, $haystack, $message = '')
  172. {
  173. $constraint = new StringContains($needle, true);
  174. static::assertThat($haystack, $constraint, $message);
  175. }
  176. /**
  177. * @param string $needle
  178. * @param string $haystack
  179. * @param string $message
  180. *
  181. * @return void
  182. */
  183. public static function assertStringNotContainsString($needle, $haystack, $message = '')
  184. {
  185. $constraint = new LogicalNot(new StringContains($needle, false));
  186. static::assertThat($haystack, $constraint, $message);
  187. }
  188. /**
  189. * @param string $needle
  190. * @param string $haystack
  191. * @param string $message
  192. *
  193. * @return void
  194. */
  195. public static function assertStringNotContainsStringIgnoringCase($needle, $haystack, $message = '')
  196. {
  197. $constraint = new LogicalNot(new StringContains($needle, true));
  198. static::assertThat($haystack, $constraint, $message);
  199. }
  200. /**
  201. * @param string $message
  202. *
  203. * @return void
  204. */
  205. public static function assertFinite($actual, $message = '')
  206. {
  207. static::assertInternalType('float', $actual, $message);
  208. static::assertTrue(is_finite($actual), $message ?: "Failed asserting that $actual is finite.");
  209. }
  210. /**
  211. * @param string $message
  212. *
  213. * @return void
  214. */
  215. public static function assertInfinite($actual, $message = '')
  216. {
  217. static::assertInternalType('float', $actual, $message);
  218. static::assertTrue(is_infinite($actual), $message ?: "Failed asserting that $actual is infinite.");
  219. }
  220. /**
  221. * @param string $message
  222. *
  223. * @return void
  224. */
  225. public static function assertNan($actual, $message = '')
  226. {
  227. static::assertInternalType('float', $actual, $message);
  228. static::assertTrue(is_nan($actual), $message ?: "Failed asserting that $actual is nan.");
  229. }
  230. /**
  231. * @param string $filename
  232. * @param string $message
  233. *
  234. * @return void
  235. */
  236. public static function assertIsReadable($filename, $message = '')
  237. {
  238. static::assertInternalType('string', $filename, $message);
  239. static::assertTrue(is_readable($filename), $message ?: "Failed asserting that $filename is readable.");
  240. }
  241. /**
  242. * @param string $filename
  243. * @param string $message
  244. *
  245. * @return void
  246. */
  247. public static function assertNotIsReadable($filename, $message = '')
  248. {
  249. static::assertInternalType('string', $filename, $message);
  250. static::assertFalse(is_readable($filename), $message ?: "Failed asserting that $filename is not readable.");
  251. }
  252. /**
  253. * @param string $filename
  254. * @param string $message
  255. *
  256. * @return void
  257. */
  258. public static function assertIsNotReadable($filename, $message = '')
  259. {
  260. static::assertNotIsReadable($filename, $message);
  261. }
  262. /**
  263. * @param string $filename
  264. * @param string $message
  265. *
  266. * @return void
  267. */
  268. public static function assertIsWritable($filename, $message = '')
  269. {
  270. static::assertInternalType('string', $filename, $message);
  271. static::assertTrue(is_writable($filename), $message ?: "Failed asserting that $filename is writable.");
  272. }
  273. /**
  274. * @param string $filename
  275. * @param string $message
  276. *
  277. * @return void
  278. */
  279. public static function assertNotIsWritable($filename, $message = '')
  280. {
  281. static::assertInternalType('string', $filename, $message);
  282. static::assertFalse(is_writable($filename), $message ?: "Failed asserting that $filename is not writable.");
  283. }
  284. /**
  285. * @param string $filename
  286. * @param string $message
  287. *
  288. * @return void
  289. */
  290. public static function assertIsNotWritable($filename, $message = '')
  291. {
  292. static::assertNotIsWritable($filename, $message);
  293. }
  294. /**
  295. * @param string $directory
  296. * @param string $message
  297. *
  298. * @return void
  299. */
  300. public static function assertDirectoryExists($directory, $message = '')
  301. {
  302. static::assertInternalType('string', $directory, $message);
  303. static::assertTrue(is_dir($directory), $message ?: "Failed asserting that $directory exists.");
  304. }
  305. /**
  306. * @param string $directory
  307. * @param string $message
  308. *
  309. * @return void
  310. */
  311. public static function assertDirectoryNotExists($directory, $message = '')
  312. {
  313. static::assertInternalType('string', $directory, $message);
  314. static::assertFalse(is_dir($directory), $message ?: "Failed asserting that $directory does not exist.");
  315. }
  316. /**
  317. * @param string $directory
  318. * @param string $message
  319. *
  320. * @return void
  321. */
  322. public static function assertDirectoryDoesNotExist($directory, $message = '')
  323. {
  324. static::assertDirectoryNotExists($directory, $message);
  325. }
  326. /**
  327. * @param string $directory
  328. * @param string $message
  329. *
  330. * @return void
  331. */
  332. public static function assertDirectoryIsReadable($directory, $message = '')
  333. {
  334. static::assertDirectoryExists($directory, $message);
  335. static::assertIsReadable($directory, $message);
  336. }
  337. /**
  338. * @param string $directory
  339. * @param string $message
  340. *
  341. * @return void
  342. */
  343. public static function assertDirectoryNotIsReadable($directory, $message = '')
  344. {
  345. static::assertDirectoryExists($directory, $message);
  346. static::assertNotIsReadable($directory, $message);
  347. }
  348. /**
  349. * @param string $directory
  350. * @param string $message
  351. *
  352. * @return void
  353. */
  354. public static function assertDirectoryIsNotReadable($directory, $message = '')
  355. {
  356. static::assertDirectoryNotIsReadable($directory, $message);
  357. }
  358. /**
  359. * @param string $directory
  360. * @param string $message
  361. *
  362. * @return void
  363. */
  364. public static function assertDirectoryIsWritable($directory, $message = '')
  365. {
  366. static::assertDirectoryExists($directory, $message);
  367. static::assertIsWritable($directory, $message);
  368. }
  369. /**
  370. * @param string $directory
  371. * @param string $message
  372. *
  373. * @return void
  374. */
  375. public static function assertDirectoryNotIsWritable($directory, $message = '')
  376. {
  377. static::assertDirectoryExists($directory, $message);
  378. static::assertNotIsWritable($directory, $message);
  379. }
  380. /**
  381. * @param string $directory
  382. * @param string $message
  383. *
  384. * @return void
  385. */
  386. public static function assertDirectoryIsNotWritable($directory, $message = '')
  387. {
  388. static::assertDirectoryNotIsWritable($directory, $message);
  389. }
  390. /**
  391. * @param string $filename
  392. * @param string $message
  393. *
  394. * @return void
  395. */
  396. public static function assertFileExists($filename, $message = '')
  397. {
  398. static::assertInternalType('string', $filename, $message);
  399. static::assertTrue(file_exists($filename), $message ?: "Failed asserting that $filename exists.");
  400. }
  401. /**
  402. * @param string $filename
  403. * @param string $message
  404. *
  405. * @return void
  406. */
  407. public static function assertFileNotExists($filename, $message = '')
  408. {
  409. static::assertInternalType('string', $filename, $message);
  410. static::assertFalse(file_exists($filename), $message ?: "Failed asserting that $filename does not exist.");
  411. }
  412. /**
  413. * @param string $filename
  414. * @param string $message
  415. *
  416. * @return void
  417. */
  418. public static function assertFileDoesNotExist($filename, $message = '')
  419. {
  420. static::assertFileNotExists($filename, $message);
  421. }
  422. /**
  423. * @param string $filename
  424. * @param string $message
  425. *
  426. * @return void
  427. */
  428. public static function assertFileIsReadable($filename, $message = '')
  429. {
  430. static::assertFileExists($filename, $message);
  431. static::assertIsReadable($filename, $message);
  432. }
  433. /**
  434. * @param string $filename
  435. * @param string $message
  436. *
  437. * @return void
  438. */
  439. public static function assertFileNotIsReadable($filename, $message = '')
  440. {
  441. static::assertFileExists($filename, $message);
  442. static::assertNotIsReadable($filename, $message);
  443. }
  444. /**
  445. * @param string $filename
  446. * @param string $message
  447. *
  448. * @return void
  449. */
  450. public static function assertFileIsNotReadable($filename, $message = '')
  451. {
  452. static::assertFileNotIsReadable($filename, $message);
  453. }
  454. /**
  455. * @param string $filename
  456. * @param string $message
  457. *
  458. * @return void
  459. */
  460. public static function assertFileIsWritable($filename, $message = '')
  461. {
  462. static::assertFileExists($filename, $message);
  463. static::assertIsWritable($filename, $message);
  464. }
  465. /**
  466. * @param string $filename
  467. * @param string $message
  468. *
  469. * @return void
  470. */
  471. public static function assertFileNotIsWritable($filename, $message = '')
  472. {
  473. static::assertFileExists($filename, $message);
  474. static::assertNotIsWritable($filename, $message);
  475. }
  476. /**
  477. * @param string $filename
  478. * @param string $message
  479. *
  480. * @return void
  481. */
  482. public static function assertFileIsNotWritable($filename, $message = '')
  483. {
  484. static::assertFileNotIsWritable($filename, $message);
  485. }
  486. /**
  487. * @param string $pattern
  488. * @param string $string
  489. * @param string $message
  490. *
  491. * @return void
  492. */
  493. public static function assertMatchesRegularExpression($pattern, $string, $message = '')
  494. {
  495. static::assertRegExp($pattern, $string, $message);
  496. }
  497. /**
  498. * @param string $pattern
  499. * @param string $string
  500. * @param string $message
  501. *
  502. * @return void
  503. */
  504. public static function assertDoesNotMatchRegularExpression($pattern, $string, $message = '')
  505. {
  506. static::assertNotRegExp($pattern, $string, $message);
  507. }
  508. }