我有一个简单的类,它包含序列化/反序列化对象数组并将它们添加到会话中,我想测试它。
我遇到的错误是
我在运行测试时转储了环境,它说它正在运行
test
我可以确认这里接受的答案已经在我的framework.yaml中(Symfony PHPUnit:无法启动会话,因为标头已被发送)
我是否遗漏了一些概念?我怎样才能创建会话?或者我应该使用其他方法来进行此测试?
下面的一些代码片段:
class SessionManager
{
public function __construct(private RequestStack $requestStack, private SerializerInterface $serializer)
{
}
public function addAttendee(Attendee $attendee): void
{
$session = $this->requestStack->getSession();
$attendees = $this->getAttendees();
$session->set('attendees', $this->serializer->serialize([...$attendees, $attendee], 'json'));
}
/**
* @return Attendee[]
*/
public function getAttendees(): array
{
$attendees = $this->requestStack->getSession()->get('attendees');
return $this->serializer->deserialize($attendees, Attendee::class . '[]', 'json');
}
}
第一个测试方法(和错误)
/** @test */
public function it_can_set_an_array_of_attendees_into_the_session()
{
$sessionManager = static::getContainer()->get(SessionManager::class);
$attendee = new Attendee('firstname', 'lastname', 'female');
$sessionManager->addAttendee($attendee);
$attendees = $sessionManager->getAttendees();
$this->assertEquals('firstname', $attendees[0]->firstname);
}
1) App\Tests\Controller\SessionManagerTest::it_can_set_an_array_of_attendees_into_the_session
Symfony\Component\HttpFoundation\Exception\SessionNotFoundException: There is currently no session available.
.../vendor/symfony/http-foundation/RequestStack.php:105
.../src/Controller/SessionManager.php:17
.../tests/Controller/SessionManagerTest.php:48
.../vendor/phpunit/phpunit/phpunit:107
尝试创建并注入会话(和错误)
/** @test */
public function it_can_set_an_array_of_attendees_into_the_session()
{
// Create and start session, add to a Request, add Request to RequestStack, set RequestStack into container
$session = new Session();
$session->start();
$request = new Request();
$request->setSession($session);
$requestStack = new RequestStack();
$requestStack->push($request);
static::getContainer()->set(RequestStack::class, $requestStack);
$sessionManager = static::getContainer()->get(SessionManager::class);
$attendee = new Attendee('firstname', 'lastname', 'female');
$sessionManager->addAttendee($attendee);
$attendees = $sessionManager->getAttendees();
$this->assertEquals('firstname', $attendees[0]->firstname);
}
1) App\Tests\Controller\SessionManagerTest::it_can_set_an_array_of_attendees_into_the_session
RuntimeException: Failed to start the session because headers have already been sent by ".../vendor/phpunit/phpunit/src/Util/Printer.php" at line 104.
.../vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php:116
.../vendor/symfony/http-foundation/Session/Session.php:59
.../tests/Controller/SessionManagerTest.php:46
.../vendor/phpunit/phpunit/phpunit:107
我不确定为什么流会输出在 Printer.php 的第 104 行。
尝试使用 Symfony 的 MockFileSessionStorage 进行单元测试。
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
class UnitTest extends KernelTestCase
{
public function it_can_set_an_array_of_attendees_into_the_session()
{
$session = new Session(new MockFileSessionStorage());
// ...
}
}