AssertsTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. class AssertsTest extends \PHPUnit\Framework\TestCase
  3. {
  4. public function testAsserts()
  5. {
  6. $module = new \Codeception\Module\Asserts(make_container());
  7. $module->assertEquals(1, 1);
  8. $module->assertContains(1, [1, 2]);
  9. $module->assertSame(1, 1);
  10. $module->assertNotSame(1, '1');
  11. $module->assertRegExp('/^[\d]$/', '1');
  12. $module->assertNotRegExp('/^[a-z]$/', '1');
  13. $module->assertStringStartsWith('fo', 'foo');
  14. $module->assertStringStartsNotWith('ba', 'foo');
  15. $module->assertEmpty([]);
  16. $module->assertNotEmpty([1]);
  17. $module->assertNull(null);
  18. $module->assertNotNull('');
  19. $module->assertNotNull(false);
  20. $module->assertNotNull(0);
  21. $module->assertTrue(true);
  22. $module->assertFalse(false);
  23. $module->assertFileExists(__FILE__);
  24. $module->assertFileNotExists(__FILE__ . '.notExist');
  25. $module->assertInstanceOf('Exception', new Exception());
  26. $module->assertInternalType('integer', 5);
  27. $module->assertArrayHasKey('one', ['one' => 1, 'two' => 2]);
  28. $module->assertArraySubset(['foo' => [1]], ['foo' => [1, 2]]);
  29. $module->assertCount(3, [1, 2, 3]);
  30. }
  31. public function testExceptions()
  32. {
  33. $module = new \Codeception\Module\Asserts(make_container());
  34. $module->expectException('Exception', function () {
  35. throw new Exception;
  36. });
  37. $module->expectException(new Exception('here'), function () {
  38. throw new Exception('here');
  39. });
  40. $module->expectException(new Exception('here', 200), function () {
  41. throw new Exception('here', 200);
  42. });
  43. }
  44. /**
  45. * @expectedException PHPUnit\Framework\AssertionFailedError
  46. */
  47. public function testExceptionFails()
  48. {
  49. $module = new \Codeception\Module\Asserts(make_container());
  50. $module->expectException(new Exception('here', 200), function () {
  51. throw new Exception('here', 2);
  52. });
  53. }
  54. }