duanmeng9336 2016-10-23 01:24
浏览 62
已采纳

未找到Symfony 2和PhpUnit类

I'm lost about how to find a solution for my problem, I already search a lot on internet (and stackoverflow) but nothing is ok for me...

The problem

I have a Symfony 2 app. I created a test with PHPUnit but when I execute the test, I have these exceptions :

1) RepositoryBundle\Tests\DAO\UserDAOTest::testCreate Error: Class 'RepositoryBundle\Tests\DAO\User' not found

And I don't know why...

<?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());
    }
}

The use line are the same in the other class and are OK.

To execute the test I use : phpunit -c app/ My config file for 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>

In composer file I have :

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

In 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(),
    );

So with all of this normally the boostrap.php.cache should be ok and thus why is it not ?

If you need anymore information, ask me. I really need your help.

  • 写回答

1条回答 默认 最新

  • douxingsuo8809 2016-10-23 04:08
    关注

    The problem is in here $user1 = new User(); If your User class located in RepositoryBundle\Entity, then there are 2 solutions.

    1. use RepositoryBundle\Entity\User instead of use RepositoryBundle\Entity
    2. $user1 = new Entity\User(); instead of $user1 = new User();

    Pick one of those solutions, not both. Read about php namespace for further understanding

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常
  • ¥15 SCENIC分析报错求解答
  • ¥15 ceph初始化mon不成功 下图不报错 这个是什么元
  • ¥15 数学建模数学建模少ai
  • ¥35 这个等效电路图是什么意思?(答疑)(有没有不是chat的,我就是在chat搜不到才来问的,chat还开了VIP)(可以追加酬金)