vendredi 27 février 2015

PHPUnit > Best approach to mock container objects? (e.g. $item->id)


I'm using PHPUnit test doubles to mock models, libraries etc. But when I want to return an array perhaps of container objects, how best to do so. Here is what I'm currently doing:



/**
* This is just a mock of the response from http client
*/
class Container {
public $values;

public function __construct($values) {
$this->values = $values;
}

public function __get($name) {
return (isset($values[$name])) ? $values[$name] : null;
}

public function __set($name, $value) {
$values[$name] = $value;
}
}


.. then in my test* methods I may do something like:



$userMock = new Container(array('id' => 2, 'name'=>'Tom'));
$this->mock
->method('find') // called within BaseController
->with(2)
->willReturn( $userMock );


So I don't know if I can use PHPUnit tets doubles or Mockery to create such classes, can I? It seems simpler to just do this way, is this a good approach though?





Aucun commentaire:

Enregistrer un commentaire