Php 方法返回对象而不是字符串
我对phpspec还不熟悉,但通常我会在遇到困难时找到解决办法,但这一点很难 我尝试了许多不同的方法,但没有找到解决办法。我用的是Symfony2 我想测试一个类:Php 方法返回对象而不是字符串,php,symfony,testing,phpspec,Php,Symfony,Testing,Phpspec,我对phpspec还不熟悉,但通常我会在遇到困难时找到解决办法,但这一点很难 我尝试了许多不同的方法,但没有找到解决办法。我用的是Symfony2 我想测试一个类: class MyClass { public function getDataForChildren(MyObject $object) { foreach ($object->getChildren() as $child) { $query = \json_deco
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
我不知道如何解决这个问题。如果有人有线索,请提供帮助。这是PhpSpec的常见障碍,声明:
MyClass $child
意味着$child的Collaborator对象将使用MyClass的相同接口进行设置。
在SUT(您正在测试的类)中调用child->getJsonQuery()时,它将返回MethodProphecy,而不是您期望它返回的字符串
您想说的是,您的ArrayCollection将包含的不是$child本身(它是一个Collaborator对象),而是Collaborator围绕的真实对象。你是这样做的:
$returnCollection = new ArrayCollection(array($child->getWrappedObject()));
class MyClass
{
/**
* @var SomeFetcher
*/
private $someFetcher;
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;
}
public function getJsonQuery()
{
}
public function setData()
{
}
public function __construct(SomeFetcher $someFetcher)
{
$this->someFetcher = $someFetcher;
}
}
此外,您不应同时使用(即is是多余的)
应在同一个合作者(一个或多个)上调用()和willReturn()
其他的就足够了。如果您已指定协作者将执行的操作
返回,很明显,它将在SUT中被称为WIT。
应在中的测试的“断言”部分中使用shouldBeCalled()
以确认已使用预期的
争论,或在正确的时间
您的最终SUT和规范应如下所示:
$returnCollection = new ArrayCollection(array($child->getWrappedObject()));
class MyClass
{
/**
* @var SomeFetcher
*/
private $someFetcher;
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;
}
public function getJsonQuery()
{
}
public function setData()
{
}
public function __construct(SomeFetcher $someFetcher)
{
$this->someFetcher = $someFetcher;
}
}
还有,线路
$query = \json_decode($child->getJsonQuery(), true);
将在$query中生成一个关联数组,即数组('id'=>1)(这是json_encode的第二个'true'参数所规定的),因此您希望使用后者调用$someFetcher->getData(),因此:
$someFetcher->getData(array('id' => 1))->shouldBeCalled();
这是PhpSpec的一个常见障碍,声明:
MyClass $child
意味着$child的Collaborator对象将使用MyClass的相同接口进行设置。
在SUT(您正在测试的类)中调用child->getJsonQuery()时,它将返回MethodProphecy,而不是您期望它返回的字符串
您想说的是,您的ArrayCollection将包含的不是$child本身(它是一个Collaborator对象),而是Collaborator围绕的真实对象。你是这样做的:
$returnCollection = new ArrayCollection(array($child->getWrappedObject()));
class MyClass
{
/**
* @var SomeFetcher
*/
private $someFetcher;
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;
}
public function getJsonQuery()
{
}
public function setData()
{
}
public function __construct(SomeFetcher $someFetcher)
{
$this->someFetcher = $someFetcher;
}
}
此外,您不应同时使用(即is是多余的)
应在同一个合作者(一个或多个)上调用()和willReturn()
其他的就足够了。如果您已指定协作者将执行的操作
返回,很明显,它将在SUT中被称为WIT。
应在中的测试的“断言”部分中使用shouldBeCalled()
以确认已使用预期的
争论,或在正确的时间
您的最终SUT和规范应如下所示:
$returnCollection = new ArrayCollection(array($child->getWrappedObject()));
class MyClass
{
/**
* @var SomeFetcher
*/
private $someFetcher;
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;
}
public function getJsonQuery()
{
}
public function setData()
{
}
public function __construct(SomeFetcher $someFetcher)
{
$this->someFetcher = $someFetcher;
}
}
还有,线路
$query = \json_decode($child->getJsonQuery(), true);
将在$query中生成一个关联数组,即数组('id'=>1)(这是json_encode的第二个'true'参数所规定的),因此您希望使用后者调用$someFetcher->getData(),因此:
$someFetcher->getData(array('id' => 1))->shouldBeCalled();
警告很清楚,
$child->getJsonQuery()
是一个对象,json\u decode需要字符串,在类中查找是否有类似$child->getJsonQuery()->>jsonString()
或类似的方法有:$child->getJsonQuery()
并且它的存根是:$child->getJsonQuery()->()->shouldBeCalled()->willReturn($query)代码>$child
是一个实体,jsonQuery是其中的一个字段,因此当调用$child->getJsonQuery()
时,我认为它将返回字符串(因为我将其存根)。警告很清楚,$child->getJsonQuery()
是一个对象,json\U decode需要字符串,查看类中是否有类似于$child->getJsonQuery()->jsonString()
或类似的方法有:$child->getJsonQuery()
并且它是存根的:$child->getJsonQuery()->shouldBeCalled()->willReturn($query)代码>$child
是一个实体,而jsonQuery是其中的一个字段,因此当调用$child->getJsonQuery()
时,我认为它将返回字符串(因为我将其存根)。