Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

issue.new

Edson Medina edited this page Jul 7, 2017 · 3 revisions

New instances

function blah () 
{
    $foo = new Foo();
    return $foo->bar();
}

Why is this a testing issue?

  • Instantiating a new object creates coupling (interdependence), blocking you from testing the caller code in isolation (without the callee)
  • Unit-tests should only test code units in isolation. Having external dependencies breaks that rule.

Possible refactorings

Dependency injection

Argument injection

function blah ($foo) 
{
    return $foo->bar();
}

Constructor injection

function __construct (Foo $foo) 
{
    $this->foo = $foo;
}
 
function blah () 
{
    return $this->foo->bar();
}