私はまだphpspecに慣れていませんが、通常、何かに苦労しているときに解決策を見つけていますが、これは難しいです。
さまざまなアプローチを試しましたが、解決策は見つかりませんでした。Symfony2 を使用しています。
テストしたいクラスがあります:
class MyClass
{
public function getDataForChildren(MyObject $object)
{
foreach ($object->getChildren() as $child) {
$query = \json_decode($child->getJsonQuery(), true);
$data = $this->someFetcher->getData($query);
$child->setData($data);
}
return $object;
}
}
そして、これが私の仕様クラスをどのように見ているかです:
class MyClassSpec
{
function let(SomeFetcher $someFetcher)
{
$this->beConstructedWith($someFetcher);
}
function it_is_initializable()
{
$this->shouldHaveType('MyClass');
}
function it_should_get_data_for_children_and_return_object(
MyClass $object,
MyClass $child, // it means that MyClass has a self-reference to MyClass
$someFetcher
)
{
$query = '{"id":1}';
$returnCollection = new ArrayCollection(array($child));
$object->getChildren()->shouldBeCalled()->willReturn($returnCollection);
$child->getJsonQuery()->shouldBeCalled()->willReturn($query);
$someFetcher->getData($query)->shouldBeCalled();
$this->getDataForChildren($object);
}
}
phpspec を実行した後、次のエラーが表示されます。
warning: json_decode() expects parameter 1 to be string, object given in
この問題を解決する方法がわかりません。誰か手がかりがある場合は、助けてください。