Symfony测试,发出请求找不到路由

u7up0aaq  于 2023-05-23  发布在  其他
关注(0)|答案(3)|浏览(152)

Symfony 6酒店
我正在写一个WebTestCase。
待测控制器传递json编码的数组数据,工作正常。
但是测试本身不能从URL加载。我得到的是一个404错误。
URL是:http://masch1/company/public/get/data?active=1&sort=desc

这是我的考验

<?php

// php bin/phpunit tests/Controller/DataControllerTest.php
namespace App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class DataControllerTest extends WebTestCase
{
    public function testSomething(): void
    {
        $client = static::createClient();
        $crawler = $client->request('GET', 'http://masch1/company/public/get/data?active=1&sort=desc');

        $this->assertResponseIsSuccessful();
        $this->assertSelectorTextContains('h1', 'Hello World');
    }
}

教程本身说硬编码请求URL是应用程序测试的最佳实践。
在绝望中,我尝试了不同的组合。

  • http://masch1/company/public/get/data\
  • /company/public/get/data
  • /public/get/data

等等
但我在跑步时得到的只有:
Copyright © 2018 - 2019 www.symfony.com. All Rights Reserved.粤ICP备15048888号-1
由ErrorException引起:No route found for“GET http://localhost/company/public/get/data”in /var/www/company/vendor/symfony/http-kernel/EventListener/RouterListener.php:127堆栈跟踪:#0 /var/www/company/vendor/symfony/framework-bundle/Test/BrowserKitAssertionsTrait.php(33):Copyright © 2018 www.cn-china.com All Rights Reserved.京ICP备15004855号-1京公网安备11010502000112号Copyright © 2018 www.cnjs.com. All Rights Reserved.粤ICP备15044866号-1/var/www/company/vendor/phpunit/phpunit/src/Framework/TestCase.php(1214):Copyright © 2018 - 2019 www.cnjs.com. All Rights Reserved.粤ICP备16048888号-1 Copyright © 2018 - 2019 www.cnjs.com. All Rights Reserved.粤ICP备15044552号-1 Copyright © 2016 - 2018 www.cnjs.com. All Rights Reserved.粤ICP备16048888号-1 Copyright © 2016 - 2018 www.cnjs.com. All Rights Reserved.粤ICP备16048888号-1 Copyright © 2016 - 2018 www.cnjs.com. All Rights Reserved.粤ICP备16048888号-1/var/www/company/vendor/phpunit/phpunit/src/TextUI/Command.php(97):PHPUnit\TextUI\Command->run()#10 /var/www/company/bin/phpunit(11):PHPUnit\TextUI\Command::main()#11 {main}

euoag5mw

euoag5mw1#

可能是因为您在测试中使用了完整的URL而不是相对URL。WebTestCase类自动创建一个模拟浏览器的客户端,但它实际上并不发送HTTP请求,而是直接调用Symfony应用程序。
请尝试以下代码:

<?php

// php bin/phpunit tests/Controller/DataControllerTest.php
namespace App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class DataControllerTest extends WebTestCase
{
    public function testSomething(): void
    {
        $client = static::createClient();
        // Use a relative path instead of a full URL
        $crawler = $client->request('GET', '/get/data?active=1&sort=desc');

        $this->assertResponseIsSuccessful();
        $this->assertSelectorTextContains('h1', 'Hello World');
    }
}

这样,客户端将自动使用默认的域和端口(通常是localhost:8000)来访问应用程序。当然,您也可以在创建客户端时传递数组参数来更改默认值:

$client = static::createClient(['HTTP_HOST' => 'masch1']);

有关WebTestCase类和客户端的详细信息,请参阅以下链接:

fjaof16o

fjaof16o2#

尝试在控制台中执行以下命令:

php bin/console debug:router

然后使用“路径”列中的值。路径不需要指定协议和域名:

$client->request('GET', '/value/from/path/column');

如果此建议没有帮助,请显示如何描述到控制器的路由,以及上面提到的命令显示的值。

vof42yt1

vof42yt13#

我所需要的只是正确的插入URL的方法。

<?php

// php bin/phpunit tests/Controller/DataControllerTest.php
namespace App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class DataControllerTest extends WebTestCase
{
    public function testSomething(): void
    {
        $client = static::createClient(['HTTP_HOST' => 'masch1']);
        $client->request('GET', '/get/data?active=1&sort=desc');
        $response = $client->getResponse();
        $content = $response->getContent();
        
        // Check json data with first record
        $jsonData = json_decode($content, true)[0];

        // check fields
        $this->assertArrayHasKey('field1', $jsonData);
        $this->assertArrayHasKey('field2', $jsonData);
        $this->assertArrayHasKey('field3', $jsonData);

        // Check field types
        $this->assertIsInt($jsonData['field1']);

        $this->assertEquals(200, $response->getStatusCode());

        $this->assertResponseIsSuccessful();
        
    }
}

相关问题