Symfony 5 Api 测试 createClient() LogicalException

2024-02-12

正如标题所示,我正在使用 Symfony 5 构建一个 API。我有一些控制器需要不同的用户权限,我想测试它们,所以我决定创建两个具有不同角色的用户用于测试目的 -ROLE_USER and ROLE_ADMIN。 当前的代码是这样的(注意,这不是完整的代码,只是一个虚拟的示例/起点)

ApiTestCase.php

<?php

namespace App\Tests;

use App\Entity\User;
use App\Tests\Http\RequestBuilder;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ApiTestCase extends WebTestCase
{

    private static $userData = [
        'firstName' => 'Pera',
        'lastName'  => 'Peric',
        'email'     => '[email protected] /cdn-cgi/l/email-protection',
        'password'  => 'test123',
        'roles'     => [
            'ROLE_USER'
        ]
    ];

    private static $adminUserData = [
        'firstName' => 'Admin',
        'lastName'  => 'Adminovic',
        'email'     => '[email protected] /cdn-cgi/l/email-protection',
        'password'  => 'admin123',
        'roles'     => [
            'ROLE_ADMIN'
        ]
    ];

    public static function createTestUser()
    {
        $res = RequestBuilder::create(self::createClient())
            ->setMethod('POST')
            ->setUri('/api/v1/security/register')
            ->setJsonContent(self::$userData)
            ->getResponse();

        $data = $res->getJsonContent();
        return $data['data'];
    }

    public static function createTestAdminUser()
    {
        $res = RequestBuilder::create(self::createClient())
            ->setMethod('POST')
            ->setUri('/api/v1/security/register')
            ->setJsonContent(self::$adminUserData)
            ->getResponse();

        $data = $res->getJsonContent();
        var_dump($data);
        return $data['data'];
    }

    public static function deleteTestUser()
    {
        self::createClient()->getContainer()
            ->get('doctrine.orm.entity_manager')
            ->getRepository(User::class)
            ->deleteUserByEmail(self::$userData['email']);
    }

    public static function deleteTestAdminUser()
    {
        self::createClient()->getContainer()
            ->get('doctrine.orm.entity_manager')
            ->getRepository(User::class)
            ->deleteUserByEmail(self::$adminUserData['email']);
    }

}

LocationControllerTest.php


namespace App\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class LocationControllerTest extends ApiTestCase
{
    public static $user;
    public static $adminUser;

    public static function setUpBeforeClass()
    {
        self::$user = self::createTestUser();
        self::$adminUser = self::createTestAdminUser();
    }

    public function testUsersExist()
    {
        // this is jut to test out an idea
        $this->assertContains('ROLE_USER', self::$user['roles']);
        $this->assertContains('ROLE_ADMIN', self::$adminUser['roles']);
    }

    public static function tearDownAfterClass()
    {
        self::deleteTestUser();
        self::deleteTestAdminUser();
    }
}

当我运行测试时(sunit是一个别名php bin/phpunit):

➜  skeleton master ✗ (*?) sunit --filter LocationController                                                                                                              [10:12AM]
PHPUnit 7.5.17 by Sebastian Bergmann and contributors.

Testing Project Test Suite
E                                                                   1 / 1 (100%)

Time: 742 ms, Memory: 24.00 MB

There was 1 error:

1) App\Tests\LocationControllerTest::testUsersExist
LogicException: Booting the kernel before calling Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient() is not supported, the kernel should only be booted once. in /Users/shkabo/code/skeleton/vendor/symfony/framework-bundle/Test/WebTestCase.php:44
Stack trace:
#0 /Users/shkabo/code/skeleton/tests/ApiTestCase.php(45): Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient()
#1 /Users/shkabo/code/skeleton/tests/LocationControllerTest.php(16): App\Tests\ApiTestCase::createTestAdminUser()
#2 /Users/shkabo/code/skeleton/bin/.phpunit/phpunit-7.5-0/src/Framework/TestSuite.php(703): App\Tests\LocationControllerTest::setUpBeforeClass()
#3 /Users/shkabo/code/skeleton/bin/.phpunit/phpunit-7.5-0/src/Framework/TestSuite.php(746): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
#4 /Users/shkabo/code/skeleton/bin/.phpunit/phpunit-7.5-0/src/TextUI/TestRunner.php(652): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
#5 /Users/shkabo/code/skeleton/bin/.phpunit/phpunit-7.5-0/src/TextUI/Command.php(206): PHPUnit\TextUI\TestRunner->doRun(Object(PHPUnit\Framework\TestSuite), Array, true)
#6 /Users/shkabo/code/skeleton/bin/.phpunit/phpunit-7.5-0/src/TextUI/Command.php(162): PHPUnit\TextUI\Command->run(Array, true)
#7 /Users/shkabo/code/skeleton/bin/.phpunit/phpunit-7.5-0/phpunit(17): PHPUnit\TextUI\Command::main()
#8 /Users/shkabo/code/skeleton/vendor/symfony/phpunit-bridge/bin/simple-phpunit.php(291): include('/Users/shkabo/c...')
#9 /Users/shkabo/code/skeleton/bin/phpunit(13): require('/Users/shkabo/c...')
#10 {main}
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

我理解这个错误,但似乎无法找到它/这种方法的解决方案。而且我很可能以错误的方式做事。

在数据库中,创建了第一个用户,而当我尝试创建第二个用户时,第二个用户抛出此错误。

我想要实现的是我有(静态)方法,我可以调用并创建虚拟用户/位置/等。并在测试该特定控制器时使用它,并在完成该特定控制器的测试后销毁它/从数据库中删除它。

RequestBuilder https://github.com/nebkam/ Fluent-test/blob/master/src/RequestBuilder.php https://github.com/nebkam/fluent-test/blob/master/src/RequestBuilder.php


您可以致电self::ensureKernelShutdown()在按照官方建议创建您的客户端之前。

self::ensureKernelShutdown();
$sally = static::createClient();

我不确定这是否能解决这个问题,但我遇到了类似的问题,谷歌把我带到了这里。

https://github.com/symfony/symfony-docs/issues/12961 https://github.com/symfony/symfony-docs/issues/12961

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Symfony 5 Api 测试 createClient() LogicalException 的相关文章

随机推荐