这是我的Symfony项目,我正在练习功能测试,但在测试我的函数时出现了这样的错误。
这里是出错的代码段:
<?php
namespace App\Tests;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use App\Entity\Category;
class AdminControllerCategoriesTest extends WebTestCase
{
public function setUp():void
{
parent::setUp();
$this->client = static::createClient();
$this->entityManager = $this->client->getContainer()->get('doctrine.orm.entity_manager');
$this->entityManager->beginTransaction();
$this->entityManager->getConnection()->setAutoCommit(false);
}
public function tearDown():void
{
parent::tearDown();
$this->entityManager->rollback();
$this->entityManager->close();
$this->entityManager = null; //avoid memory leaks
}
public function testTextOnPage()
{
$crawler = $this->client->request('GET', '/admin/categories');
$this->assertSame('Categories list', $crawler->filter('h2')->text());
$this->assertContains('Electronics', $this->client->getResponse()->getContent());
}
public function testNumberOfItems()
{
$crawler = $this->client->request('GET', '/admin/categories');
$this->assertCount(21, $crawler->filter('option'));
}
}
这里是我的 .env 文件,里面存放着我的数据库连接信息:
# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
# * .env contains default values for the environment variables needed by the app
# * .env.local uncommitted file with local overrides
# * .env.$APP_ENV committed environment-specific defaults
# * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
#
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
#
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration
###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=018d7408d23791c60854cbb4fc65b667
###< symfony/framework-bundle ###
###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
DATABASE_URL="mysql://root:@127.0.0.1:3306/symf5?serverVersion=mariadb-10.4.11"
# DATABASE_URL="postgresql://symfony:ChangeMe@127.0.0.1:5432/app?serverVersion=13&charset=utf8"
###< doctrine/doctrine-bundle ###
这里,我在我的.env.test文件中有以下代码:
# define your env variables for the test env here
KERNEL_CLASS='App\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
我不知道问题出在哪里,我尝试了不同的方法,但都没有成功,而且我也不知道出了什么问题以及该怎么做。希望你们能帮我解决问题。
谢谢!