PageRenderTime 21ms CodeModel.GetById 14ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/vendor/phpunit/phpunit-mock-objects/tests/GeneratorTest.php

https://gitlab.com/jhonn/rest
PHP | 176 lines | 96 code | 23 blank | 57 comment | 1 complexity | abd9dd2e49d0444abfc3990bb3c2e301 MD5 | raw file
  1<?php
  2class Framework_MockObject_GeneratorTest extends PHPUnit_Framework_TestCase
  3{
  4    /**
  5     * @var PHPUnit_Framework_MockObject_Generator
  6     */
  7    protected $generator;
  8
  9    protected function setUp()
 10    {
 11        $this->generator = new PHPUnit_Framework_MockObject_Generator;
 12    }
 13
 14    /**
 15     * @covers PHPUnit_Framework_MockObject_Generator::getMock
 16     * @expectedException PHPUnit_Framework_Exception
 17     */
 18    public function testGetMockFailsWhenInvalidFunctionNameIsPassedInAsAFunctionToMock()
 19    {
 20        $this->generator->getMock('StdClass', array(0));
 21    }
 22
 23    /**
 24     * @covers PHPUnit_Framework_MockObject_Generator::getMock
 25     */
 26    public function testGetMockCanCreateNonExistingFunctions()
 27    {
 28        $mock = $this->generator->getMock('StdClass', array('testFunction'));
 29        $this->assertTrue(method_exists($mock, 'testFunction'));
 30    }
 31
 32    /**
 33     * @covers PHPUnit_Framework_MockObject_Generator::getMock
 34     * @expectedException PHPUnit_Framework_MockObject_RuntimeException
 35     * @expectedExceptionMessage duplicates: "foo, foo"
 36     */
 37    public function testGetMockGeneratorFails()
 38    {
 39        $mock = $this->generator->getMock('StdClass', array('foo', 'foo'));
 40    }
 41
 42    /**
 43     * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
 44     */
 45    public function testGetMockForAbstractClassDoesNotFailWhenFakingInterfaces()
 46    {
 47        $mock = $this->generator->getMockForAbstractClass('Countable');
 48        $this->assertTrue(method_exists($mock, 'count'));
 49    }
 50
 51    /**
 52     * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
 53     */
 54    public function testGetMockForAbstractClassStubbingAbstractClass()
 55    {
 56        $mock = $this->generator->getMockForAbstractClass('AbstractMockTestClass');
 57        $this->assertTrue(method_exists($mock, 'doSomething'));
 58    }
 59
 60    /**
 61     * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
 62     */
 63    public function testGetMockForAbstractClassWithNonExistentMethods()
 64    {
 65        $mock = $this->generator->getMockForAbstractClass(
 66            'AbstractMockTestClass', array(), '',  true,
 67            true, true, array('nonexistentMethod')
 68        );
 69
 70        $this->assertTrue(method_exists($mock, 'nonexistentMethod'));
 71        $this->assertTrue(method_exists($mock, 'doSomething'));
 72    }
 73
 74    /**
 75     * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
 76     */
 77    public function testGetMockForAbstractClassShouldCreateStubsOnlyForAbstractMethodWhenNoMethodsWereInformed()
 78    {
 79        $mock = $this->generator->getMockForAbstractClass('AbstractMockTestClass');
 80
 81        $mock->expects($this->any())
 82             ->method('doSomething')
 83             ->willReturn('testing');
 84
 85        $this->assertEquals('testing', $mock->doSomething());
 86        $this->assertEquals(1, $mock->returnAnything());
 87    }
 88
 89    /**
 90     * @dataProvider getMockForAbstractClassExpectsInvalidArgumentExceptionDataprovider
 91     * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
 92     * @expectedException PHPUnit_Framework_Exception
 93     */
 94    public function testGetMockForAbstractClassExpectingInvalidArgumentException($className, $mockClassName)
 95    {
 96        $mock = $this->generator->getMockForAbstractClass($className, array(), $mockClassName);
 97    }
 98
 99    /**
100     * @covers PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass
101     * @expectedException PHPUnit_Framework_MockObject_RuntimeException
102     */
103    public function testGetMockForAbstractClassAnstractClassDoesNotExist()
104    {
105        $mock = $this->generator->getMockForAbstractClass('Tux');
106    }
107
108    /**
109     * Dataprovider for test "testGetMockForAbstractClassExpectingInvalidArgumentException"
110     */
111    public static function getMockForAbstractClassExpectsInvalidArgumentExceptionDataprovider()
112    {
113        return array(
114            'className not a string' => array(array(), ''),
115            'mockClassName not a string' => array('Countable', new StdClass),
116        );
117    }
118
119    /**
120     * @covers PHPUnit_Framework_MockObject_Generator::getMockForTrait
121     * @requires PHP 5.4.0
122     */
123    public function testGetMockForTraitWithNonExistentMethodsAndNonAbstractMethods()
124    {
125        $mock = $this->generator->getMockForTrait(
126            'AbstractTrait', array(), '',  true,
127            true, true, array('nonexistentMethod')
128        );
129
130        $this->assertTrue(method_exists($mock, 'nonexistentMethod'));
131        $this->assertTrue(method_exists($mock, 'doSomething'));
132        $this->assertTrue($mock->mockableMethod());
133        $this->assertTrue($mock->anotherMockableMethod());
134    }
135
136    /**
137     * @covers   PHPUnit_Framework_MockObject_Generator::getMockForTrait
138     * @requires PHP 5.4.0
139     */
140    public function testGetMockForTraitStubbingAbstractMethod()
141    {
142        $mock = $this->generator->getMockForTrait('AbstractTrait');
143        $this->assertTrue(method_exists($mock, 'doSomething'));
144    }
145
146    /**
147     * @requires PHP 5.4.0
148     */
149    public function testGetMockForSingletonWithReflectionSuccess()
150    {
151        // Probably, this should be moved to tests/autoload.php
152        require_once __DIR__ . '/_fixture/SingletonClass.php';
153
154        $mock = $this->generator->getMock('SingletonClass', array('doSomething'), array(), '', false);
155        $this->assertInstanceOf('SingletonClass', $mock);
156    }
157
158    /**
159     * Same as "testGetMockForSingletonWithReflectionSuccess", but we expect
160     * warning for PHP < 5.4.0 since PHPUnit will try to execute private __wakeup
161     * on unserialize
162     */
163    public function testGetMockForSingletonWithUnserializeFail()
164    {
165        if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
166            $this->markTestSkipped('Only for PHP < 5.4.0');
167        }
168
169        $this->setExpectedException('PHPUnit_Framework_MockObject_RuntimeException');
170
171        // Probably, this should be moved to tests/autoload.php
172        require_once __DIR__ . '/_fixture/SingletonClass.php';
173
174        $mock = $this->generator->getMock('SingletonClass', array('doSomething'), array(), '', false);
175    }
176}