Este foro ya no está activo, así que no puedes publicar nuevas preguntas ni responder a las preguntas existentes.

Error al extender de WebTestCase

23 de noviembre de 2016

Buenas noches. Estoy intentando extender la clase WebTestCase pero al ejecutar phpunit me devuelve el siguiente error:

PHP Fatal error: Class 'tests\AppBundle\Fixtures\AbstractTestCase' not found in ../tests/AppBundle/Controller/GroupControllerTest.php on line 11

La lógica de los controladores no afecta, falla igualmente. Estoy usando parte de código de EasyAdminBundle para crear la base de datos de prueba y las fixtures, parte que funciona correctamente.

Espero me puedan ayudar, saludos.

#phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?>
 
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="true"
         bootstrap="tests/bootstrap.php"
>
    <php>
        <ini name="error_reporting" value="-1" />
    </php>
 
    <testsuites>
        <testsuite name="App Test Suite">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
 
    <php>
        <server name="KERNEL_DIR" value="app/" />
        <ini name="error_reporting" value="E_ALL" />
        <ini name="display_errors" value="1" />
        <ini name="display_startup_errors" value="1" />
        <ini name="memory_limit" value="-1" />
        <ini name="zend.enable_gc" value="1" />
    </php>
 
    <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>
#bootstrap.php
<?php
 
$file = __DIR__.'/../vendor/autoload.php';
if (!file_exists($file)) {
    throw new RuntimeException('Install dependencies to run test suite.');
}
$autoload = require $file;
 
// Test Setup: remove all the contents in the build/ directory
// (PHP doesn't allow to delete directories unless they are empty)
if (is_dir($buildDir = 'build/')) {
    $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($buildDir, RecursiveDirectoryIterator::SKIP_DOTS),
        RecursiveIteratorIterator::CHILD_FIRST
    );
    foreach ($files as $fileinfo) {
        $fileinfo->isDir() ? rmdir($fileinfo->getRealPath()) : unlink($fileinfo->getRealPath());
    }
}
 
//drop/create database
system(sprintf('php "bin/console" doctrine:database:drop --env=test --force', __DIR__));
//create database
system(sprintf('php "bin/console" doctrine:database:create --env=test', __DIR__));
//init database
system(sprintf('php "bin/console" doctrine:schema:create --env=test', __DIR__));
//insert fixtures
system(sprintf('php "bin/console" doctrine:fixtures:load --env=test -n', __DIR__));
 
// Make a copy of the original SQLite database to use the same unmodified database in every test
copy($buildDir.'/test.db', $buildDir.'/original_test.db');
<?php
 
namespace tests\AppBundle\Fixtures;
 
use GuzzleHttp\Client;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
 
abstract class AbstractTestCase extends WebTestCase
{
    protected $client;
 
    protected function setUp()
    {
        $this->initClient();
        $this->initDataBase();
    }
 
    protected function initClient()
    {
        $this->client = new Client([
            'base_uri' => 'http://127.0.0.1:8000'
        ]);
    }
 
    protected function initDataBase()
    {
        $buildDir = 'build/';
        copy($buildDir.'/original_test.db', $buildDir.'/test.db');
    }
 
    protected function createAuthenticatedClient($username = 'Antonio', $password = 'antonio')
    {
        $this->client = static::createClient();
        $this->client->request(
            'POST',
            '/login',
            array(
                'username' => $username,
                'password' => $password,
            )
        );
 
        $data = json_decode($this->client->getResponse()->getContent(), true);
 
        $client = static::createClient();
        $client->setServerParameter('HTTP_Authorization', sprintf('Bearer %s', $data['token']));
 
        return $client;
    }
}
<?php
 
namespace tests\AppBundle\Controller;
 
use tests\AppBundle\Fixtures\AbstractTestCase;
 
class GroupControllerTest extends AbstractTestCase
{
    public function testGetAllGroups()
    {
        $response = $this->client->get('/groups');
 
        $this->assertEquals(200, $response->getStatusCode());
 
        $data = json_decode($response->getBody(), true);
        $this->assertEquals('Friends', $data[1]['name']);
    }