final class CreateTest extends DbWebTestCase
{
public function testCreate(): void
{
$this->loginAs('test_admin');
$this->client->request('GET', '/panel/solidcp/enterprise-dispatchers/create');
//тут динамическая подмена заголовка HTTP по сути это тоже касается вопроса,
//так как мокание происходит похожим образом через *.yaml
$this->setCustomHttpClientRespond('http://127.0.0.2:9003', ['HTTP/1.1 200 OK']);
$this->client->submitForm('Create', [
'form[name]' => $name = 'Test Enterprise Dispatcher',
'form[url]' => 'http://127.0.0.2:9003',
'form[login]' => 'test_login',
'form[password]' => 'test_password',
]);
$this->assertSame(302, $this->client->getResponse()->getStatusCode());
$crawler = $this->client->followRedirect();
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$this->assertStringContainsString('Enterprise Dispatchers', $crawler->filter('title')->text());
$this->assertStringContainsString($name, $crawler->filter('body')->text());
}
}
class DbWebTestCase extends WebTestCase
{
private EntityManagerInterface $em;
protected KernelBrowser $client;
protected function setUp(): void
{
parent::setUp();
$this->client = static::createClient();
$this->client->disableReboot();
$this->em = static::$kernel->getContainer()->get('doctrine')->getManager();
$this->em->getConnection()->beginTransaction();
$this->em->getConnection()->setAutoCommit(false);
}
protected function tearDown(): void
{
$this->em->getConnection()->rollback();
$this->em->close();
parent::tearDown();
}
protected function loginAs(string $name): void
{
$userRepository = $this->client->getContainer()->get(UserRepository::class);
$user = $userRepository->getByLogin($name);
$this->client->loginUser(UserMapper::mapUserToUserIdentity($user));
}
}
$this->client->getContainer()->set();
вызовет ошибкуSymfony\Component\DependencyInjection\Exception\InvalidArgumentException:
The "App\Service\Service" service is already initialized, you cannot replace it.
Когда идешь таким путем, то да - получается много компонентов (в данном случае классов/файлов).
Symfony\Component\DependencyInjection\Exception\InvalidArgumentException:
The "App\Service\Service" service is already initialized, you cannot replace it.
container->reset()
чтобы сбросить контейнер зависимостей и избежать ошибку выше, но со всеми подводными камнями.
С юнит тестами у меня нет проблем, там просто замокаем класс и всё проходит без каких проблем и разрастания кода.
Функциональное тестирование имитирует работу реального приложения, с настоящим контейнером зависимостей, ядром, базой данной и всеми сопутствующими деталями фреймворка. Я не могу никуда убрать проверку выше, она всё равно сработает. Проверка по сути и так является компонентом, и она только в одном месте.
Вас унесло не в ту сторону. Прочитайте мой вопрос ещё раз.
То, что вы предлагаете, ни насколько не решит проблему: