padraic/mockery — pull request #74 by davedevelopment (2012-07-27 13:28:42+00:00)
Title: Constructor arguments for partials

It's probably a problem of my class/object design, but I like to isolate methods as much as possible, so find myself mocking methods in the classes own public API by manually calling the constructor of partial mocks.

There's some discussion of this in #33.

e.g.

``` php
arg1 = $arg1;
$this->arg2 = $arg2;
}

public function foo()
{
// some complicated stuff
}

public function bar()
{
$this->foo();
$this->arg1->doSomething();
}

}

$arg1 = m::mock(array("doSomething" => true));
$arg2 = m::mock();

// Before this PR

$m = m::mock("Foo[bar]", array("foo" => 123));
$m->__construct($arg1, $arg2);
$m->bar();

// or if you really dont want to call the constructor
$c = get_class(m::mock("Foo[bar]", array("foo" => 123)));
$m = new $c($arg1, $arg2);
m::getContainer()->rememberMock($m); // optional if you verify manually

// After this PR
$m = m::mock("Foo[bar]", array("foo" => 123), array($arg1, $arg2));

```

I'm still left with the problem that my tests need to know how to instantiate the object, so perhaps a new method could be added:

``` php
shouldDeferMissing();

// in test
$m->shouldReceive("foo")->andReturn(123);
$m->bar(); // calls parent method

```

Will add a separate PR if that sounds like a reasonable idea?