StubTraitTest.php 2.68 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
<?php
require_once __DIR__ .'/ResetMocks.php';

class StubTraitTest extends \Codeception\PHPUnit\TestCase
{
    use ResetMocks;
    use \Codeception\Test\Feature\Stub;
    /**
     * @var DummyClass
     */
    protected $dummy;

    public function _setUp()
    {
        require_once $file = __DIR__. '/_data/DummyOverloadableClass.php';
        require_once $file = __DIR__. '/_data/DummyClass.php';
        $this->dummy = new DummyClass(true);
    }

    public function testMakeStubs()
    {
        $this->dummy = $this->make('DummyClass', ['helloWorld' => 'bye']);
        $this->assertEquals('bye', $this->dummy->helloWorld());
        $this->assertEquals('good bye', $this->dummy->goodByeWorld());

        $this->dummy = $this->makeEmpty('DummyClass', ['helloWorld' => 'bye']);
        $this->assertEquals('bye', $this->dummy->helloWorld());
        $this->assertNull($this->dummy->goodByeWorld());

        $this->dummy = $this->makeEmptyExcept('DummyClass', 'goodByeWorld', ['helloWorld' => 'bye']);
        $this->assertEquals('bye', $this->dummy->helloWorld());
        $this->assertEquals('good bye', $this->dummy->goodByeWorld());
        $this->assertNull($this->dummy->exceptionalMethod());
    }

    public function testConstructStubs()
    {
        $this->dummy = $this->construct('DummyClass', ['!'], ['helloWorld' => 'bye']);
        $this->assertEquals('constructed: !', $this->dummy->getCheckMe());
        $this->assertEquals('bye', $this->dummy->helloWorld());
        $this->assertEquals('good bye', $this->dummy->goodByeWorld());

        $this->dummy = $this->constructEmpty('DummyClass', ['!'], ['helloWorld' => 'bye']);
        $this->assertNull($this->dummy->getCheckMe());
        $this->assertEquals('bye', $this->dummy->helloWorld());
        $this->assertNull($this->dummy->goodByeWorld());

        $this->dummy = $this->constructEmptyExcept('DummyClass', 'getCheckMe', ['!'], ['helloWorld' => 'bye']);
        $this->assertEquals('constructed: !', $this->dummy->getCheckMe());
        $this->assertEquals('bye', $this->dummy->helloWorld());
        $this->assertNull($this->dummy->goodByeWorld());
        $this->assertNull($this->dummy->exceptionalMethod());
    }

    public function testMakeMocks()
    {
        $this->dummy = $this->make('DummyClass', [
            'helloWorld' => \Codeception\Stub\Expected::once()
        ]);
        $this->dummy->helloWorld();
        try {
            $this->dummy->helloWorld();
        } catch (Exception $e) {
            $this->assertTrue(strpos('was not expected to be called more than once', $e->getMessage()) >= 0, 'String contains');
            $this->resetMockObjects();
            return;
        }
        $this->fail('No exception thrown');
    }
}