dongwei1954 2018-07-22 15:46
浏览 13
已采纳

如何测试Symfony独立捆绑包

I have developped some bundles i.e. https://github.com/975L/EmailBundle and I want to add tests for them. So, I have created the file Tests/Service/EmailServiceTest.php that contains one test.

I have installed phpunit using sudo apt install phpunit on my Ubuntu computer, but when I cd to my bundle's folder and run phpunit I have the following error:

1) c975L\EmailBundle\Tests\Service\EmailServiceTest::testCreateEmailObject Error: Class 'c975L\EmailBundle\Service\EmailService' not found

It looks like the autoload can't find the class, even if declared at the top with use.

I saw some solutions over the web that requires to create a bootstrap.php with autoload but even with this, it doesn't work...

So my question is how do we test a standalone Symfony Bundle with phpunit?

  • 写回答

1条回答 默认 最新

  • donglugou6652 2018-07-22 16:07
    关注

    It is a good idea to install phpunit on a per project basis, as version differences could come up if you have multiple projects.

    (1) composer require phpunit/phpunit --dev

    I saw some solutions over the web that requires to create a bootstrap.php with autoload but even with this, it doesn't work...

    You can use composer's autoloader as a bootstrap. A custom bootstrap might come in handy in some cases, but not necessary.

    (2) Create a phpunit.xml.dist file, something like this (your current one does not specify a bootstrap):

    <?xml version="1.0" encoding="UTF-8"?>
    
    <phpunit bootstrap="./vendor/autoload.php">
    <testsuites>
        <testsuite name="MyBundle test suite">
            <directory suffix="Test.php">./Tests</directory>
        </testsuite>
    </testsuites>
    
    <filter>
        <whitelist>
            <directory>./</directory>
            <exclude>
                <directory>./Resources</directory>
                <directory>./Tests</directory>
                <directory>./vendor</directory>
            </exclude>
        </whitelist>
    </filter>
    </phpunit>
    

    Also check out vendor/bin/phpunit --generate-configuration which is nice.

    (3) Run vendor/bin/phpunit

    It should find the config file automatically, but you can also specify it with the --configuration option

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

报告相同问题?

悬赏问题

  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 pyqt5读取ui文件报错
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?