问题描述:

I'm doing some functional testing with phpunit and guzzle inside a Symfony 3.2 application.

I have multiple tests that need to load a database and login to an application before the tests run. This is set up like this:

<?php

namespace Tests\Legacy\Functional;

use GuzzleHttp\Client;

use GuzzleHttp\Cookie\CookieJar;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class UsersTest extends KernelTestCase

{

protected function setUp()

{

self::bootKernel();

$dir = self::$kernel->getRootDir()."/../Tests/Legacy/Functional";

exec('mysql < '.$dir.'/pagesTestData.sql');

}

protected function getCookieJar()

{

static $jar;

if (!isset($jar)){

$jar = new CookieJar;

}

return $jar;

}

protected function getClientAndLogin()

{

$client = new Client([

'base_uri' => 'http://functionaltest.thesite.example.com',

'cookies' => $this->getCookieJar()

]);

$loginPageResponse = $client->get('/index.php');

$csrf = [];

preg_match('#<input type="hidden" name="X-XSRF-TOKEN" value="(.*?)" />#ism', $loginPageResponse->getBody(), $csrf);

if ($csrf[1]){

$loginData = [

'email_address' => '[email protected]',

'password' => 'secure',

'X-XSRF-TOKEN' => $csrf[1],

'start' => 'Start!'

];

if ($client->post('/index.php', [

'form_params' => $loginData,

'cookies' => $this->getCookieJar()

])){

return $client;

}else{

throw new \Exception('Test login failed');

}

}else{

throw new \Exception('Csrf not enabled');

}

}

public function testUsers()

{

$client = $this->getClientAndLogin();

//actual test and

}

}

My phpunit.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"

backupGlobals="false"

colors="true"

bootstrap="var/bootstrap.php.cache"

>

<php>

<ini name="error_reporting" value="-1" />

<server name="KERNEL_DIR" value="app/" />

</php>

<testsuites>

<testsuite name="Project Test Suite">

<directory>Tests</directory>

</testsuite>

</testsuites>

<filter>

<whitelist>

<directory>src</directory>

<exclude>

<directory>src/*Bundle/Resources</directory>

<directory>src/*/*Bundle/Resources</directory>

<directory>src/*/Bundle/*Bundle/Resources</directory>

</exclude>

</whitelist>

</filter>

</phpunit>

When I extract setup() and getClientAndLogin() into a separate file and class "FunctionalShared" in the same folder, this is not found:

<?php

/**

* Created by PhpStorm.

* User: jochen

* Date: 6/01/17

* Time: 11:19 AM

*/

namespace Tests\Legacy\Functional;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class FunctionalShared extends KernelTestCase

{

protected function setUp()

{

...

protected function getCookieJar()

{

...

protected function getClientAndLogin()

{

...

class UsersTest extends FunctionalShared

{

/usr/bin/php /tmp/ide-phpunit.php --bootstrap /home/jochen/projects/zog2017/system/var/bootstrap.php.cache --configuration /home/jochen/projects/zog2017/system/phpunit.xml Tests\Legacy\Functional\UsersTest /home/jochen/projects/zog2017/system/Tests/Legacy/Functional/UsersTest.php

Testing started at 11:24 AM ...

PHP Fatal error: Class 'Tests\Legacy\Functional\FunctionalShared' not found in /home/jochen/projects/zog2017/system/Tests/Legacy/Functional/UsersTest.php on line 17

PHP Stack trace:

PHP 1. {main}() /tmp/ide-phpunit.php:0

PHP 2. IDE_Base_PHPUnit_TextUI_Command::main() /tmp/ide-phpunit.php:587

PHP 3. PHPUnit_TextUI_Command->run() /tmp/ide-phpunit.php:299

PHP 4. PHPUnit_Runner_BaseTestRunner->getTest() /usr/share/php/PHPUnit/TextUI/Command.php:149

PHP 5. PHPUnit_Runner_BaseTestRunner->loadSuiteClass() /usr/share/php/PHPUnit/Runner/BaseTestRunner.php:102

PHP 6. PHPUnit_Runner_StandardTestSuiteLoader->load() /usr/share/php/PHPUnit/Runner/BaseTestRunner.php:168

PHP 7. PHPUnit_Util_Fileloader::checkAndLoad() /usr/share/php/PHPUnit/Runner/StandardTestSuiteLoader.php:77

PHP 8. PHPUnit_Util_Fileloader::load() /usr/share/php/PHPUnit/Util/Fileloader.php:76

PHP 9. include_once() /usr/share/php/PHPUnit/Util/Fileloader.php:92

Process finished with exit code 255

网友答案:

In the UsersTest file you need to use the FunctionalShared class at the very beginning:

<?php
...
use Tests\Legacy\Functional\FunctionalShared;

class UsersTest extends FunctionalShared  
{
...

If you take a look at #5 in this Testing Setup Tutorial, you can see an example on how to properly extend a class.

相关阅读:
Top