Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify parameters should validate an object state at that instance #229

Open
simonhayre opened this issue Jan 10, 2017 · 0 comments
Open

Comments

@simonhayre
Copy link

When verifying the parameters that are passed into a method, the state of an object that is passed into the method must match the state at that moment and not the final state of the object.

Example:

/********************************
   ********* ValueObject **********
   ********************************/
class MyObject
{
  private $var1;
  private $var2;

  public function setVar1($var1)
  {
    $this->var1 = $var1;
  }

  public setVar2($var2) 
  {
    $this->var2 = $var2;
  }
}

/********************************
   ********** Sub Class **********
   ********************************/
interface SubClass
{
  public function call1($obj);
  public function call2($obj);
}

/********************************
   ************ Class *************
   ********************************/
class MyClass
{
  /** @var MySubClass */
  private $subClass;
  public function __construct(SubClass $subClass)
  {
    $this->subClass = $subClass;
  }

  public function run(MyObject $obj) 
  {
    $obj->setVar1('CHANGED');
    $this->subClass->call1($obj);
    $obj->setVar2('CHANGED');
    $this->subClass->call2($obj);
  }
}

/********************************
   ************ Test **************
   ********************************/
class MyTest extends \PHPUnit_Framework_TestCase
{ 
  public function test()
  {
    $mock = \Phake::mock(MySubClass::class);
  
    $test = new MyClass($mock)
    $obj = new MyObject();
    $obj->setVar1('unchanged');
    $obj->setVar2('unchanged');

    $test->run($obj);

    \Phake::verify($mock)->call1(
      (new MyObject())
        ->setVar1('CHANGED')
        ->setVar2('unchanged') // <-- This will fail, because the final state 
// of the object has changed to 'CHANGED'
    );

    \Phake::verify($mock)->call2(
      (new MyObject())
        ->setVar1('CHANGED')
        ->setVar2('CHANGED')
    );
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant