Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Leave constructor intact by forcing a block #93

Merged
merged 1 commit into from

3 participants

@davedevelopment
Collaborator

In the event where the user passes constructor args to the 'god' method, we can
assume that they want their constructor to be called, by adding __construct to
the blocks array, the mock's parent's constructor will be called.

@davedevelopment davedevelopment Leave constructor intact by forcing a block
In the event where the user passes constructor args to the 'god' method, we can
assume that they want their constructor to be called, by adding __construct to
the blocks array, the mock's parent's constructor will be called.
43ad798
@adanlobato

Hi @padraic, we're using mockery (nice product, congrats!) and we would like to use the feature this PR adds.
Could you review and merge it please?

Thank you! :)

@padraic padraic merged commit 998714d into padraic:master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 8, 2012
  1. @davedevelopment

    Leave constructor intact by forcing a block

    davedevelopment authored
    In the event where the user passes constructor args to the 'god' method, we can
    assume that they want their constructor to be called, by adding __construct to
    the blocks array, the mock's parent's constructor will be called.
This page is out of date. Refresh to see the latest.
Showing with 11 additions and 3 deletions.
  1. +4 −3 library/Mockery/Container.php
  2. +7 −0 tests/Mockery/ContainerTest.php
View
7 library/Mockery/Container.php
@@ -129,6 +129,7 @@ public function mock()
$quickdefs = array_shift($args);
} elseif (is_array($arg)) {
$constructorArgs = array_shift($args);
+ $blocks[] = "__construct"; // Assume they want to use the actual constructor regardless of other requirements
} else {
throw new \Mockery\Exception(
'Unable to parse arguments sent to '
@@ -138,9 +139,9 @@ public function mock()
}
if (!is_null($name) && !is_null($class)) {
if (!$makeInstanceMock) {
- $mockName = \Mockery\Generator::createClassMock($class);
+ $mockName = \Mockery\Generator::createClassMock($class, null, null, $blocks);
} else {
- $mockName = \Mockery\Generator::createClassMock($class, null, null, array(), true);
+ $mockName = \Mockery\Generator::createClassMock($class, null, null, $blocks, true);
}
$result = class_alias($mockName, $name);
$mock = $this->_getInstance($name, $constructorArgs);
@@ -149,7 +150,7 @@ public function mock()
$mock = new \Mockery\Mock();
$mock->mockery_init($name, $this);
} elseif(!is_null($class)) {
- $mockName = \Mockery\Generator::createClassMock($class, null, null, array(), false, $partialMethods);
+ $mockName = \Mockery\Generator::createClassMock($class, null, null, $blocks, false, $partialMethods);
$mock = $this->_getInstance($mockName, $constructorArgs);
$mock->mockery_init($class, $this);
} elseif(!is_null($partial)) {
View
7 tests/Mockery/ContainerTest.php
@@ -95,6 +95,13 @@ public function testNamedMockWithConstructorArgsWithInternalCallToMockedMethod()
$this->assertEquals(123, $m->bar());
}
+ public function testNamedMockWithConstructorArgsButNoQuickDefsShouldLeaveConstructorIntact()
+ {
+ $m = $this->container->mock("MockeryTest_ClassConstructor2", array($param1 = new stdClass()));
+ $m->shouldDeferMissing();
+ $this->assertEquals($param1, $m->getParam1());
+ }
+
public function testNamedMockWithShouldDeferMissing()
{
$m = $this->container->mock("MockeryTest_ClassConstructor2", array($param1 = new stdClass()));
Something went wrong with that request. Please try again.