Skip to content

Commit

Permalink
Merge d997857 into 9a2306a
Browse files Browse the repository at this point in the history
  • Loading branch information
seyfahni committed Apr 10, 2019
2 parents 9a2306a + d997857 commit 0af9b29
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## 0.9.11 (2019-02-12)

* Set property values on all related mocks #944
* Backported from master: Added possibility to add Constructor-Expectations on hard dependencies, read: Mockery::mock('overload:...')

## 0.9.4 (2015-04-02)

Expand Down
Expand Up @@ -36,6 +36,8 @@ public function __construct()
}
}
\Mockery::getContainer()->rememberMock(\$this);
\$this->_mockery_constructorCalled(func_get_args());
}
MOCK;

Expand Down
17 changes: 15 additions & 2 deletions library/Mockery/Mock.php
Expand Up @@ -675,8 +675,7 @@ protected function _mockery_handleMethodCall($method, array $args)
return call_user_func_array("parent::$method", $args);
}

if (isset($this->_mockery_expectations[$method])
&& !$this->_mockery_disableExpectationMatching) {
if (isset($this->_mockery_expectations[$method]) && !$this->_mockery_disableExpectationMatching) {
$handler = $this->_mockery_expectations[$method];

try {
Expand Down Expand Up @@ -755,4 +754,18 @@ function ($method) {
})
);
}

/**
* Called when an instance Mock was created and its constructor is getting called
*
* @see \Mockery\Generator\StringManipulation\Pass\InstanceMockPass
* @param array $args
*/
protected function _mockery_constructorCalled(array $args)
{
if (!isset($this->_mockery_expectations['__construct']) /* _mockery_handleMethodCall runs the other checks */) {
return;
}
$this->_mockery_handleMethodCall('__construct', $args);
}
}
38 changes: 38 additions & 0 deletions tests/Mockery/ContainerTest.php
Expand Up @@ -743,6 +743,44 @@ public function testSettingPropertyOnInstanceMockWillSetItOnActualInstance()
Mockery::resetContainer();
}

public function testInstantiationOfInstanceMockWithConstructorParameterValidation()
{
$m = Mockery::mock('overload:MyNamespace\MyClass14');
$params = array(
'value1' => uniqid('test_')
);
$m->shouldReceive('__construct')->with($params);

new MyNamespace\MyClass14($params);
}

/**
* @expectedException \Mockery\Exception\NoMatchingExpectationException
*/
public function testInstantiationOfInstanceMockWithConstructorParameterValidationNegative()
{
$m = Mockery::mock('overload:MyNamespace\MyClass15');
$params = array(
'value1' => uniqid('test_')
);
$m->shouldReceive('__construct')->with($params);

new MyNamespace\MyClass15(array());
}

/**
* @expectedException \Exception
* @expectedExceptionMessageRegExp /^instanceMock \d{3}$/
*/
public function testInstantiationOfInstanceMockWithConstructorParameterValidationException()
{
$m = Mockery::mock('overload:MyNamespace\MyClass16');
$m->shouldReceive('__construct')
->andThrow(new \Exception('instanceMock '.rand(100, 999)));

new MyNamespace\MyClass16();
}

public function testMethodParamsPassedByReferenceHaveReferencePreserved()
{
$m = $this->container->mock('MockeryTestRef1');
Expand Down
Expand Up @@ -20,5 +20,6 @@ public function shouldAppendConstructorAndPropertyForInstanceMock()
$code = $pass->apply('class Dave { }', $config);
$this->assertContains('public function __construct', $code);
$this->assertContains('protected $_mockery_ignoreVerification', $code);
$this->assertContains('this->_mockery_constructorCalled(func_get_args());', $code);
}
}

0 comments on commit 0af9b29

Please sign in to comment.