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

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

问题描述

我刚刚通过Composer安装了PHPUnit 3.7.19版本的Sebastian Bergmann,并写了一个我想要单元测试的类。



我想拥有所有我的类自动加载到每个单元测试无需在顶部使用 include require



这是我的目录结构看起来像一个拖尾/斜杠表示一个目录,而不是一个文件:




  • composer.json

  • composer.lock

  • phar

  • lib /

    • returning.php


  • 测试/

    • returningTest.php


  • vendor /

    • bin /

      • phpunit


    • composer /

    • phpunit /

    • symfony /

    • autoload.php




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

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

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

 <?php 
类返回{
public $ var;
function __construct(){
$ this-> var = 1;
}
}
?>

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

 <?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

code>从命令行,我得到以下错误:


PHP致命错误:未找到类'返回'
/files/code/php/db/tests/returningTest.php on line 8


我注意到 composer vendor / autoload.php 中产生了 autoload.php 不确定这是否与我的问题相关。



此外,在Stack Overflow的一些其他答案中,人们提到了使用 PSR-0 命名空间命令在PHP中,但我没有成功使用任何一个。



请帮助!我只想在PHPUnit中自动加载我的类,所以我可以使用它们来创建对象,而不用担心 include require






更新日期:2013年8月14日



我现在创建了一个名为 PHPUnit Skeleton 的开源项目,以帮助您启动并运行PHPUnit测试很容易为你的项目。

解决方案

你需要告诉自动加载器在哪里可以找到一个类的php文件。这是通过遵循PSR-0标准完成的。



最好的方法是使用命名空间。当您请求 Acme\Tests \ReturningTest 时,自动加载器搜索 Acme / Tests / ReturningTest.php 类。有一些伟大的命名空间教程,只有搜索和阅读。请注意,命名空间不是自动加载到PHP中的内容,而是可以用于自动加载的内容。



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



将此添加到您的 composer.json

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

文档



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

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

但是,使用 PHPunit配置文件更好一些:

 <! -  /phpunit.xml.dist  - > 
<?xml version =1.0encoding =utf-8?>
< phpunit bootstrap =./ vendor / autoload.php>

< testsuites>
< testsuite name =项目的测试套件>
< directory> ./ tests< / directory>
< / testsuite>
< / testsuites>

< / phpunit>

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

  $ ./vendor/bin/phpunit 

如果将配置文件放入另一个目录,则需要在命令中使用 -c 选项。


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.

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
  • lib/
    • returning.php
  • tests/
    • returningTest.php
  • vendor/
    • bin/
      • phpunit
    • composer/
    • phpunit/
    • symfony/
    • autoload.php

My composer.json file includes the following:

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

My returning.php class file includes the following:

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

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()
    {

    }
}
?>

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

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

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

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.

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.


Update: 14th of August 2013

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.

解决方案

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.

The best way is to use namespaces. The autoloader searches for a Acme/Tests/ReturningTest.php file when you requested a Acme\Tests\ReturningTest 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 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.

Add this to your composer.json:

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

More information in the documentation.

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

However, it's nicer to use a PHPunit configuration file:

<!-- /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

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天全站免登陆