Skip to content

Commit

Permalink
Merge pull request #780 from davedevelopment/cater-for-object-type-hints
Browse files Browse the repository at this point in the history
Cater for object type hints in PHP 7.2
  • Loading branch information
davedevelopment committed Aug 22, 2017
2 parents b5b7fda + 2fb26a5 commit afc610b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ protected function renderTypeHint(Parameter $param)
'string',
'iterable',
);

if (version_compare(PHP_VERSION, '7.2.0-dev') >= 0) {
$languageTypeHints[] = 'object';
}

$typeHint = trim($param->getTypeHintAsString());

if (!empty($typeHint)) {
Expand Down
5 changes: 5 additions & 0 deletions library/Mockery/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,11 @@ public function mockery_returnValueForMethod($name)
case 'void':
return null;

case 'object':
if (version_compare(PHP_VERSION, '7.2.0-dev') >= 0) {
return \Mockery::mock();
}

default:
return \Mockery::mock($type);
}
Expand Down
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
<exclude>./tests/Mockery/MockingVariadicArgumentsTest.php</exclude>
<exclude>./tests/Mockery/MockingParameterAndReturnTypesTest.php</exclude>
<exclude>./tests/Mockery/Generator/StringManipulation/Pass/MagicMethodTypeHintsPassTest.php</exclude>
<exclude>./tests/Mockery/Php72LanguageFeaturesTest.php</exclude>
<file phpVersion="7.0.0-dev" phpVersionOperator=">=">./tests/Mockery/MockingParameterAndReturnTypesTest.php</file>
<file phpVersion="7.0.0-dev" phpVersionOperator=">=">./tests/Mockery/Generator/StringManipulation/Pass/MagicMethodTypeHintsPassTest.php</file>
<file phpVersion="7.2.0-dev" phpVersionOperator=">=">./tests/Mockery/Php72LanguageFeaturesTest.php</file>
</testsuite>
</testsuites>
<filter>
Expand Down
2 changes: 1 addition & 1 deletion tests/Mockery/DemeterChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DemeterChainTest extends MockeryTestCase

public function setUp()
{
$this->mock = $this->mock = Mockery::mock('object')->shouldIgnoreMissing();
$this->mock = $this->mock = Mockery::mock()->shouldIgnoreMissing();
}

public function tearDown()
Expand Down
41 changes: 41 additions & 0 deletions tests/Mockery/Php72LanguageFeaturesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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
{
function foo(object $foo) {}
}

class ReturnTypeObjectTypeHint
{
function foo(): object {}
}

0 comments on commit afc610b

Please sign in to comment.