未找到Symfony 2和PhpUnit类

时间:2016-10-23 01:24:57

标签: php symfony phpunit classnotfound

我迷失了如何为我的问题找到解决方案,我已经在互联网上搜索了很多(和stackoverflow),但对我来说没有什么可以...

问题

我有一个Symfony 2应用程序。我用PHPUnit创建了一个测试但是当我执行测试时,我有以下例外:

1)RepositoryBundle \ Tests \ DAO \ UserDAOTest :: testCreate 错误:未找到类'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());
    }
}

使用行在其他类中是相同的并且没问题。

要执行测试我使用: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应该没问题,为什么不呢?

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

1 个答案:

答案 0 :(得分:2)

问题在于$user1 = new User(); 如果您的用户类位于RepositoryBundle\Entity,则有2个解决方案。

  1. use RepositoryBundle\Entity\User代替use RepositoryBundle\Entity
  2. $user1 = new Entity\User();代替$user1 = new User();
  3. 选择其中一种解决方案,而不是两者。阅读有关php名称空间的信息,以便进一步理解