Frage

Exist some class:

class A{
  protected $options;
  public function setDefaults($defaults){
      $this->options=$defaults;
  }
  public function getOptions(){
     return  $this->options;
  }
}

And other class:

class B{
public function setDefaultOptions(A $options){
   $options->setDefaults('foo'=>'bar')
}

}

Uses:

$a=new A();
$b=new B();
$b->setDefaultsOptions(a);

How I can check in spec for B that $a->options['foo'] is set?


Now I implement it as:

  public function getMatchers(){
    return array('haveResolverKey'=>
    function($subject, $key){
        $resolver=new A();
        $subject->setDefaultOptions($resolver);
        $ret=$resolver->getOptions();

        return isset($ret[$key]);
    });
  }

Maybe exist easy way?

War es hilfreich?

Lösung

How I can check in spec for B that $a->options['foo'] is set?

You shouldn't. Verify the behaviour instead of a state.

In other words you should verify outputs of methods and express expectations against collaborators, instead of verifying the internal state of your classes.

Internals might change once you refactor, behaviour should remain the same.

As far as I understood your example, it could be described as:

class BSpec extends ObjectBehavior
{
    function it_sets_defaults_on_a(A $a)
    {
        $a->setDefaults(array('foo' => 'bar'))->shouldBeCalled();

        $this->setDefaultOptions($a);
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top