我按照Symfony关于功能测试的文档来编写我的第一个,但是我有一些问题.我通过浏览器得到的响应很好:
但是当我phpunit -c app/在shell中运行时,我会失败.
1)AppBundle\Tests\Controller\MeterAPIControllerTest :: testGetAllVariables声明500匹配预期200失败.
这是代码:
<?php
namespace AppBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class MeterAPIControllerTest extends WebTestCase
{
public function testGetAllVariables()
{
$client = static::createClient();
$crawler = $client->request(
'GET',
'/meters/121/120/variables'
);
// Assert a specific 200 status code
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试另一个测试断言,我也会失败.
// Assert that the "Content-Type" header is "application/json"
$this->assertTrue(
$client->getResponse()->headers->contains(
'Content-Type',
'application/json'
)
);
Run Code Online (Sandbox Code Playgroud)
编辑
当我运行时phpunit,app/logs/test.log我得到一个PHP异常:
[2016-03-31 15:25:21] request.CRITICAL:未捕获的PHP Exception Doctrine\Common\Persistence\Mapping\MappingException:"类AppBundle\Entity的映射文件'AppBundle.Entity.EM2Meter.orm.yml'无效\ EM2Meter"." at /Applications/MAMP/htdocs/iso50k1_tst_symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php第86行{"exception":"[object](Doctrine\Common\Persistence\Mapping\MappingException) (代码:0):类'AppBundle\Entity\EM2Meter'的映射文件'AppBundle.Entity.EM2Meter.orm.yml'无效.在/ Applications/MAMP/htdocs/iso50k1_tst_symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:86)"} [] …
我曾经json_encode($array);把数组转换为json.
[{"x":1418736600,"y":"82.2"},{"x":1418736900,"y":"82.2"}]
Run Code Online (Sandbox Code Playgroud)
但我真正需要的是一个JavaScript对象,如下所示:
[{x:1418736600,y:"82.2"},{x:1418736900,y:"82.2"}]
Run Code Online (Sandbox Code Playgroud)
为了简化它我想要一个没有引用键的JSON,但最好避免解析什么json_encode输出并使用更直接的方式.
是否可以在PHP中执行此操作?