我想在EasyAdmin 3后端运行功能测试。
基本上,我希望确保普通用户不能访问页面、查看字段或查看/运行他们不允许执行的操作。
最好的方法是什么?有没有什么有用的资源我错过了入门?
发布于 2021-05-17 22:43:46
EasyAdmin 3CRUD控制器基本上是普通的Symfony控制器,因此它们可以作为任何其他Symfony控制器进行测试。
<?php
// tests/Controller/AdminControllerTest.php
namespace App\Tests\Controller;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class AdminControllerTest extends WebTestCase
{
// ...
public function testVisitingWhileLoggedIn()
{
$client = static::createClient();
$userRepository = static::$container->get(UserRepository::class);
// retrieve the test user
$testUser = $userRepository->findOneByEmail('john.doe@example.com');
// simulate $testUser being logged in
$client->loginUser($testUser);
// test e.g. the admin page
$client->request('GET', '/admin');
$this->assertResponseStatusCodeSame(403);
}
}EasyAdmin Crud文档https://symfony.com/doc/current/bundles/EasyAdminBundle/crud.html
https://stackoverflow.com/questions/67022489
复制相似问题