AssertsTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. class AssertsTest extends \PHPUnit\Framework\TestCase
  3. {
  4. /** @var \Codeception\Module\Asserts */
  5. protected $module;
  6. public function setUp()
  7. {
  8. $this->module = new \Codeception\Module\Asserts(make_container());
  9. }
  10. public function testAsserts()
  11. {
  12. $this->module->assertEquals(1, 1);
  13. $this->module->assertContains(1, [1, 2]);
  14. $this->module->assertSame(1, 1);
  15. $this->module->assertNotSame(1, '1');
  16. $this->module->assertRegExp('/^[\d]$/', '1');
  17. $this->module->assertNotRegExp('/^[a-z]$/', '1');
  18. $this->module->assertStringStartsWith('fo', 'foo');
  19. $this->module->assertStringStartsNotWith('ba', 'foo');
  20. $this->module->assertEmpty([]);
  21. $this->module->assertNotEmpty([1]);
  22. $this->module->assertNull(null);
  23. $this->module->assertNotNull('');
  24. $this->module->assertNotNull(false);
  25. $this->module->assertNotNull(0);
  26. $this->module->assertTrue(true);
  27. $this->module->assertNotTrue(false);
  28. $this->module->assertNotTrue(null);
  29. $this->module->assertNotTrue('foo');
  30. $this->module->assertFalse(false);
  31. $this->module->assertNotFalse(true);
  32. $this->module->assertNotFalse(null);
  33. $this->module->assertNotFalse('foo');
  34. $this->module->assertFileExists(__FILE__);
  35. $this->module->assertFileNotExists(__FILE__ . '.notExist');
  36. $this->module->assertInstanceOf('Exception', new Exception());
  37. $this->module->assertInternalType('integer', 5);
  38. $this->module->assertArrayHasKey('one', ['one' => 1, 'two' => 2]);
  39. $this->module->assertArraySubset(['foo' => [1]], ['foo' => [1, 2]]);
  40. $this->module->assertCount(3, [1, 2, 3]);
  41. }
  42. public function testExceptions()
  43. {
  44. $this->module->expectException('Exception', function () {
  45. throw new Exception;
  46. });
  47. $this->module->expectException(new Exception('here'), function () {
  48. throw new Exception('here');
  49. });
  50. $this->module->expectException(new Exception('here', 200), function () {
  51. throw new Exception('here', 200);
  52. });
  53. }
  54. public function testExceptionFails()
  55. {
  56. $this->expectException(PHPUnit\Framework\AssertionFailedError::class);
  57. $this->module->expectException(new Exception('here', 200), function () {
  58. throw new Exception('here', 2);
  59. });
  60. }
  61. public function testOutputExceptionTimeWhenNothingCaught()
  62. {
  63. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  64. $this->expectExceptionMessageRegExp('/RuntimeException/');
  65. $this->module->expectException(RuntimeException::class, function () {
  66. });
  67. }
  68. public function testExpectThrowable()
  69. {
  70. $this->module->expectThrowable('Exception', function () {
  71. throw new Exception();
  72. });
  73. $this->module->expectThrowable(new Exception('here'), function () {
  74. throw new Exception('here');
  75. });
  76. $this->module->expectThrowable(new Exception('here', 200), function () {
  77. throw new Exception('here', 200);
  78. });
  79. }
  80. public function testExpectThrowableFailOnDifferentClass()
  81. {
  82. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  83. $this->module->expectThrowable(new RuntimeException(), function () {
  84. throw new Exception();
  85. });
  86. }
  87. public function testExpectThrowableFailOnDifferentMessage()
  88. {
  89. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  90. $this->module->expectThrowable(new Exception('foo', 200), function () {
  91. throw new Exception('bar', 200);
  92. });
  93. }
  94. public function testExpectThrowableFailOnDifferentCode()
  95. {
  96. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  97. $this->module->expectThrowable(new Exception('foobar', 200), function () {
  98. throw new Exception('foobar', 2);
  99. });
  100. }
  101. public function testExpectThrowableFailOnNothingCaught()
  102. {
  103. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  104. $this->expectExceptionMessageRegExp('/RuntimeException/');
  105. $this->module->expectThrowable(RuntimeException::class, function () {
  106. });
  107. }
  108. }