AssertTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php declare(strict_types=1);
  2. /**
  3. * This file is part of toolkit/stdlib.
  4. *
  5. * @author https://github.com/inhere
  6. * @link https://github.com/php-toolkit/stdlib
  7. * @license MIT
  8. */
  9. namespace Toolkit\StdlibTest\Str;
  10. use Toolkit\Stdlib\Helper\Assert;
  11. use Toolkit\StdlibTest\BaseLibTestCase;
  12. use const STDOUT;
  13. /**
  14. * class AssertTest
  15. *
  16. * @author inhere
  17. */
  18. class AssertTest extends BaseLibTestCase
  19. {
  20. public function testAssert_notEmpty(): void
  21. {
  22. $tests = [
  23. false,
  24. null,
  25. 0,
  26. 0.0,
  27. '',
  28. '0',
  29. [],
  30. ];
  31. foreach ($tests as $val) {
  32. $e = $this->tryCatchRun(fn () => Assert::notEmpty($val));
  33. $this->assertException($e, 'Expected a non-empty value');
  34. }
  35. $tests = [
  36. true,
  37. ];
  38. foreach ($tests as $val) {
  39. $e = $this->tryCatchRun(fn () => Assert::notEmpty($val));
  40. $this->assertException($e, 'NO ERROR', -1);
  41. }
  42. }
  43. public function testAssert_bool(): void
  44. {
  45. $e = $this->tryCatchRun(fn () => Assert::isFalse(false));
  46. $this->assertException($e, 'NO ERROR', -1);
  47. $e = $this->tryCatchRun(fn () => Assert::isFalse(true));
  48. $this->assertException($e, 'Expected a false value');
  49. $e = $this->tryCatchRun(fn () => Assert::isFalse(true, 'custom error'));
  50. $this->assertException($e, 'custom error');
  51. $e = $this->tryCatchRun(fn () => Assert::isTrue(true));
  52. $this->assertException($e, 'NO ERROR', -1);
  53. $e = $this->tryCatchRun(fn () => Assert::isTrue(false));
  54. $this->assertException($e, 'Expected a true value');
  55. $e = $this->tryCatchRun(fn () => Assert::isTrue(false, 'custom error'));
  56. $this->assertException($e, 'custom error');
  57. }
  58. public function testAssert_FS(): void
  59. {
  60. $e = $this->tryCatchRun(fn () => Assert::isFile(__DIR__ . '/AssertTest.php'));
  61. $this->assertException($e, 'NO ERROR', -1);
  62. $e = $this->tryCatchRun(fn () => Assert::isFile('./not-exists.file'));
  63. $this->assertException($e, 'No such file: ./not-exists.file');
  64. $e = $this->tryCatchRun(fn () => Assert::isDir(__DIR__));
  65. $this->assertException($e, 'NO ERROR', -1);
  66. $e = $this->tryCatchRun(fn () => Assert::isDir('./not-exists'));
  67. $this->assertException($e, 'No such dir: ./not-exists');
  68. $e = $this->tryCatchRun(fn () => Assert::isResource('invalid'));
  69. $this->assertException($e, 'Excepted an resource');
  70. $e = $this->tryCatchRun(fn () => Assert::isResource(STDOUT));
  71. $this->assertException($e, 'NO ERROR', -1);
  72. }
  73. }