Include Vendor

This commit is contained in:
Mathieu Lagace
2018-06-27 20:31:54 -04:00
parent d0807a7cf5
commit 5fa3c85b97
6624 changed files with 728653 additions and 1 deletions

View File

@@ -0,0 +1,66 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
/*
* Set error reporting to the level to which Mockery code must comply.
*/
error_reporting(E_ALL);
function isAbsolutePath($path)
{
$windowsPattern = '~^[A-Z]:[\\/]~i';
return ($path[0] === DIRECTORY_SEPARATOR) || (preg_match($windowsPattern, $path) === 1);
}
$root = realpath(dirname(dirname(__FILE__)));
$composerVendorDirectory = getenv("COMPOSER_VENDOR_DIR") ?: "vendor";
if (!isAbsolutePath($composerVendorDirectory)) {
$composerVendorDirectory = $root . DIRECTORY_SEPARATOR . $composerVendorDirectory;
}
/**
* Check that composer installation was done
*/
$autoloadPath = $composerVendorDirectory . DIRECTORY_SEPARATOR . 'autoload.php';
if (!file_exists($autoloadPath)) {
throw new Exception(
'Please run "php composer.phar install" in root directory '
. 'to setup unit test dependencies before running the tests'
);
}
require_once $autoloadPath;
$hamcrestRelativePath = 'hamcrest/hamcrest-php/hamcrest/Hamcrest.php';
if (DIRECTORY_SEPARATOR !== '/') {
$hamcrestRelativePath = str_replace('/', DIRECTORY_SEPARATOR, $hamcrestRelativePath);
}
$hamcrestPath = $composerVendorDirectory . DIRECTORY_SEPARATOR . $hamcrestRelativePath;
require_once $hamcrestPath;
Mockery::globalHelpers();
/*
* Unset global variables that are no longer needed.
*/
unset($root, $autoloadPath, $hamcrestPath, $composerVendorDirectory);

View File

@@ -0,0 +1,63 @@
<?php
namespace test\Mockery\Adapter\Phpunit;
use Mockery as m;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Mockery\Exception\BadMethodCallException;
class BaseClassStub
{
use MockeryPHPUnitIntegration;
public function finish()
{
$this->checkMockeryExceptions();
}
public function markAsRisky()
{
}
};
class MockeryPHPUnitIntegrationTest extends MockeryTestCase
{
/**
* @test
* @requires PHPUnit 5.7.6
*/
public function it_marks_a_passing_test_as_risky_if_we_threw_exceptions()
{
$mock = mock();
try {
$mock->foobar();
} catch (\Exception $e) {
// exception swallowed...
}
$test = spy(BaseClassStub::class)->makePartial();
$test->finish();
$test->shouldHaveReceived()->markAsRisky();
}
/**
* @test
* @requires PHPUnit 5.7.6
*/
public function the_user_can_manually_dismiss_an_exception_to_avoid_the_risky_test()
{
$mock = mock();
try {
$mock->foobar();
} catch (BadMethodCallException $e) {
$e->dismiss();
}
$test = spy(BaseClassStub::class)->makePartial();
$test->finish();
$test->shouldNotHaveReceived()->markAsRisky();
}
}

View File

@@ -0,0 +1,103 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/blob/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace tests\Mockery\Adapter\Phpunit;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestResult;
use Mockery\Adapter\Phpunit\TestListener;
use PHPUnit\Runner\BaseTestRunner;
class Mockery_Adapter_Phpunit_TestListenerTest extends TestCase
{
protected function setUp()
{
/**
* Skip all tests here if PHPUnit is less than 6.0.0
*/
if (class_exists('\PHPUnit\Runner\Version')) {
$ver = \PHPUnit\Runner\Version::series();
} else {
$ver = \PHPUnit_Runner_Version::series();
}
if (intval($ver) < 6) {
$this->markTestSkipped('The TestListener is only supported with PHPUnit 6+.');
return;
}
// We intentionally test the static container here. That is what the
// listener will check.
$this->container = \Mockery::getContainer();
$this->listener = new TestListener();
$this->testResult = new TestResult();
$this->test = new EmptyTestCase();
$this->test->setTestResultObject($this->testResult);
$this->testResult->addListener($this->listener);
$this->assertTrue($this->testResult->wasSuccessful(), 'sanity check: empty test results should be considered successful');
}
public function testSuccessOnClose()
{
$mock = $this->container->mock();
$mock->shouldReceive('bar')->once();
$mock->bar();
// This is what MockeryPHPUnitIntegration and MockeryTestCase trait
// will do. We intentionally call the static close method.
$this->test->addToAssertionCount($this->container->mockery_getExpectationCount());
\Mockery::close();
$this->listener->endTest($this->test, 0);
$this->assertTrue($this->testResult->wasSuccessful(), 'expected test result to indicate success');
}
public function testFailureOnMissingClose()
{
$mock = $this->container->mock();
$mock->shouldReceive('bar')->once();
$this->listener->endTest($this->test, 0);
$this->assertFalse($this->testResult->wasSuccessful(), 'expected test result to indicate failure');
// Satisfy the expectation and close the global container now so we
// don't taint the environment.
$mock->bar();
\Mockery::close();
}
public function testMockeryIsAddedToBlacklist()
{
$suite = \Mockery::mock(\PHPUnit\Framework\TestSuite::class);
$this->assertArrayNotHasKey(\Mockery::class, \PHPUnit\Util\Blacklist::$blacklistedClassNames);
$this->listener->startTestSuite($suite);
$this->assertSame(1, \PHPUnit\Util\Blacklist::$blacklistedClassNames[\Mockery::class]);
}
}
class EmptyTestCase extends TestCase
{
public function getStatus()
{
return BaseTestRunner::STATUS_PASSED;
}
}

View File

@@ -0,0 +1,119 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/blob/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
use Mockery\Adapter\Phpunit\MockeryTestCase;
/**
* Ad-hoc unit tests for various scenarios reported by users
*/
class Mockery_AdhocTest extends MockeryTestCase
{
public function setup()
{
$this->container = new \Mockery\Container(\Mockery::getDefaultGenerator(), \Mockery::getDefaultLoader());
}
public function teardown()
{
$this->container->mockery_close();
}
public function testSimplestMockCreation()
{
$m = $this->container->mock('MockeryTest_NameOfExistingClass');
$this->assertInstanceOf(MockeryTest_NameOfExistingClass::class, $m);
}
public function testMockeryInterfaceForClass()
{
$m = $this->container->mock('SplFileInfo');
$this->assertInstanceOf(\Mockery\MockInterface::class, $m);
}
public function testMockeryInterfaceForNonExistingClass()
{
$m = $this->container->mock('ABC_IDontExist');
$this->assertInstanceOf(\Mockery\MockInterface::class, $m);
}
public function testMockeryInterfaceForInterface()
{
$m = $this->container->mock('MockeryTest_NameOfInterface');
$this->assertInstanceOf(\Mockery\MockInterface::class, $m);
}
public function testMockeryInterfaceForAbstract()
{
$m = $this->container->mock('MockeryTest_NameOfAbstract');
$this->assertInstanceOf(\Mockery\MockInterface::class, $m);
}
public function testInvalidCountExceptionThrowsRuntimeExceptionOnIllegalComparativeSymbol()
{
$this->expectException('Mockery\Exception\RuntimeException');
$e = new \Mockery\Exception\InvalidCountException;
$e->setExpectedCountComparative('X');
}
public function testMockeryConstructAndDestructIsNotCalled()
{
MockeryTest_NameOfExistingClassWithDestructor::$isDestructorWasCalled = false;
// We pass no arguments in constructor, so it's not being called. Then destructor shouldn't be called too.
$this->container->mock('MockeryTest_NameOfExistingClassWithDestructor');
// Clear references to trigger destructor
$this->container->mockery_close();
$this->assertFalse(MockeryTest_NameOfExistingClassWithDestructor::$isDestructorWasCalled);
}
public function testMockeryConstructAndDestructIsCalled()
{
MockeryTest_NameOfExistingClassWithDestructor::$isDestructorWasCalled = false;
$this->container->mock('MockeryTest_NameOfExistingClassWithDestructor', array());
// Clear references to trigger destructor
$this->container->mockery_close();
$this->assertTrue(MockeryTest_NameOfExistingClassWithDestructor::$isDestructorWasCalled);
}
}
class MockeryTest_NameOfExistingClass
{
}
interface MockeryTest_NameOfInterface
{
public function foo();
}
abstract class MockeryTest_NameOfAbstract
{
abstract public function foo();
}
class MockeryTest_NameOfExistingClassWithDestructor
{
public static $isDestructorWasCalled = false;
public function __destruct()
{
self::$isDestructorWasCalled = true;
}
}

View File

@@ -0,0 +1,111 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery;
use Mockery as m;
use Mockery\Spy;
use Mockery\Exception\InvalidCountException;
use PHPUnit\Framework\TestCase;
class AllowsExpectsSyntaxTest extends TestCase
{
/** @test */
public function allowsSetsUpMethodStub()
{
$stub = m::mock();
$stub->allows()->foo(123)->andReturns(456);
$this->assertEquals(456, $stub->foo(123));
}
/** @test */
public function allowsCanTakeAnArrayOfCalls()
{
$stub = m::mock();
$stub->allows([
"foo" => "bar",
"bar" => "baz",
]);
$this->assertEquals("bar", $stub->foo());
$this->assertEquals("baz", $stub->bar());
}
/** @test */
public function allowsCanTakeAString()
{
$stub = m::mock();
$stub->allows("foo")->andReturns("bar");
$this->assertEquals("bar", $stub->foo());
}
/** @test */
public function expects_can_optionally_match_on_any_arguments()
{
$mock = m::mock();
$mock->allows()->foo()->withAnyArgs()->andReturns(123);
$this->assertEquals(123, $mock->foo(456, 789));
}
/** @test */
public function expects_can_take_a_string()
{
$mock = m::mock();
$mock->expects("foo")->andReturns(123);
$this->assertEquals(123, $mock->foo(456, 789));
}
/** @test */
public function expectsSetsUpExpectationOfOneCall()
{
$mock = m::mock();
$mock->expects()->foo(123);
$this->expectException("Mockery\Exception\InvalidCountException");
m::close();
}
/** @test */
public function callVerificationCountCanBeOverridenAfterExpectsThrowsExceptionWhenIncorrectNumberOfCalls()
{
$mock = m::mock();
$mock->expects()->foo(123)->twice();
$mock->foo(123);
$this->expectException("Mockery\Exception\InvalidCountException");
m::close();
}
/** @test */
public function callVerificationCountCanBeOverridenAfterExpects()
{
$mock = m::mock();
$mock->expects()->foo(123)->twice();
$mock->foo(123);
$mock->foo(123);
m::close();
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,204 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
require_once __DIR__.'/DummyClasses/DemeterChain.php';
}
use Mockery\Adapter\Phpunit\MockeryTestCase;
class DemeterChainTest extends MockeryTestCase
{
/** @var Mockery\Mock $this->mock */
private $mock;
public function setUp()
{
$this->mock = $this->mock = Mockery::mock()->shouldIgnoreMissing();
}
public function tearDown()
{
$this->mock->mockery_getContainer()->mockery_close();
}
public function testTwoChains()
{
$this->mock->shouldReceive('getElement->getFirst')
->once()
->andReturn('something');
$this->mock->shouldReceive('getElement->getSecond')
->once()
->andReturn('somethingElse');
$this->assertEquals(
'something',
$this->mock->getElement()->getFirst()
);
$this->assertEquals(
'somethingElse',
$this->mock->getElement()->getSecond()
);
$this->mock->mockery_getContainer()->mockery_close();
}
public function testTwoChainsWithExpectedParameters()
{
$this->mock->shouldReceive('getElement->getFirst')
->once()
->with('parameter')
->andReturn('something');
$this->mock->shouldReceive('getElement->getSecond')
->once()
->with('secondParameter')
->andReturn('somethingElse');
$this->assertEquals(
'something',
$this->mock->getElement()->getFirst('parameter')
);
$this->assertEquals(
'somethingElse',
$this->mock->getElement()->getSecond('secondParameter')
);
$this->mock->mockery_getContainer()->mockery_close();
}
public function testThreeChains()
{
$this->mock->shouldReceive('getElement->getFirst')
->once()
->andReturn('something');
$this->mock->shouldReceive('getElement->getSecond')
->once()
->andReturn('somethingElse');
$this->assertEquals(
'something',
$this->mock->getElement()->getFirst()
);
$this->assertEquals(
'somethingElse',
$this->mock->getElement()->getSecond()
);
$this->mock->shouldReceive('getElement->getFirst')
->once()
->andReturn('somethingNew');
$this->assertEquals(
'somethingNew',
$this->mock->getElement()->getFirst()
);
}
public function testManyChains()
{
$this->mock->shouldReceive('getElements->getFirst')
->once()
->andReturn('something');
$this->mock->shouldReceive('getElements->getSecond')
->once()
->andReturn('somethingElse');
$this->mock->getElements()->getFirst();
$this->mock->getElements()->getSecond();
}
public function testTwoNotRelatedChains()
{
$this->mock->shouldReceive('getElement->getFirst')
->once()
->andReturn('something');
$this->mock->shouldReceive('getOtherElement->getSecond')
->once()
->andReturn('somethingElse');
$this->assertEquals(
'somethingElse',
$this->mock->getOtherElement()->getSecond()
);
$this->assertEquals(
'something',
$this->mock->getElement()->getFirst()
);
}
public function testDemeterChain()
{
$this->mock->shouldReceive('getElement->getFirst')
->once()
->andReturn('somethingElse');
$this->assertEquals('somethingElse', $this->mock->getElement()->getFirst());
}
public function testMultiLevelDemeterChain()
{
$this->mock->shouldReceive('levelOne->levelTwo->getFirst')
->andReturn('first');
$this->mock->shouldReceive('levelOne->levelTwo->getSecond')
->andReturn('second');
$this->assertEquals(
'second',
$this->mock->levelOne()->levelTwo()->getSecond()
);
$this->assertEquals(
'first',
$this->mock->levelOne()->levelTwo()->getFirst()
);
}
public function testSimilarDemeterChainsOnDifferentClasses()
{
$mock1 = Mockery::mock('overload:mock1');
$mock1->shouldReceive('select->some->data')->andReturn(1);
$mock1->shouldReceive('select->some->other->data')->andReturn(2);
$mock2 = Mockery::mock('overload:mock2');
$mock2->shouldReceive('select->some->data')->andReturn(3);
$mock2->shouldReceive('select->some->other->data')->andReturn(4);
$this->assertEquals(1, mock1::select()->some()->data());
$this->assertEquals(2, mock1::select()->some()->other()->data());
$this->assertEquals(3, mock2::select()->some()->data());
$this->assertEquals(4, mock2::select()->some()->other()->data());
}
/**
* @requires PHP 7.0.0
*/
public function testDemeterChainsWithClassReturnTypeHints()
{
$a = \Mockery::mock(\DemeterChain\A::class);
$a->shouldReceive('foo->bar->baz')->andReturn(new stdClass);
$m = new \DemeterChain\Main();
$result = $m->callDemeter($a);
$this->assertInstanceOf(stdClass::class, $result);
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace DemeterChain;
class C
{
public function baz(): \stdClass
{
return new \stdClass();
}
}
class B
{
public function bar(): C
{
return new C();
}
}
class A
{
public function foo(): B
{
return new B();
}
}
class Main
{
public function callDemeter(A $a)
{
return $a->foo()->bar()->baz();
}
}

View File

@@ -0,0 +1,35 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace Nature
{
class Plant
{
}
}
namespace
{
abstract class Gardener
{
abstract public function water(Nature\Plant $plant);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,30 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery\Fixtures;
class ClassWithAllLowerCaseMethod
{
public function userexpectscamelcasemethod()
{
return 'real';
}
}

View File

@@ -0,0 +1,29 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery\Fixtures;
class ClassWithConstants
{
const FOO = 'bar';
const X = 1;
}

View File

@@ -0,0 +1,29 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery\Fixtures;
class MethodWithIterableTypeHints
{
public function foo(iterable $bar): iterable
{
}
}

View File

@@ -0,0 +1,67 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery\Fixtures;
use Mockery\Adapter\Phpunit\MockeryTestCase;
class MethodWithNullableReturnType extends MockeryTestCase
{
public function nonNullablePrimitive() : string
{
return 'test';
}
public function nullablePrimitive() : ?string
{
return null;
}
public function nonNullableSelf() : self
{
return $this;
}
public function nullableSelf() : ?self
{
return null;
}
public function nonNullableClass() : MethodWithNullableReturnType
{
return $this;
}
public function nullableClass() : ?MethodWithNullableReturnType
{
return null;
}
public function nullableInt() : ?int
{
return null;
}
public function nullableString() : ?string
{
return null;
}
}

View File

@@ -0,0 +1,37 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery\Fixtures;
class MethodWithNullableTypedParameter
{
public function foo(?string $bar)
{
}
public function bar(string $bar = null)
{
}
public function baz(?string $bar = null)
{
}
}

View File

@@ -0,0 +1,33 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery\Fixtures;
class MethodWithParametersWithDefaultValues
{
public function foo($bar = null)
{
}
public function bar(string $bar = null)
{
}
}

View File

@@ -0,0 +1,29 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery\Fixtures;
class MethodWithVoidReturnType
{
public function foo(): void
{
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Mockery\Fixtures;
class SemiReservedWordsAsMethods
{
function callable() {}
function class() {}
function trait() {}
function extends() {}
function implements() {}
function static() {}
function abstract() {}
function final() {}
function public() {}
function protected() {}
function private() {}
function const() {}
function enddeclare() {}
function endfor() {}
function endforeach() {}
function endif() {}
function endwhile() {}
function and() {}
function global() {}
function goto() {}
function instanceof() {}
function insteadof() {}
function interface() {}
function namespace() {}
function new() {}
function or() {}
function xor() {}
function try() {}
function use() {}
function var() {}
function exit() {}
function list() {}
function clone() {}
function include() {}
function include_once() {}
function throw() {}
function array() {}
function print() {}
function echo() {}
function require() {}
function require_once() {}
function return() {}
function else() {}
function elseif() {}
function default() {}
function break() {}
function continue() {}
function switch() {}
function yield() {}
function function() {}
function if() {}
function endswitch() {}
function finally() {}
function for() {}
function foreach() {}
function declare() {}
function case() {}
function do() {}
function while() {}
function as() {}
function catch() {}
function die() {}
function self() {}
function parent() {}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace Mockery;
use Mockery\Generator\DefinedTargetClass;
use PHPUnit\Framework\TestCase;
class DefinedTargetClassTest extends TestCase
{
/** @test */
public function it_knows_if_one_of_its_ancestors_is_internal()
{
$target = new DefinedTargetClass(new \ReflectionClass("ArrayObject"));
$this->assertTrue($target->hasInternalAncestor());
$target = new DefinedTargetClass(new \ReflectionClass("Mockery\MockeryTest_ClassThatExtendsArrayObject"));
$this->assertTrue($target->hasInternalAncestor());
$target = new DefinedTargetClass(new \ReflectionClass("Mockery\DefinedTargetClassTest"));
$this->assertFalse($target->hasInternalAncestor());
}
}
class MockeryTest_ClassThatExtendsArrayObject extends \ArrayObject
{
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace tests\Mockery\Generator;
use Mockery as m;
use Mockery\Generator\MockConfigurationBuilder;
use PHPUnit\Framework\TestCase;
class MockConfigurationBuilderTest extends TestCase
{
/**
* @test
*/
public function reservedWordsAreBlackListedByDefault()
{
$builder = new MockConfigurationBuilder;
$this->assertContains('__halt_compiler', $builder->getMockConfiguration()->getBlackListedMethods());
// need a builtin for this
$this->markTestSkipped("Need a builtin class with a method that is a reserved word");
}
/**
* @test
*/
public function magicMethodsAreBlackListedByDefault()
{
$builder = new MockConfigurationBuilder;
$builder->addTarget(ClassWithMagicCall::class);
$methods = $builder->getMockConfiguration()->getMethodsToMock();
$this->assertCount(1, $methods);
$this->assertEquals("foo", $methods[0]->getName());
}
/** @test */
public function xdebugs_debug_info_is_black_listed_by_default()
{
$builder = new MockConfigurationBuilder;
$builder->addTarget(ClassWithDebugInfo::class);
$methods = $builder->getMockConfiguration()->getMethodsToMock();
$this->assertCount(1, $methods);
$this->assertEquals("foo", $methods[0]->getName());
}
}
class ClassWithMagicCall
{
public function foo()
{
}
public function __call($method, $args)
{
}
}
class ClassWithDebugInfo
{
public function foo()
{
}
public function __debugInfo()
{
}
}

View File

@@ -0,0 +1,218 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace Mockery\Generator;
use PHPUnit\Framework\TestCase;
class MockConfigurationTest extends TestCase
{
/**
* @test
*/
public function blackListedMethodsShouldNotBeInListToBeMocked()
{
$config = new MockConfiguration(array("Mockery\Generator\\TestSubject"), array("foo"));
$methods = $config->getMethodsToMock();
$this->assertCount(1, $methods);
$this->assertEquals("bar", $methods[0]->getName());
}
/**
* @test
*/
public function blackListsAreCaseInsensitive()
{
$config = new MockConfiguration(array("Mockery\Generator\\TestSubject"), array("FOO"));
$methods = $config->getMethodsToMock();
$this->assertCount(1, $methods);
$this->assertEquals("bar", $methods[0]->getName());
}
/**
* @test
*/
public function onlyWhiteListedMethodsShouldBeInListToBeMocked()
{
$config = new MockConfiguration(array("Mockery\Generator\\TestSubject"), array(), array('foo'));
$methods = $config->getMethodsToMock();
$this->assertCount(1, $methods);
$this->assertEquals("foo", $methods[0]->getName());
}
/**
* @test
*/
public function whitelistOverRulesBlackList()
{
$config = new MockConfiguration(array("Mockery\Generator\\TestSubject"), array("foo"), array("foo"));
$methods = $config->getMethodsToMock();
$this->assertCount(1, $methods);
$this->assertEquals("foo", $methods[0]->getName());
}
/**
* @test
*/
public function whiteListsAreCaseInsensitive()
{
$config = new MockConfiguration(array("Mockery\Generator\\TestSubject"), array(), array("FOO"));
$methods = $config->getMethodsToMock();
$this->assertCount(1, $methods);
$this->assertEquals("foo", $methods[0]->getName());
}
/**
* @test
*/
public function finalMethodsAreExcluded()
{
$config = new MockConfiguration(array("Mockery\Generator\\ClassWithFinalMethod"));
$methods = $config->getMethodsToMock();
$this->assertCount(1, $methods);
$this->assertEquals("bar", $methods[0]->getName());
}
/**
* @test
*/
public function shouldIncludeMethodsFromAllTargets()
{
$config = new MockConfiguration(array("Mockery\\Generator\\TestInterface", "Mockery\\Generator\\TestInterface2"));
$methods = $config->getMethodsToMock();
$this->assertCount(2, $methods);
}
/**
* @test
* @expectedException Mockery\Exception
*/
public function shouldThrowIfTargetClassIsFinal()
{
$config = new MockConfiguration(array("Mockery\\Generator\\TestFinal"));
$config->getTargetClass();
}
/**
* @test
*/
public function shouldTargetIteratorAggregateIfTryingToMockTraversable()
{
$config = new MockConfiguration(array("\\Traversable"));
$interfaces = $config->getTargetInterfaces();
$this->assertCount(1, $interfaces);
$first = array_shift($interfaces);
$this->assertEquals("IteratorAggregate", $first->getName());
}
/**
* @test
*/
public function shouldTargetIteratorAggregateIfTraversableInTargetsTree()
{
$config = new MockConfiguration(array("Mockery\Generator\TestTraversableInterface"));
$interfaces = $config->getTargetInterfaces();
$this->assertCount(2, $interfaces);
$this->assertEquals("IteratorAggregate", $interfaces[0]->getName());
$this->assertEquals("Mockery\Generator\TestTraversableInterface", $interfaces[1]->getName());
}
/**
* @test
*/
public function shouldBringIteratorToHeadOfTargetListIfTraversablePresent()
{
$config = new MockConfiguration(array("Mockery\Generator\TestTraversableInterface2"));
$interfaces = $config->getTargetInterfaces();
$this->assertCount(2, $interfaces);
$this->assertEquals("Iterator", $interfaces[0]->getName());
$this->assertEquals("Mockery\Generator\TestTraversableInterface2", $interfaces[1]->getName());
}
/**
* @test
*/
public function shouldBringIteratorAggregateToHeadOfTargetListIfTraversablePresent()
{
$config = new MockConfiguration(array("Mockery\Generator\TestTraversableInterface3"));
$interfaces = $config->getTargetInterfaces();
$this->assertCount(2, $interfaces);
$this->assertEquals("IteratorAggregate", $interfaces[0]->getName());
$this->assertEquals("Mockery\Generator\TestTraversableInterface3", $interfaces[1]->getName());
}
}
interface TestTraversableInterface extends \Traversable
{
}
interface TestTraversableInterface2 extends \Traversable, \Iterator
{
}
interface TestTraversableInterface3 extends \Traversable, \IteratorAggregate
{
}
final class TestFinal
{
}
interface TestInterface
{
public function foo();
}
interface TestInterface2
{
public function bar();
}
class TestSubject
{
public function foo()
{
}
public function bar()
{
}
}
class ClassWithFinalMethod
{
final public function foo()
{
}
public function bar()
{
}
}

View File

@@ -0,0 +1,59 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace Mockery\Test\Generator\StringManipulation\Pass;
use Mockery as m;
use Mockery\Generator\StringManipulation\Pass\CallTypeHintPass;
use PHPUnit\Framework\TestCase;
class CallTypeHintPassTest extends TestCase
{
const CODE = ' public function __call($method, array $args) {}
public static function __callStatic($method, array $args) {}
';
/**
* @test
*/
public function shouldRemoveCallTypeHintIfRequired()
{
$pass = new CallTypeHintPass;
$config = m::mock("Mockery\Generator\MockConfiguration", array(
"requiresCallTypeHintRemoval" => true,
))->makePartial();
$code = $pass->apply(static::CODE, $config);
$this->assertContains('__call($method, $args)', $code);
}
/**
* @test
*/
public function shouldRemoveCallStaticTypeHintIfRequired()
{
$pass = new CallTypeHintPass;
$config = m::mock("Mockery\Generator\MockConfiguration", array(
"requiresCallStaticTypeHintRemoval" => true,
))->makePartial();
$code = $pass->apply(static::CODE, $config);
$this->assertContains('__callStatic($method, $args)', $code);
}
}

View File

@@ -0,0 +1,78 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace Mockery\Generator\StringManipulation\Pass;
use Mockery as m;
use Mockery\Generator\MockConfiguration;
use Mockery\Generator\StringManipulation\Pass\ClassNamePass;
use PHPUnit\Framework\TestCase;
class ClassNamePassTest extends TestCase
{
const CODE = "namespace Mockery; class Mock {}";
public function setup()
{
$this->pass = new ClassNamePass();
}
/**
* @test
*/
public function shouldRemoveNamespaceDefinition()
{
$config = new MockConfiguration(array(), array(), array(), "Dave\Dave");
$code = $this->pass->apply(static::CODE, $config);
$this->assertNotContains('namespace Mockery;', $code);
}
/**
* @test
*/
public function shouldReplaceNamespaceIfClassNameIsNamespaced()
{
$config = new MockConfiguration(array(), array(), array(), "Dave\Dave");
$code = $this->pass->apply(static::CODE, $config);
$this->assertNotContains('namespace Mockery;', $code);
$this->assertContains('namespace Dave;', $code);
}
/**
* @test
*/
public function shouldReplaceClassNameWithSpecifiedName()
{
$config = new MockConfiguration(array(), array(), array(), "Dave");
$code = $this->pass->apply(static::CODE, $config);
$this->assertContains('class Dave', $code);
}
/**
* @test
*/
public function shouldRemoveLeadingBackslashesFromNamespace()
{
$config = new MockConfiguration(array(), array(), array(), "\Dave\Dave");
$code = $this->pass->apply(static::CODE, $config);
$this->assertContains('namespace Dave;', $code);
}
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace Mockery\Test\Generator\StringManipulation\Pass;
use Mockery as m;
use Mockery\Generator\MockConfiguration;
use Mockery\Generator\StringManipulation\Pass\ConstantsPass;
use PHPUnit\Framework\TestCase;
class ConstantsPassTest extends TestCase
{
const CODE = 'class Foo {}';
/**
* @test
*/
public function shouldAddConstants()
{
$pass = new ConstantsPass;
$config = new MockConfiguration(
array(),
array(),
array(),
"ClassWithConstants",
false,
array(),
false,
['ClassWithConstants' => ['FOO' => 'test']]
);
$code = $pass->apply(static::CODE, $config);
$this->assertContains("const FOO = 'test'", $code);
}
}

View File

@@ -0,0 +1,44 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace Mockery\Test\Generator\StringManipulation\Pass;
use Mockery as m;
use Mockery\Generator\MockConfigurationBuilder;
use Mockery\Generator\StringManipulation\Pass\InstanceMockPass;
use PHPUnit\Framework\TestCase;
class InstanceMockPassTest extends TestCase
{
/**
* @test
*/
public function shouldAppendConstructorAndPropertyForInstanceMock()
{
$builder = new MockConfigurationBuilder;
$builder->setInstanceMock(true);
$config = $builder->getMockConfiguration();
$pass = new InstanceMockPass;
$code = $pass->apply('class Dave { }', $config);
$this->assertContains('public function __construct', $code);
$this->assertContains('protected $_mockery_ignoreVerification', $code);
}
}

View File

@@ -0,0 +1,66 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace Mockery\Test\Generator\StringManipulation\Pass;
use Mockery as m;
use Mockery\Generator\MockConfiguration;
use Mockery\Generator\StringManipulation\Pass\InterfacePass;
use PHPUnit\Framework\TestCase;
class InterfacePassTest extends TestCase
{
const CODE = "class Mock implements MockInterface";
/**
* @test
*/
public function shouldNotAlterCodeIfNoTargetInterfaces()
{
$pass = new InterfacePass;
$config = m::mock("Mockery\Generator\MockConfiguration", array(
"getTargetInterfaces" => array(),
));
$code = $pass->apply(static::CODE, $config);
$this->assertEquals(static::CODE, $code);
}
/**
* @test
*/
public function shouldAddAnyInterfaceNamesToImplementsDefinition()
{
$pass = new InterfacePass;
$config = m::mock("Mockery\Generator\MockConfiguration", array(
"getTargetInterfaces" => array(
m::mock(array("getName" => "Dave\Dave")),
m::mock(array("getName" => "Paddy\Paddy")),
),
));
$code = $pass->apply(static::CODE, $config);
$this->assertContains("implements MockInterface, \Dave\Dave, \Paddy\Paddy", $code);
}
}

View File

@@ -0,0 +1,392 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
declare(strict_types=1);
namespace Mockery\Test\Generator\StringManipulation\Pass;
use Mockery as m;
use Mockery\Generator\DefinedTargetClass;
use Mockery\Generator\StringManipulation\Pass\MagicMethodTypeHintsPass;
use PHPUnit\Framework\TestCase;
class MagicMethodTypeHintsPassTest extends TestCase
{
/**
* @var MagicMethodTypeHintsPass
*/
private $pass;
/**
* @var MockConfiguration
*/
private $mockedConfiguration;
/**
* Setup method
* @return void
*/
public function setup()
{
$this->pass = new MagicMethodTypeHintsPass;
$this->mockedConfiguration = m::mock(
'Mockery\Generator\MockConfiguration'
);
}
/**
* @test
*/
public function itShouldWork()
{
$this->assertTrue(true);
}
/**
* @test
*/
public function itShouldGrabClassMagicMethods()
{
$targetClass = DefinedTargetClass::factory(
'Mockery\Test\Generator\StringManipulation\Pass\MagicDummy'
);
$magicMethods = $this->pass->getMagicMethods($targetClass);
$this->assertCount(6, $magicMethods);
$this->assertEquals('__isset', $magicMethods[0]->getName());
}
/**
* @test
*/
public function itShouldGrabInterfaceMagicMethods()
{
$targetClass = DefinedTargetClass::factory(
'Mockery\Test\Generator\StringManipulation\Pass\MagicInterfaceDummy'
);
$magicMethods = $this->pass->getMagicMethods($targetClass);
$this->assertCount(6, $magicMethods);
$this->assertEquals('__isset', $magicMethods[0]->getName());
}
/**
* @test
*/
public function itShouldAddStringTypeHintOnMagicMethod()
{
$this->configureForClass();
$code = $this->pass->apply(
'public function __isset($name) {}',
$this->mockedConfiguration
);
$this->assertContains('string $name', $code);
$this->configureForInterface();
$code = $this->pass->apply(
'public function __isset($name) {}',
$this->mockedConfiguration
);
$this->assertContains('string $name', $code);
}
/**
* @test
*/
public function itShouldAddStringTypeHintOnAllMagicMethods()
{
$this->configureForInterfaces([
'Mockery\Test\Generator\StringManipulation\Pass\MagicInterfaceDummy',
'Mockery\Test\Generator\StringManipulation\Pass\MagicUnsetInterfaceDummy'
]);
$code = $this->pass->apply(
'public function __isset($name) {}',
$this->mockedConfiguration
);
$this->assertContains('string $name', $code);
$code = $this->pass->apply(
'public function __unset($name) {}',
$this->mockedConfiguration
);
$this->assertContains('string $name', $code);
}
/**
* @test
*/
public function itShouldAddBooleanReturnOnMagicMethod()
{
$this->configureForClass();
$code = $this->pass->apply(
'public function __isset($name) {}',
$this->mockedConfiguration
);
$this->assertContains(' : bool', $code);
$this->configureForInterface();
$code = $this->pass->apply(
'public function __isset($name) {}',
$this->mockedConfiguration
);
$this->assertContains(' : bool', $code);
}
/**
* @test
*/
public function itShouldAddTypeHintsOnToStringMethod()
{
$this->configureForClass();
$code = $this->pass->apply(
'public function __toString() {}',
$this->mockedConfiguration
);
$this->assertContains(' : string', $code);
$this->configureForInterface();
$code = $this->pass->apply(
'public function __toString() {}',
$this->mockedConfiguration
);
$this->assertContains(' : string', $code);
}
/**
* @test
*/
public function itShouldAddTypeHintsOnCallMethod()
{
$this->configureForClass();
$code = $this->pass->apply(
'public function __call($method, array $args) {}',
$this->mockedConfiguration
);
$this->assertContains('string $method', $code);
$this->configureForInterface();
$code = $this->pass->apply(
'public function __call($method, array $args) {}',
$this->mockedConfiguration
);
$this->assertContains('string $method', $code);
}
/**
* @test
*/
public function itShouldAddTypeHintsOnCallStaticMethod()
{
$this->configureForClass();
$code = $this->pass->apply(
'public static function __callStatic($method, array $args) {}',
$this->mockedConfiguration
);
$this->assertContains('string $method', $code);
$this->configureForInterface();
$code = $this->pass->apply(
'public static function __callStatic($method, array $args) {}',
$this->mockedConfiguration
);
$this->assertContains('string $method', $code);
}
/**
* @test
*/
public function itShouldNotAddReturnTypeHintIfOneIsNotFound()
{
$this->configureForClass('Mockery\Test\Generator\StringManipulation\Pass\MagicReturnDummy');
$code = $this->pass->apply(
'public static function __isset($parameter) {}',
$this->mockedConfiguration
);
$this->assertContains(') {', $code);
$this->configureForInterface('Mockery\Test\Generator\StringManipulation\Pass\MagicReturnInterfaceDummy');
$code = $this->pass->apply(
'public static function __isset($parameter) {}',
$this->mockedConfiguration
);
$this->assertContains(') {', $code);
}
/**
* @test
*/
public function itShouldReturnEmptyArrayIfClassDoesNotHaveMagicMethods()
{
$targetClass = DefinedTargetClass::factory(
'\StdClass'
);
$magicMethods = $this->pass->getMagicMethods($targetClass);
$this->assertInternalType('array', $magicMethods);
$this->assertEmpty($magicMethods);
}
/**
* @test
*/
public function itShouldReturnEmptyArrayIfClassTypeIsNotExpected()
{
$magicMethods = $this->pass->getMagicMethods(null);
$this->assertInternalType('array', $magicMethods);
$this->assertEmpty($magicMethods);
}
/**
* Tests if the pass correclty replaces all the magic
* method parameters with those found in the
* Mock class. This is made to avoid variable
* conflicts withing Mock's magic methods
* implementations.
*
* @test
*/
public function itShouldGrabAndReplaceAllParametersWithTheCodeStringMatches()
{
$this->configureForClass();
$code = $this->pass->apply(
'public function __call($method, array $args) {}',
$this->mockedConfiguration
);
$this->assertContains('$method', $code);
$this->assertContains('array $args', $code);
$this->configureForInterface();
$code = $this->pass->apply(
'public function __call($method, array $args) {}',
$this->mockedConfiguration
);
$this->assertContains('$method', $code);
$this->assertContains('array $args', $code);
}
protected function configureForClass(string $className = 'Mockery\Test\Generator\StringManipulation\Pass\MagicDummy')
{
$targetClass = DefinedTargetClass::factory($className);
$this->mockedConfiguration
->shouldReceive('getTargetClass')
->andReturn($targetClass)
->byDefault();
$this->mockedConfiguration
->shouldReceive('getTargetInterfaces')
->andReturn([])
->byDefault();
}
protected function configureForInterface(string $interfaceName = 'Mockery\Test\Generator\StringManipulation\Pass\MagicDummy')
{
$targetInterface = DefinedTargetClass::factory($interfaceName);
$this->mockedConfiguration
->shouldReceive('getTargetClass')
->andReturn(null)
->byDefault();
$this->mockedConfiguration
->shouldReceive('getTargetInterfaces')
->andReturn([$targetInterface])
->byDefault();
}
protected function configureForInterfaces(array $interfaceNames)
{
$targetInterfaces = array_map([DefinedTargetClass::class, 'factory'], $interfaceNames);
$this->mockedConfiguration
->shouldReceive('getTargetClass')
->andReturn(null)
->byDefault();
$this->mockedConfiguration
->shouldReceive('getTargetInterfaces')
->andReturn($targetInterfaces)
->byDefault();
}
}
class MagicDummy
{
public function __isset(string $name) : bool
{
return false;
}
public function __toString() : string
{
return '';
}
public function __wakeup()
{
}
public function __destruct()
{
}
public function __call(string $name, array $arguments) : string
{
}
public static function __callStatic(string $name, array $arguments) : int
{
}
public function nonMagicMethod()
{
}
}
class MagicReturnDummy
{
public function __isset(string $name)
{
return false;
}
}
interface MagicInterfaceDummy
{
public function __isset(string $name) : bool;
public function __toString() : string;
public function __wakeup();
public function __destruct();
public function __call(string $name, array $arguments) : string;
public static function __callStatic(string $name, array $arguments) : int;
public function nonMagicMethod();
}
interface MagicReturnInterfaceDummy
{
public function __isset(string $name);
}
interface MagicUnsetInterfaceDummy
{
public function __unset(string $name);
}

View File

@@ -0,0 +1,63 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/blob/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @copyright Copyright (c) 2016 Dave Marshall
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
use PHPUnit\Framework\TestCase;
class GlobalHelpersTest extends TestCase
{
public function setup()
{
\Mockery::globalHelpers();
}
public function tearDown()
{
\Mockery::close();
}
/** @test */
public function mock_creates_a_mock()
{
$double = mock();
$this->assertInstanceOf(\Mockery\MockInterface::class, $double);
$this->expectException(\Exception::class);
$double->foo();
}
/** @test */
public function spy_creates_a_spy()
{
$double = spy();
$this->assertInstanceOf(\Mockery\MockInterface::class, $double);
$double->foo();
}
/** @test */
public function named_mock_creates_a_named_mock()
{
$className = "Class".uniqid();
$double = namedMock($className);
$this->assertInstanceOf(\Mockery\MockInterface::class, $double);
$this->assertInstanceOf($className, $double);
}
}

View File

@@ -0,0 +1,62 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
use Mockery\Adapter\Phpunit\MockeryTestCase;
class HamcrestExpectationTest extends MockeryTestCase
{
public function setUp()
{
parent::setUp();
$this->mock = mock('foo');
}
public function tearDown()
{
\Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
parent::tearDown();
}
/** Just a quickie roundup of a few Hamcrest matchers to check nothing obvious out of place **/
public function testAnythingConstraintMatchesArgument()
{
$this->mock->shouldReceive('foo')->with(anything())->once();
$this->mock->foo(2);
}
public function testGreaterThanConstraintMatchesArgument()
{
$this->mock->shouldReceive('foo')->with(greaterThan(1))->once();
$this->mock->foo(2);
}
/**
* @expectedException Mockery\Exception
*/
public function testGreaterThanConstraintNotMatchesArgument()
{
$this->mock->shouldReceive('foo')->with(greaterThan(1));
$this->mock->foo(1);
Mockery::close();
}
}

View File

@@ -0,0 +1,35 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace Mockery\Loader;
use Mockery as m;
use Mockery\Loader\EvalLoader;
require_once __DIR__.'/LoaderTestCase.php';
class EvalLoaderTest extends LoaderTestCase
{
public function getLoader()
{
return new EvalLoader();
}
}

View File

@@ -0,0 +1,47 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace Mockery\Loader;
use Mockery\Generator\MockConfiguration;
use Mockery\Generator\MockDefinition;
use PHPUnit\Framework\TestCase;
abstract class LoaderTestCase extends TestCase
{
/**
* @test
*/
public function loadLoadsTheCode()
{
$className = 'Mock_' . uniqid();
$config = new MockConfiguration(array(), array(), array(), $className);
$code = "<?php class $className { } ";
$definition = new MockDefinition($config, $code);
$this->getLoader()->load($definition);
$this->assertTrue(class_exists($className));
}
abstract public function getLoader();
}

View File

@@ -0,0 +1,35 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace Mockery\Loader;
use Mockery as m;
use Mockery\Loader\RequireLoader;
require_once __DIR__.'/LoaderTestCase.php';
class RequireLoaderTest extends LoaderTestCase
{
public function getLoader()
{
return new RequireLoader(sys_get_temp_dir());
}
}

View File

@@ -0,0 +1,95 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
use Mockery\MockInterface;
use Mockery\Matcher\PHPUnitConstraint;
use PHPUnit\Framework\TestCase;
class PHPUnitConstraintTest extends TestCase
{
/** @var PHPUnitConstraint */
protected $matcher;
/** @var PHPUnitConstraint */
protected $rethrowingMatcher;
/** @var MockInterface */
protected $constraint;
public function setUp()
{
/*
* Revise by PHPUnit version
*/
if (class_exists('\PHPUnit\Framework\AssertionFailedError')) {
$this->assertionFailedError = '\PHPUnit\Framework\AssertionFailedError';
$this->frameworkConstraint = '\PHPUnit\Framework\Constraint';
} else {
$this->assertionFailedError = '\PHPUnit_Framework_AssertionFailedError';
$this->frameworkConstraint = '\PHPUnit_Framework_Constraint';
}
$this->constraint = \Mockery::mock($this->frameworkConstraint);
$this->matcher = new PHPUnitConstraint($this->constraint);
$this->rethrowingMatcher = new PHPUnitConstraint($this->constraint, true);
}
public function testMatches()
{
$value1 = 'value1';
$value2 = 'value1';
$value3 = 'value1';
$this->constraint
->shouldReceive('evaluate')
->once()
->with($value1)
->getMock()
->shouldReceive('evaluate')
->once()
->with($value2)
->andThrow($this->assertionFailedError)
->getMock()
->shouldReceive('evaluate')
->once()
->with($value3)
->getMock()
;
$this->assertTrue($this->matcher->match($value1));
$this->assertFalse($this->matcher->match($value2));
$this->assertTrue($this->rethrowingMatcher->match($value3));
}
public function testMatchesWhereNotMatchAndRethrowing()
{
$this->expectException($this->assertionFailedError);
$value = 'value';
$this->constraint
->shouldReceive('evaluate')
->once()
->with($value)
->andThrow($this->assertionFailedError)
;
$this->rethrowingMatcher->match($value);
}
public function test__toString()
{
$this->assertEquals('<Constraint>', $this->matcher);
}
}

View File

@@ -0,0 +1,97 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/blob/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @copyright Copyright (c) 2017 Dave Marshall
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace tests\Mockery\Matcher;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Mockery\Matcher\Subset;
class SubsetTest extends MockeryTestCase
{
/** @test */
public function it_matches_a_shallow_subset()
{
$matcher = Subset::strict(['dave' => 123]);
$actual = [
'foo' => 'bar',
'dave' => 123,
'bar' => 'baz',
];
$this->assertTrue($matcher->match($actual));
}
/** @test */
public function it_recursively_matches()
{
$matcher = Subset::strict(['foo' => ['bar' => ['baz' => 123]]]);
$actual = [
'foo' => [
'bar' => [
'baz' => 123,
]
],
'dave' => 123,
'bar' => 'baz',
];
$this->assertTrue($matcher->match($actual));
}
/** @test */
public function it_is_strict_by_default()
{
$matcher = new Subset(['dave' => 123]);
$actual = [
'foo' => 'bar',
'dave' => 123.0,
'bar' => 'baz',
];
$this->assertFalse($matcher->match($actual));
}
/** @test */
public function it_can_run_a_loose_comparison()
{
$matcher = Subset::loose(['dave' => 123]);
$actual = [
'foo' => 'bar',
'dave' => 123.0,
'bar' => 'baz',
];
$this->assertTrue($matcher->match($actual));
}
/** @test */
public function it_returns_false_if_actual_is_not_an_array()
{
$matcher = new Subset(['dave' => 123]);
$actual = null;
$this->assertFalse($matcher->match($actual));
}
}

View File

@@ -0,0 +1,94 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2012 Philip Graham <philip.robert.graham@gmail.com>
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
class MockClassWithFinalWakeupTest extends MockeryTestCase
{
protected function setUp()
{
$this->container = new \Mockery\Container;
}
protected function tearDown()
{
$this->container->mockery_close();
}
/**
* @test
*
* Test that we are able to create partial mocks of classes that have
* a __wakeup method marked as final. As long as __wakeup is not one of the
* mocked methods.
*/
public function testCreateMockForClassWithFinalWakeup()
{
$mock = $this->container->mock("test\Mockery\TestWithFinalWakeup");
$this->assertInstanceOf("test\Mockery\TestWithFinalWakeup", $mock);
$this->assertEquals('test\Mockery\TestWithFinalWakeup::__wakeup', $mock->__wakeup());
$mock = $this->container->mock('test\Mockery\SubclassWithFinalWakeup');
$this->assertInstanceOf('test\Mockery\SubclassWithFinalWakeup', $mock);
$this->assertEquals('test\Mockery\TestWithFinalWakeup::__wakeup', $mock->__wakeup());
}
public function testCreateMockForClassWithNonFinalWakeup()
{
$mock = $this->container->mock('test\Mockery\TestWithNonFinalWakeup');
$this->assertInstanceOf('test\Mockery\TestWithNonFinalWakeup', $mock);
// Make sure __wakeup is overridden and doesn't return anything.
$this->assertNull($mock->__wakeup());
}
}
class TestWithFinalWakeup
{
public function foo()
{
return 'foo';
}
public function bar()
{
return 'bar';
}
final public function __wakeup()
{
return __METHOD__;
}
}
class SubclassWithFinalWakeup extends TestWithFinalWakeup
{
}
class TestWithNonFinalWakeup
{
public function __wakeup()
{
return __METHOD__;
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace test\Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
class MockClassWithMethodOverloadingTest extends MockeryTestCase
{
/**
* @expectedException BadMethodCallException
*/
public function testCreateMockForClassWithMethodOverloading()
{
$mock = mock('test\Mockery\TestWithMethodOverloading')
->makePartial();
$this->assertInstanceOf('test\Mockery\TestWithMethodOverloading', $mock);
// TestWithMethodOverloading::__call wouldn't be used. See Gotchas!.
$mock->randomMethod();
}
public function testCreateMockForClassWithMethodOverloadingWithExistingMethod()
{
$mock = mock('test\Mockery\TestWithMethodOverloading')
->makePartial();
$this->assertInstanceOf('test\Mockery\TestWithMethodOverloading', $mock);
$this->assertSame(1, $mock->thisIsRealMethod());
}
}
class TestWithMethodOverloading
{
public function __call($name, $arguments)
{
return 1;
}
public function thisIsRealMethod()
{
return 1;
}
}

View File

@@ -0,0 +1,43 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery;
use Mockery\MockInterface;
use Mockery\Adapter\Phpunit\MockeryTestCase;
class MockClassWithUnknownTypeHintTest extends MockeryTestCase
{
/** @test */
public function itShouldSuccessfullyBuildTheMock()
{
$mock = mock("test\Mockery\HasUnknownClassAsTypeHintOnMethod");
$this->assertInstanceOf(MockInterface::class, $mock);
}
}
class HasUnknownClassAsTypeHintOnMethod
{
public function foo(\UnknownTestClass\Bar $bar)
{
}
}

View File

@@ -0,0 +1,186 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/blob/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
use Mockery\Adapter\Phpunit\MockeryTestCase;
class Mockery_MockTest extends MockeryTestCase
{
public function testAnonymousMockWorksWithNotAllowingMockingOfNonExistentMethods()
{
\Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
$m = mock();
$m->shouldReceive("test123")->andReturn(true);
assertThat($m->test123(), equalTo(true));
\Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
}
public function testMockWithNotAllowingMockingOfNonExistentMethodsCanBeGivenAdditionalMethodsToMockEvenIfTheyDontExistOnClass()
{
\Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
$m = mock('ExampleClassForTestingNonExistentMethod');
$m->shouldAllowMockingMethod('testSomeNonExistentMethod');
$m->shouldReceive("testSomeNonExistentMethod")->andReturn(true);
assertThat($m->testSomeNonExistentMethod(), equalTo(true));
\Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
}
public function testShouldAllowMockingMethodReturnsMockInstance()
{
$m = Mockery::mock('someClass');
$this->assertInstanceOf('Mockery\MockInterface', $m->shouldAllowMockingMethod('testFunction'));
}
public function testShouldAllowMockingProtectedMethodReturnsMockInstance()
{
$m = Mockery::mock('someClass');
$this->assertInstanceOf('Mockery\MockInterface', $m->shouldAllowMockingProtectedMethods('testFunction'));
}
public function testMockAddsToString()
{
$mock = mock('ClassWithNoToString');
$this->assertTrue(method_exists($mock, '__toString'));
}
public function testMockToStringMayBeDeferred()
{
$mock = mock('ClassWithToString')->makePartial();
$this->assertEquals("foo", (string)$mock);
}
public function testMockToStringShouldIgnoreMissingAlwaysReturnsString()
{
$mock = mock('ClassWithNoToString')->shouldIgnoreMissing();
$this->assertNotEquals('', (string)$mock);
$mock->asUndefined();
$this->assertNotEquals('', (string)$mock);
}
public function testShouldIgnoreMissing()
{
$mock = mock('ClassWithNoToString')->shouldIgnoreMissing();
$this->assertNull($mock->nonExistingMethod());
}
/**
* @expectedException Mockery\Exception
*/
public function testShouldIgnoreMissingDisallowMockingNonExistentMethodsUsingGlobalConfiguration()
{
Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
$mock = mock('ClassWithMethods')->shouldIgnoreMissing();
$mock->shouldReceive('nonExistentMethod');
}
/**
* @expectedException BadMethodCallException
*/
public function testShouldIgnoreMissingCallingNonExistentMethodsUsingGlobalConfiguration()
{
Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
$mock = mock('ClassWithMethods')->shouldIgnoreMissing();
$mock->nonExistentMethod();
}
public function testShouldIgnoreMissingCallingExistentMethods()
{
Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
$mock = mock('ClassWithMethods')->shouldIgnoreMissing();
assertThat(nullValue($mock->foo()));
$mock->shouldReceive('bar')->passthru();
assertThat($mock->bar(), equalTo('bar'));
}
public function testShouldIgnoreMissingCallingNonExistentMethods()
{
Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
$mock = mock('ClassWithMethods')->shouldIgnoreMissing();
assertThat(nullValue($mock->foo()));
assertThat(nullValue($mock->bar()));
assertThat(nullValue($mock->nonExistentMethod()));
$mock->shouldReceive(array('foo' => 'new_foo', 'nonExistentMethod' => 'result'));
$mock->shouldReceive('bar')->passthru();
assertThat($mock->foo(), equalTo('new_foo'));
assertThat($mock->bar(), equalTo('bar'));
assertThat($mock->nonExistentMethod(), equalTo('result'));
}
public function testCanMockException()
{
$exception = Mockery::mock('Exception');
$this->assertInstanceOf('Exception', $exception);
}
public function testCanMockSubclassOfException()
{
$errorException = Mockery::mock('ErrorException');
$this->assertInstanceOf('ErrorException', $errorException);
$this->assertInstanceOf('Exception', $errorException);
}
public function testCallingShouldReceiveWithoutAValidMethodName()
{
$mock = Mockery::mock();
$this->expectException("InvalidArgumentException", "Received empty method name");
$mock->shouldReceive("");
}
/**
* @expectedException Mockery\Exception
*/
public function testShouldThrowExceptionWithInvalidClassName()
{
mock('ClassName.CannotContainDot');
}
}
class ExampleClassForTestingNonExistentMethod
{
}
class ClassWithToString
{
public function __toString()
{
return 'foo';
}
}
class ClassWithNoToString
{
}
class ClassWithMethods
{
public function foo()
{
return 'foo';
}
public function bar()
{
return 'bar';
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Mockery;
use Mockery as m;
use Mockery\Fixtures\SemiReservedWordsAsMethods;
use PHPUnit\Framework\TestCase;
/**
* @requires PHP 7.0.0
*/
class MockeryCanMockClassesWithSemiReservedWordsTest extends TestCase
{
/**
* @test
*/
public function smoke_test()
{
require __DIR__.'/Fixtures/SemiReservedWordsAsMethods.php';
$mock = m::mock("Mockery\Fixtures\SemiReservedWordsAsMethods");
$mock->shouldReceive("include")->andReturn("foo");
$this->assertTrue(method_exists($mock, "include"));
$this->assertEquals("foo", $mock->include());
}
}

View File

@@ -0,0 +1,65 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace Mockery\Tests;
use Mockery\Adapter\Phpunit\MockeryTestCase;
class GeneratorTest extends MockeryTestCase
{
/** @test */
public function shouldNotDuplicateDoublyInheritedMethods()
{
$container = new \Mockery\Container;
$mock = $container->mock('Mockery\Tests\Evenement_EventEmitter', 'Mockery\Tests\Chatroulette_ConnectionInterface');
}
}
interface Evenement_EventEmitterInterface
{
public function on($name, $callback);
}
class Evenement_EventEmitter implements Evenement_EventEmitterInterface
{
public function on($name, $callback)
{
}
}
interface React_StreamInterface extends Evenement_EventEmitterInterface
{
public function close();
}
interface React_ReadableStreamInterface extends React_StreamInterface
{
public function pause();
}
interface React_WritableStreamInterface extends React_StreamInterface
{
public function write($data);
}
interface Chatroulette_ConnectionInterface extends React_ReadableStreamInterface, React_WritableStreamInterface
{
}

View File

@@ -0,0 +1,43 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
class MockingAllLowerCasedMethodsTest extends MockeryTestCase
{
/** @test */
public function itShouldAllowToCallAllLowerCasedMethodAsCamelCased()
{
require __DIR__."/Fixtures/ClassWithAllLowerCaseMethod.php";
$mock = mock('test\Mockery\Fixtures\ClassWithAllLowerCaseMethod');
$mock->shouldReceive('userExpectsCamelCaseMethod')
->andReturn('mocked');
$result = $mock->userExpectsCamelCaseMethod();
$expected = 'mocked';
self::assertSame($expected, $result);
}
}

View File

@@ -0,0 +1,43 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
class MockingClassConstantsTest extends MockeryTestCase
{
/** @test */
public function itShouldAllowToMockClassConstants()
{
\Mockery::getConfiguration()->setConstantsMap([
'ClassWithConstants' => [
'FOO' => 'baz',
'X' => 2,
]
]);
$mock = \Mockery::mock('overload:ClassWithConstants');
self::assertEquals('baz', $mock::FOO);
self::assertEquals(2, $mock::X);
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
/**
* @requires PHP 7.1.0RC3
*/
class MockingMethodsWithIterableTypeHintsTest extends MockeryTestCase
{
/** @test */
public function itShouldSuccessfullyBuildTheMock()
{
require __DIR__."/Fixtures/MethodWithIterableTypeHints.php";
$mock = mock("test\Mockery\Fixtures\MethodWithIterableTypeHints");
$this->assertInstanceOf(\test\Mockery\Fixtures\MethodWithIterableTypeHints::class, $mock);
}
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
/**
*/
class MockingMethodsWithNullableParametersTest extends MockeryTestCase
{
/**
* @test
* @requires PHP 7.1.0RC3
*/
public function it_can_handle_nullable_typed_parameters()
{
require __DIR__."/Fixtures/MethodWithNullableTypedParameter.php";
$mock = mock("test\Mockery\Fixtures\MethodWithNullableTypedParameter");
$this->assertInstanceOf(\test\Mockery\Fixtures\MethodWithNullableTypedParameter::class, $mock);
}
/**
* @test
*/
public function it_can_handle_default_parameters()
{
require __DIR__."/Fixtures/MethodWithParametersWithDefaultValues.php";
$mock = mock("test\Mockery\Fixtures\MethodWithParametersWithDefaultValues");
$this->assertInstanceOf(\test\Mockery\Fixtures\MethodWithParametersWithDefaultValues::class, $mock);
}
}

View File

@@ -0,0 +1,217 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Mockery\Generator\Method;
use test\Mockery\Fixtures\MethodWithNullableReturnType;
/**
* @requires PHP 7.1.0RC3
*/
class MockingNullableMethodsTest extends MockeryTestCase
{
/**
* @var \Mockery\Container
*/
private $container;
protected function setUp()
{
parent::setUp();
require_once __DIR__."/Fixtures/MethodWithNullableReturnType.php";
}
/**
* @test
*/
public function itShouldAllowNonNullableTypeToBeSet()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");
$mock->shouldReceive('nonNullablePrimitive')->andReturn('a string');
$mock->nonNullablePrimitive();
}
/**
* @test
* @expectedException \TypeError
*/
public function itShouldNotAllowNonNullToBeNull()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");
$mock->shouldReceive('nonNullablePrimitive')->andReturn(null);
$mock->nonNullablePrimitive();
}
/**
* @test
*/
public function itShouldAllowPrimitiveNullableToBeNull()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");
$mock->shouldReceive('nullablePrimitive')->andReturn(null);
$mock->nullablePrimitive();
}
/**
* @test
*/
public function itShouldAllowPrimitiveNullabeToBeSet()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");
$mock->shouldReceive('nullablePrimitive')->andReturn('a string');
$mock->nullablePrimitive();
}
/**
* @test
*/
public function itShouldAllowSelfToBeSet()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");
$mock->shouldReceive('nonNullableSelf')->andReturn(new MethodWithNullableReturnType());
$mock->nonNullableSelf();
}
/**
* @test
* @expectedException \TypeError
*/
public function itShouldNotAllowSelfToBeNull()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");
$mock->shouldReceive('nonNullableSelf')->andReturn(null);
$mock->nonNullableSelf();
}
/**
* @test
*/
public function itShouldAllowNullableSelfToBeSet()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");
$mock->shouldReceive('nullableSelf')->andReturn(new MethodWithNullableReturnType());
$mock->nullableSelf();
}
/**
* @test
*/
public function itShouldAllowNullableSelfToBeNull()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");
$mock->shouldReceive('nullableSelf')->andReturn(null);
$mock->nullableSelf();
}
/**
* @test
*/
public function itShouldAllowClassToBeSet()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");
$mock->shouldReceive('nonNullableClass')->andReturn(new MethodWithNullableReturnType());
$mock->nonNullableClass();
}
/**
* @test
* @expectedException \TypeError
*/
public function itShouldNotAllowClassToBeNull()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");
$mock->shouldReceive('nonNullableClass')->andReturn(null);
$mock->nonNullableClass();
}
/**
* @test
*/
public function itShouldAllowNullalbeClassToBeSet()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");
$mock->shouldReceive('nullableClass')->andReturn(new MethodWithNullableReturnType());
$mock->nullableClass();
}
/**
* @test
*/
public function itShouldAllowNullableClassToBeNull()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");
$mock->shouldReceive('nullableClass')->andReturn(null);
$mock->nullableClass();
}
/** @test */
public function it_allows_returning_null_for_nullable_object_return_types()
{
$double= \Mockery::mock(MethodWithNullableReturnType::class);
$double->shouldReceive("nullableClass")->andReturnNull();
$this->assertNull($double->nullableClass());
}
/** @test */
public function it_allows_returning_null_for_nullable_string_return_types()
{
$double= \Mockery::mock(MethodWithNullableReturnType::class);
$double->shouldReceive("nullableString")->andReturnNull();
$this->assertNull($double->nullableString());
}
/** @test */
public function it_allows_returning_null_for_nullable_int_return_types()
{
$double= \Mockery::mock(MethodWithNullableReturnType::class);
$double->shouldReceive("nullableInt")->andReturnNull();
$this->assertNull($double->nullableInt());
}
/** @test */
public function it_returns_null_on_calls_to_ignored_methods_of_spies_if_return_type_is_nullable()
{
$double = \Mockery::spy(MethodWithNullableReturnType::class);
$this->assertNull($double->nullableClass());
}
}

View File

@@ -0,0 +1,42 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Mockery\MockInterface;
class MockingOldStyleConstructorTest extends MockeryTestCase
{
/**
* @issue issue/139
*/
public function testCanMockClassWithOldStyleConstructorAndArguments()
{
$this->assertInstanceOf(MockInterface::class, mock('MockeryTest_OldStyleConstructor'));
}
}
class MockeryTest_OldStyleConstructor
{
public function MockeryTest_OldStyleConstructor($arg)
{
}
}

View File

@@ -0,0 +1,177 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
declare(strict_types=1); // Use strict types to ensure exact types are returned or passed
namespace test\Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
class MockingParameterAndReturnTypesTest extends MockeryTestCase
{
public function testMockingStringReturnType()
{
$mock = mock("test\Mockery\TestWithParameterAndReturnType");
$mock->shouldReceive("returnString");
$this->assertSame("", $mock->returnString());
}
public function testMockingIntegerReturnType()
{
$mock = mock("test\Mockery\TestWithParameterAndReturnType");
$mock->shouldReceive("returnInteger");
$this->assertSame(0, $mock->returnInteger());
}
public function testMockingFloatReturnType()
{
$mock = mock("test\Mockery\TestWithParameterAndReturnType");
$mock->shouldReceive("returnFloat");
$this->assertSame(0.0, $mock->returnFloat());
}
public function testMockingBooleanReturnType()
{
$mock = mock("test\Mockery\TestWithParameterAndReturnType");
$mock->shouldReceive("returnBoolean");
$this->assertFalse($mock->returnBoolean());
}
public function testMockingArrayReturnType()
{
$mock = mock("test\Mockery\TestWithParameterAndReturnType");
$mock->shouldReceive("returnArray");
$this->assertSame([], $mock->returnArray());
}
public function testMockingGeneratorReturnTyps()
{
$mock = mock("test\Mockery\TestWithParameterAndReturnType");
$mock->shouldReceive("returnGenerator");
$this->assertInstanceOf("\Generator", $mock->returnGenerator());
}
public function testMockingCallableReturnType()
{
$mock = mock("test\Mockery\TestWithParameterAndReturnType");
$mock->shouldReceive("returnCallable");
$this->assertInternalType('callable', $mock->returnCallable());
}
public function testMockingClassReturnTypes()
{
$mock = mock("test\Mockery\TestWithParameterAndReturnType");
$mock->shouldReceive("withClassReturnType");
$this->assertInstanceOf("test\Mockery\TestWithParameterAndReturnType", $mock->withClassReturnType());
}
public function testMockingParameterTypes()
{
$mock = mock("test\Mockery\TestWithParameterAndReturnType");
$mock->shouldReceive("withScalarParameters");
$mock->withScalarParameters(1, 1.0, true, 'string');
}
public function testIgnoringMissingReturnsType()
{
$mock = mock("test\Mockery\TestWithParameterAndReturnType");
$mock->shouldIgnoreMissing();
$this->assertSame('', $mock->returnString());
$this->assertSame(0, $mock->returnInteger());
$this->assertSame(0.0, $mock->returnFloat());
$this->assertFalse( $mock->returnBoolean());
$this->assertSame([], $mock->returnArray());
$this->assertInternalType('callable', $mock->returnCallable());
$this->assertInstanceOf("\Generator", $mock->returnGenerator());
$this->assertInstanceOf("test\Mockery\TestWithParameterAndReturnType", $mock->withClassReturnType());
}
public function testAutoStubbingSelf()
{
$spy = \Mockery::spy("test\Mockery\TestWithParameterAndReturnType");
$this->assertInstanceOf("test\Mockery\TestWithParameterAndReturnType", $spy->returnSelf());
}
public function testItShouldMockClassWithHintedParamsInMagicMethod()
{
$this->assertNotNull(
\Mockery::mock('test\Mockery\MagicParams')
);
}
public function testItShouldMockClassWithHintedReturnInMagicMethod()
{
$this->assertNotNull(
\Mockery::mock('test\Mockery\MagicReturns')
);
}
}
class MagicParams
{
public function __isset(string $property)
{
return false;
}
}
class MagicReturns
{
public function __isset($property) : bool
{
return false;
}
}
abstract class TestWithParameterAndReturnType
{
public function returnString(): string {}
public function returnInteger(): int {}
public function returnFloat(): float {}
public function returnBoolean(): bool {}
public function returnArray(): array {}
public function returnCallable(): callable {}
public function returnGenerator(): \Generator {}
public function withClassReturnType(): TestWithParameterAndReturnType {}
public function withScalarParameters(int $integer, float $float, bool $boolean, string $string) {}
public function returnSelf(): self {}
}

View File

@@ -0,0 +1,133 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
class MockingProtectedMethodsTest extends MockeryTestCase
{
/**
* @test
*
* This is a regression test, basically we don't want the mock handling
* interfering with calling protected methods partials
*/
public function shouldAutomaticallyDeferCallsToProtectedMethodsForPartials()
{
$mock = mock("test\Mockery\TestWithProtectedMethods[foo]");
$this->assertEquals("bar", $mock->bar());
}
/**
* @test
*
* This is a regression test, basically we don't want the mock handling
* interfering with calling protected methods partials
*/
public function shouldAutomaticallyDeferCallsToProtectedMethodsForRuntimePartials()
{
$mock = mock("test\Mockery\TestWithProtectedMethods")->makePartial();
$this->assertEquals("bar", $mock->bar());
}
/** @test */
public function shouldAutomaticallyIgnoreAbstractProtectedMethods()
{
$mock = mock("test\Mockery\TestWithProtectedMethods")->makePartial();
$this->assertNull($mock->foo());
}
/** @test */
public function shouldAllowMockingProtectedMethods()
{
$mock = mock("test\Mockery\TestWithProtectedMethods")
->makePartial()
->shouldAllowMockingProtectedMethods();
$mock->shouldReceive("protectedBar")->andReturn("notbar");
$this->assertEquals("notbar", $mock->bar());
}
/** @test */
public function shouldAllowMockingProtectedMethodOnDefinitionTimePartial()
{
$mock = mock("test\Mockery\TestWithProtectedMethods[protectedBar]")
->shouldAllowMockingProtectedMethods();
$mock->shouldReceive("protectedBar")->andReturn("notbar");
$this->assertEquals("notbar", $mock->bar());
}
/** @test */
public function shouldAllowMockingAbstractProtectedMethods()
{
$mock = mock("test\Mockery\TestWithProtectedMethods")
->makePartial()
->shouldAllowMockingProtectedMethods();
$mock->shouldReceive("abstractProtected")->andReturn("abstractProtected");
$this->assertEquals("abstractProtected", $mock->foo());
}
/** @test */
public function shouldAllowMockingIncreasedVisabilityMethods()
{
$mock = mock("test\Mockery\TestIncreasedVisibilityChild");
$mock->shouldReceive('foobar')->andReturn("foobar");
$this->assertEquals('foobar', $mock->foobar());
}
}
abstract class TestWithProtectedMethods
{
public function foo()
{
return $this->abstractProtected();
}
abstract protected function abstractProtected();
public function bar()
{
return $this->protectedBar();
}
protected function protectedBar()
{
return 'bar';
}
}
class TestIncreasedVisibilityParent
{
protected function foobar()
{
}
}
class TestIncreasedVisibilityChild extends TestIncreasedVisibilityParent
{
public function foobar()
{
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
class MockingVariadicArgumentsTest extends MockeryTestCase
{
/** @test */
public function shouldAllowMockingVariadicArguments()
{
$mock = mock("test\Mockery\TestWithVariadicArguments");
$mock->shouldReceive("foo")->andReturn("notbar");
$this->assertEquals("notbar", $mock->foo());
}
}
abstract class TestWithVariadicArguments
{
public function foo(...$bar)
{
return $bar;
}
}

View File

@@ -0,0 +1,53 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
/**
* @requires PHP 7.1.0RC3
*/
class MockingVoidMethodsTest extends MockeryTestCase
{
protected function setUp()
{
require_once __DIR__."/Fixtures/MethodWithVoidReturnType.php";
}
/** @test */
public function itShouldSuccessfullyBuildTheMock()
{
$mock = mock("test\Mockery\Fixtures\MethodWithVoidReturnType");
$this->assertInstanceOf(\test\Mockery\Fixtures\MethodWithVoidReturnType::class, $mock);
}
/** @test */
public function it_can_stub_and_mock_void_methods()
{
$mock = mock("test\Mockery\Fixtures\MethodWithVoidReturnType");
$mock->shouldReceive("foo");
$mock->foo();
}
}

View File

@@ -0,0 +1,86 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
require_once __DIR__.'/DummyClasses/Namespaced.php';
use Mockery\Adapter\Phpunit\MockeryTestCase;
use test\Mockery\Stubs\Animal;
use test\Mockery\Stubs\Habitat;
class NamedMockTest extends MockeryTestCase
{
/** @test */
public function itCreatesANamedMock()
{
$mock = Mockery::namedMock("Mockery\Dave123");
$this->assertInstanceOf("Mockery\Dave123", $mock);
}
/** @test */
public function itCreatesPassesFurtherArgumentsJustLikeMock()
{
$mock = Mockery::namedMock("Mockery\Dave456", "DateTime", array(
"getDave" => "dave"
));
$this->assertInstanceOf("DateTime", $mock);
$this->assertEquals("dave", $mock->getDave());
}
/**
* @test
* @expectedException Mockery\Exception
* @expectedExceptionMessage The mock named 'Mockery\Dave7' has been already defined with a different mock configuration
*/
public function itShouldThrowIfAttemptingToRedefineNamedMock()
{
$mock = Mockery::namedMock("Mockery\Dave7");
$mock = Mockery::namedMock("Mockery\Dave7", "DateTime");
}
/** @test */
public function itCreatesConcreteMethodImplementationWithReturnType()
{
$cactus = new \Nature\Plant();
$gardener = Mockery::namedMock(
"NewNamespace\\ClassName",
"Gardener",
array('water' => true)
);
$this->assertTrue($gardener->water($cactus));
}
/**
* @test
* @requires PHP 7.0.0
*/
public function it_gracefully_handles_namespacing()
{
$animal = Mockery::namedMock(
uniqid(Animal::class, false),
Animal::class
);
$animal->shouldReceive("habitat")->andReturn(new Habitat());
$this->assertInstanceOf(Habitat::class, $animal->habitat());
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace test\Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
/**
* @requires PHP 7.2.0-dev
*/
class Php72LanguageFeaturesTest extends MockeryTestCase
{
/** @test */
public function it_can_mock_a_class_with_an_object_argument_type_hint()
{
$mock = mock(ArgumentObjectTypeHint::class);
$object = new \stdClass;
$mock->allows()->foo($object);
$mock->foo($object);
}
/** @test */
public function it_can_mock_a_class_with_an_object_return_type_hint()
{
$mock = spy(ReturnTypeObjectTypeHint::class);
$object = $mock->foo();
$this->assertTrue(is_object($object));
}
}
class ArgumentObjectTypeHint
{
public function foo(object $foo)
{
}
}
class ReturnTypeObjectTypeHint
{
public function foo(): object
{
}
}

View File

@@ -0,0 +1,152 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery;
use Mockery as m;
use Mockery\Spy;
use Mockery\Adapter\Phpunit\MockeryTestCase;
class SpyTest extends MockeryTestCase
{
/** @test */
public function itVerifiesAMethodWasCalled()
{
$spy = m::spy();
$spy->myMethod();
$spy->shouldHaveReceived("myMethod");
$this->expectException("Mockery\Exception\InvalidCountException");
$spy->shouldHaveReceived("someMethodThatWasNotCalled");
}
/** @test */
public function itVerifiesAMethodWasNotCalled()
{
$spy = m::spy();
$spy->shouldNotHaveReceived("myMethod");
$this->expectException("Mockery\Exception\InvalidCountException");
$spy->myMethod();
$spy->shouldNotHaveReceived("myMethod");
}
/** @test */
public function itVerifiesAMethodWasNotCalledWithParticularArguments()
{
$spy = m::spy();
$spy->myMethod(123, 456);
$spy->shouldNotHaveReceived("myMethod", array(789, 10));
$this->expectException("Mockery\Exception\InvalidCountException");
$spy->shouldNotHaveReceived("myMethod", array(123, 456));
}
/** @test */
public function itVerifiesAMethodWasCalledASpecificNumberOfTimes()
{
$spy = m::spy();
$spy->myMethod();
$spy->myMethod();
$spy->shouldHaveReceived("myMethod")->twice();
$this->expectException("Mockery\Exception\InvalidCountException");
$spy->myMethod();
$spy->shouldHaveReceived("myMethod")->twice();
}
/** @test */
public function itVerifiesAMethodWasCalledWithSpecificArguments()
{
$spy = m::spy();
$spy->myMethod(123, "a string");
$spy->shouldHaveReceived("myMethod")->with(123, "a string");
$spy->shouldHaveReceived("myMethod", array(123, "a string"));
$this->expectException("Mockery\Exception\InvalidCountException");
$spy->shouldHaveReceived("myMethod")->with(123);
}
/** @test */
public function itIncrementsExpectationCountWhenShouldHaveReceivedIsUsed()
{
$spy = m::spy();
$spy->myMethod('param1', 'param2');
$spy->shouldHaveReceived('myMethod')->with('param1', 'param2');
$this->assertEquals(1, $spy->mockery_getExpectationCount());
}
/** @test */
public function itIncrementsExpectationCountWhenShouldNotHaveReceivedIsUsed()
{
$spy = m::spy();
$spy->shouldNotHaveReceived('method');
$this->assertEquals(1, $spy->mockery_getExpectationCount());
}
/** @test */
public function any_args_can_be_used_with_alternative_syntax()
{
$spy = m::spy();
$spy->foo(123, 456);
$spy->shouldHaveReceived()->foo(anyArgs());
}
/** @test */
public function should_have_received_higher_order_message_call_a_method_with_correct_arguments()
{
$spy = m::spy();
$spy->foo(123);
$spy->shouldHaveReceived()->foo(123);
}
/** @test */
public function should_have_received_higher_order_message_call_a_method_with_incorrect_arguments_throws_exception()
{
$spy = m::spy();
$spy->foo(123);
$this->expectException("Mockery\Exception\InvalidCountException");
$spy->shouldHaveReceived()->foo(456);
}
/** @test */
public function should_not_have_received_higher_order_message_call_a_method_with_incorrect_arguments()
{
$spy = m::spy();
$spy->foo(123);
$spy->shouldNotHaveReceived()->foo(456);
}
/** @test */
public function should_not_have_received_higher_order_message_call_a_method_with_correct_arguments_throws_an_exception()
{
$spy = m::spy();
$spy->foo(123);
$this->expectException("Mockery\Exception\InvalidCountException");
$spy->shouldNotHaveReceived()->foo(123);
}
}

View File

@@ -0,0 +1,29 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery\Stubs;
class Animal
{
public function habitat(): Habitat
{
}
}

View File

@@ -0,0 +1,26 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery\Stubs;
class Habitat
{
}

View File

@@ -0,0 +1,72 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/blob/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @copyright Copyright (c) 2017 Dave Marshall (dave@atst.io)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
namespace test\Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Mockery\Loader\RequireLoader;
class TraitTest extends MockeryTestCase
{
/** @test */
public function it_can_create_an_object_for_a_simple_trait()
{
$trait = mock(SimpleTrait::class);
$this->assertEquals('bar', $trait->foo());
}
/** @test */
public function it_creates_abstract_methods_as_necessary()
{
$trait = mock(TraitWithAbstractMethod::class, ['doBaz' => 'baz']);
$this->assertEquals('baz', $trait->baz());
}
/** @test */
public function it_can_create_an_object_using_multiple_traits()
{
$trait = mock(SimpleTrait::class, TraitWithAbstractMethod::class, [
'doBaz' => 123,
]);
$this->assertEquals('bar', $trait->foo());
$this->assertEquals(123, $trait->baz());
}
}
trait SimpleTrait
{
public function foo()
{
return 'bar';
}
}
trait TraitWithAbstractMethod
{
public function baz()
{
return $this->doBaz();
}
abstract public function doBaz();
}

View File

@@ -0,0 +1,123 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/
use PHPUnit\Framework\TestCase;
class WithFormatterExpectationTest extends TestCase
{
/**
* @dataProvider formatObjectsDataProvider
*/
public function testFormatObjects($args, $expected)
{
$this->assertEquals(
$expected,
Mockery::formatObjects($args)
);
}
/**
* @expectedException Mockery\Exception\NoMatchingExpectationException
*
* Note that without the patch checked in with this test, rather than throwing
* an exception, the program will go into an infinite recursive loop
*/
public function testFormatObjectsWithMockCalledInGetterDoesNotLeadToRecursion()
{
$mock = Mockery::mock('stdClass');
$mock->shouldReceive('doBar')->with('foo');
$obj = new ClassWithGetter($mock);
$obj->getFoo();
}
public function formatObjectsDataProvider()
{
return array(
array(
array(null),
''
),
array(
array('a string', 98768, array('a', 'nother', 'array')),
''
),
);
}
/** @test */
public function format_objects_should_not_call_getters_with_params()
{
$obj = new ClassWithGetterWithParam();
$string = Mockery::formatObjects(array($obj));
$this->assertNotContains('Missing argument 1 for', $string);
}
public function testFormatObjectsExcludesStaticProperties()
{
$obj = new ClassWithPublicStaticProperty();
$string = Mockery::formatObjects(array($obj));
$this->assertNotContains('excludedProperty', $string);
}
public function testFormatObjectsExcludesStaticGetters()
{
$obj = new ClassWithPublicStaticGetter();
$string = Mockery::formatObjects(array($obj));
$this->assertNotContains('getExcluded', $string);
}
}
class ClassWithGetter
{
private $dep;
public function __construct($dep)
{
$this->dep = $dep;
}
public function getFoo()
{
return $this->dep->doBar('bar', $this);
}
}
class ClassWithGetterWithParam
{
public function getBar($bar)
{
}
}
class ClassWithPublicStaticProperty
{
public static $excludedProperty;
}
class ClassWithPublicStaticGetter
{
public static function getExcluded()
{
}
}

View File