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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog