StubTraitTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. require_once __DIR__ .'/ResetMocks.php';
  3. class StubTraitTest extends \PHPUnit\Framework\TestCase
  4. {
  5. use ResetMocks;
  6. use \Codeception\Test\Feature\Stub;
  7. /**
  8. * @var DummyClass
  9. */
  10. protected $dummy;
  11. public function setUp(): void
  12. {
  13. require_once $file = __DIR__. '/_data/DummyOverloadableClass.php';
  14. require_once $file = __DIR__. '/_data/DummyClass.php';
  15. $this->dummy = new DummyClass(true);
  16. }
  17. public function testMakeStubs()
  18. {
  19. $this->dummy = $this->make('DummyClass', ['helloWorld' => 'bye']);
  20. $this->assertEquals('bye', $this->dummy->helloWorld());
  21. $this->assertEquals('good bye', $this->dummy->goodByeWorld());
  22. $this->dummy = $this->makeEmpty('DummyClass', ['helloWorld' => 'bye']);
  23. $this->assertEquals('bye', $this->dummy->helloWorld());
  24. $this->assertNull($this->dummy->goodByeWorld());
  25. $this->dummy = $this->makeEmptyExcept('DummyClass', 'goodByeWorld', ['helloWorld' => 'bye']);
  26. $this->assertEquals('bye', $this->dummy->helloWorld());
  27. $this->assertEquals('good bye', $this->dummy->goodByeWorld());
  28. $this->assertNull($this->dummy->exceptionalMethod());
  29. }
  30. public function testConstructStubs()
  31. {
  32. $this->dummy = $this->construct('DummyClass', ['!'], ['helloWorld' => 'bye']);
  33. $this->assertEquals('constructed: !', $this->dummy->getCheckMe());
  34. $this->assertEquals('bye', $this->dummy->helloWorld());
  35. $this->assertEquals('good bye', $this->dummy->goodByeWorld());
  36. $this->dummy = $this->constructEmpty('DummyClass', ['!'], ['helloWorld' => 'bye']);
  37. $this->assertNull($this->dummy->getCheckMe());
  38. $this->assertEquals('bye', $this->dummy->helloWorld());
  39. $this->assertNull($this->dummy->goodByeWorld());
  40. $this->dummy = $this->constructEmptyExcept('DummyClass', 'getCheckMe', ['!'], ['helloWorld' => 'bye']);
  41. $this->assertEquals('constructed: !', $this->dummy->getCheckMe());
  42. $this->assertEquals('bye', $this->dummy->helloWorld());
  43. $this->assertNull($this->dummy->goodByeWorld());
  44. $this->assertNull($this->dummy->exceptionalMethod());
  45. }
  46. public function testMakeMocks()
  47. {
  48. $this->dummy = $this->make('DummyClass', [
  49. 'helloWorld' => \Codeception\Stub\Expected::once()
  50. ]);
  51. $this->dummy->helloWorld();
  52. try {
  53. $this->dummy->helloWorld();
  54. } catch (Exception $e) {
  55. $this->assertTrue(strpos('was not expected to be called more than once', $e->getMessage()) >= 0, 'String contains');
  56. $this->resetMockObjects();
  57. return;
  58. }
  59. $this->fail('No exception thrown');
  60. }
  61. }