[英]Symfony 2 and PhpUnit Class not found
我迷失了如何為我的問題找到解決方案,我已經在互聯網上(和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,因此為什么不可以呢?
如果您需要更多信息,請問我。 我真的需要你的幫助。
問題出在這里$user1 = new User();
如果您的User類位於RepositoryBundle\\Entity
,則有2個解決方案。
use RepositoryBundle\\Entity\\User
代替use RepositoryBundle\\Entity
$user1 = new Entity\\User();
而不是$user1 = new User();
選擇其中一種解決方案,而不是全部。 閱讀有關php名稱空間的詳細信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.