找不到 Symfony 2 和 PhpUnit Class

Symfony 2 and PhpUnit Class not found

我不知道如何为我的问题找到解决方案,我已经在互联网(和 Whosebug)上搜索了很多,但对我来说没有任何问题...

问题

我有一个 Symfony 2 应用程序。我用 PHPUnit 创建了一个测试,但是当我执行测试时,出现了这些异常:

1) RepositoryBundle\Tests\DAO\UserDAOTest::测试创建 错误:Class 'RepositoryBundle\Tests\DAO\User' 未找到

而且我不知道为什么...

<?php

namespace RepositoryBundle\Tests\DAO;

use RepositoryBundle\Entity;
use RepositoryBundle\DAO;

class UserDAOTest extends \PHPUnit_Framework_TestCase
{
    public function testCreate()
    {
        $user1 = new User();
        $user1.setName("TestName");
        $user1.setEmail("TestEmail");
        $user1.setPassword("TestPassword");
        $user1.setMemberNumber(12345);
        $user1.setAdmin(true);
        $user1.setRole(1);

        $UserDAO = FactoryDAO::getInstance("UserDAO");
        $user1 = $UserDAO.save($user1);

        $this->assertTrue($user1.getId() > 0);
        $this->assertEquals("TestName", $user1.getName());
        $this->assertEquals("TestEmail", $user1.getEmail());
        $this->assertEquals("TestPassword", $user1.getPassword());
        $this->assertEquals(12345, $user1.getMemberNumber());
        $this->assertEquals(true, $user1.isAdmin());
        $this->assertEquals(1, $user1.getRole());
    }
}

其他使用线相同class可以。

要执行我使用的测试:phpunit -c app/ 我的 phpunit 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="phpunit.xsd"
     bootstrap="bootstrap.php.cache"
     backupGlobals="false"
     verbose="true">

<testsuites>
    <testsuite name="Project Test Suite">
        <directory>../src/*Bundle/Tests</directory>

    </testsuite>
</testsuites>

<php>
    <const name="PHPUNIT_TESTSUITE" value="true"/>
</php>
</phpunit>

在作曲家文件中我有:

"autoload": {
    "psr-4": { "": "src/" },
    "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},

在AppKernele.php

    $bundles = array(
        // The Base Symfony Bundle
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        new Symfony\Bundle\SecurityBundle\SecurityBundle(),
        new Symfony\Bundle\TwigBundle\TwigBundle(),
        new Symfony\Bundle\MonologBundle\MonologBundle(),
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),

        // Sonata Admin Bundle
        new Sonata\CoreBundle\SonataCoreBundle(),
        new Sonata\BlockBundle\SonataBlockBundle(),
        new Knp\Bundle\MenuBundle\KnpMenuBundle(),
        new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
        new Sonata\AdminBundle\SonataAdminBundle(),

        // Our Bundle
        new RepositoryBundle\RepositoryBundle(),
        new AdminBundle\AdminBundle(),
    );

所以通常情况下,boostrap.php.cache 应该没问题,但为什么不行呢?

如果您需要更多信息,请问我。我真的需要你的帮助。

问题出在这里$user1 = new User(); 如果您的用户 class 位于 RepositoryBundle\Entity,那么有 2 个解决方案。

  1. use RepositoryBundle\Entity\User 而不是 use RepositoryBundle\Entity
  2. $user1 = new Entity\User(); 而不是 $user1 = new User();

选择其中一个解决方案,而不是两个。阅读 php 命名空间以进一步了解