1
1
1alexandr2016-11-22 17:32:07
symfony
1alexandr, 2016-11-22 17:32:07

Problem with phpunit. How to inherit a class?

I want to move the code to a separate class and inherit it by other classes... When I run tests, I get a fatality:

PHP Fatal error:  Class 'App\Bundle\CoreBundle\Tests\Repository\DoctrineTestCase' not found in /home/user/desktop/app/tests/AppCoreBundle/Repository/CategoryRepositoryTest.php on line 8

If nothing goes anywhere - everything works. Please tell me where and what I'm doing wrong.
namespace App\Bundle\CoreBundle\Tests\Repository;

use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class DoctrineTestCase extends KernelTestCase 
{
    /**
     * @var EntityManager
     */
    protected $em;

    protected function setUp()
    {
        self::bootKernel();

        $this->em = static::$kernel->getContainer()
            ->get('doctrine')
            ->getManager();
    }

    protected function tearDown()
    {
        parent::tearDown();

        $this->em->close();
        $this->em = null;
    }
...
}

namespace App\Bundle\CoreBundle\Tests\Repository;

use App\Bundle\CoreBundle\Entity\Category;

class CategoryRepositoryTest extends DoctrineTestCase
{
...
}

use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;

/** @var ClassLoader $loader */
$loader = require __DIR__.'/../vendor/autoload.php';

AnnotationRegistry::registerLoader([$loader, 'loadClass']);

return $loader;

<?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="app/autoload.php"
>
    <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>

The problem was in the composer autoloader:
"autoload": {
        "psr-4": {
            "": "src/"
        },
        "classmap": [
            "app/AppKernel.php",
            "app/AppCache.php"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },

Decided to move DoctrineTestCase to src. Although it would be possible to leave it in the same place, shamanizing with exclusions.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Anisimov, 2016-11-25
@AnisimovAM

Good afternoon.
You have a problem with Namespace.
You specified App\Bundle\CoreBundle\Tests\Repository;
And the files are in app/tests/AppCoreBundle/Repository;

D
devalone, 2017-03-04
@devalone

Maybe they forgot to install grub, maybe they forgot to enable boot from the railway in bios

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question