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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。