使用 Composer 和 autoload.php 在 PHPUnit 中自动加载类 [英] Autoloading classes in PHPUnit using Composer and autoload.php

查看:47
本文介绍了使用 Composer 和 autoload.php 在 PHPUnit 中自动加载类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚通过 Composer 安装了 Sebastian Bergmann 的 PHPUnit 3.7.19 版,并编写了一个我想进行单元测试的类.

I have just installed PHPUnit version 3.7.19 by Sebastian Bergmann via Composer and have written a class I would like to unit test.

我希望将我的所有类自动加载到每个单元测试中不必在测试顶部使用 includerequire但事实证明这很困难!

I would like to have all my classes autoloaded into each unit test without having to use include or require at the top of my test but this is proving to be difficult!

这是我的目录结构的样子(尾部/斜杠表示目录,而不是文件):

This is what my directory structure looks like (a trailing / slash indicates a directory, not a file):

  • composer.json
  • composer.lock
  • composer.phar
  • 库/
    • 返回.php
    • 返回Test.php
    • 斌/
      • phpunit

      我的 composer.json 文件包括以下内容:

      My composer.json file includes the following:

      "require": {
          "phpunit/phpunit": "3.7.*",
          "phpunit/phpunit-selenium": ">=1.2"
      }
      

      我的returning.php 类文件包括以下内容:

      My returning.php class file includes the following:

      <?php
      class Returning {
          public $var;
          function __construct(){
              $this->var = 1;
          }
      }
      ?>
      

      我的returningTest.php 测试文件包括以下内容:

      My returningTest.php test file includes the following:

      <?php
      class ReturningTest extends PHPUnit_Framework_TestCase
      {
          protected $obj = null;
      
          protected function setUp()
          {
              $this->obj = new Returning;
          }
      
          public function testExample()
          {   
              $this->assertEquals(1, $this->obj->var);
          }
      
          protected function tearDown()
          {
      
          }
      }
      ?>
      

      但是,当我从命令行运行 ./vendor/bin/phpunit tests 时,出现以下错误:

      However, when I run ./vendor/bin/phpunit tests from the command-line, I get the following error:

      PHP 致命错误:未在其中找到Returning"类/files/code/php/db/tests/returningTest.php 第 8 行

      PHP Fatal error: Class 'Returning' not found in /files/code/php/db/tests/returningTest.php on line 8

      我注意到 composervendor/autoload.php 中生成了一个 autoload.php 文件,但不确定这是否与我的问题相关.

      I noticed that composer produced an autoload.php file in vendor/autoload.php but not sure if this is relevant for my problem.

      此外,在 Stack Overflow 的其他一些答案中,人们也提到了使用 PSR-0 在 Composer 中和 namespace 在 PHP 中,但我没有成功使用任何一个.

      Also, in some other answers on Stack Overflow people have mentioned something about using PSR-0 in composer and the namespace command in PHP, but I have not been successful in using either one.

      请帮忙!我只想在 PHPUnit 中自动加载我的类,这样我就可以使用它们来创建对象而不必担心 includerequire.

      Please help! I just want to autoload my classes in PHPUnit so I can just use them to create objects without worrying about include or require.

      更新:2013 年 8 月 14 日

      我现在创建了一个名为 PHPUnit Skeleton 的开源项目,以帮助您轻松启动和运行 PHPUnit 测试你的项目.

      I have now created an Open Source project called PHPUnit Skeleton to help you get up and running with PHPUnit testing easily for your project.

      推荐答案

      好吧,首先.您需要告诉自动加载器在哪里可以找到类的 php 文件.这是通过遵循 PSR-0 标准完成的.

      Well, at first. You need to tell the autoloader where to find the php file for a class. That's done by following the PSR-0 standard.

      最好的方法是使用命名空间.当您请求 AcmeTestsReturningTest 类时,自动加载器会搜索 Acme/Tests/ReturningTest.php 文件.那里有一些很棒的命名空间教程,只需搜索和阅读即可.请注意,命名空间不是进入 PHP 用于自动加载的东西,而是可用于自动加载的东西.

      The best way is to use namespaces. The autoloader searches for a Acme/Tests/ReturningTest.php file when you requested a AcmeTestsReturningTest class. There are some great namespace tutorials out there, just search and read. Please note that namespacing is not something that came into PHP for autoloading, it's something that can be used for autoloading.

      Composer 带有一个标准的 PSR-0 自动加载器(vendor/autoload.php 中的那个).在您的情况下,您想告诉自动加载器在 lib 目录中搜索文件.然后当你使用 ReturningTest 时,它会寻找 /lib/ReturningTest.php.

      Composer comes with a standard PSR-0 autoloader (the one in vendor/autoload.php). In your case you want to tell the autoloader to search for files in the lib directory. Then when you use ReturningTest it will look for /lib/ReturningTest.php.

      将此添加到您的 composer.json:

      {
          ...
          "autoload": {
              "psr-0": { "": "lib/" }
          }
      }
      

      文档中的更多信息.

      现在自动加载器可以找到您需要让 PHPunit 知道在运行测试之前要执行的文件的类:引导文件.您可以使用 --bootstrap 选项来指定引导程序文件所在的位置:

      Now the autoloader can find your classes you need to let PHPunit know there is a file to execute before running the tests: a bootstrap file. You can use the --bootstrap option to specify where the bootstrap file is located:

      $ ./vendor/bin/phpunit tests --bootstrap vendor/autoload.php
      

      但是,使用 PHPunit 配置文件:

      <!-- /phpunit.xml.dist -->
      <?xml version="1.0" encoding="utf-8" ?>
      <phpunit bootstrap="./vendor/autoload.php">
      
          <testsuites>
              <testsuite name="The project's test suite">
                  <directory>./tests</directory>
              </testsuite>
          </testsuites>
      
      </phpunit>
      

      现在,您可以运行该命令,它会自动检测配置文件:

      Now, you can run the command and it will automatically detect the configuration file:

      $ ./vendor/bin/phpunit
      

      如果你把配置文件放到另一个目录下,你需要在命令中用-c选项把那个目录的路径放到.

      If you put the configuration file into another directory, you need to put the path to that directory in the command with the -c option.

      这篇关于使用 Composer 和 autoload.php 在 PHPUnit 中自动加载类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆