使用composer和phpunit设置php项目

时间:2016-01-18 12:29:02

标签: php phpunit composer-php

我正在尝试建立一个新的php项目。因为我是phpunit和作曲家的新手,所以我对项目设置很感兴趣。

我的文件夹结构如下所示:

- TestProject
-- src
--- Controller
----- Foo.php
--- index.php
-- tests
--- Controller
----- FooTest.php
-- vendor
-- composer.json
-- phpunit.xml

我的作曲家档案:

"require": {
   "php": ">=5.4.0"
},
"require-dev": {
   "phpunit/phpunit": "4.7.*"
},
"autoload": {
   "psr-4": {
       "TestProject\\": "src/"
   }
},
"autoload-dev": {
   "psr-4": {
       "TestProject\\Tests\\": "tests/"
   }
}

我的phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="vendor/autoload.php">
    <testsuites>
        <testsuite name="TestProject Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>

Foo.php

namespace TestProject\Controller;

class Foo
{
    private $amount;

    public function __construct($amount)
    {
        $this->amount = $amount;
    }

    public function getAmount()
    {
        return $this->amount;
    }
}

FooTest.php

namespace TestProject\Tests\Controller;

class FooTest extends \PHPUnit_Framework_TestCase
{
    public function testCanBeNegated()
    {
        // Arrange
        $a = new Foo(1);

        // Act
        $b = $a->getAmount();

        // Assert
        $this->assertEquals(1, $b->getAmount());
    }
}

但是现在我不确定让./vendor/bin/phpunit .运行我缺少什么。目前它给我一个错误:

PHP Fatal error:  Uncaught PHPUnit_Framework_Exception: Class "PHPUnit_Extensions_RepeatedTest" does not extend PHPUnit_Framework_TestCase.

1 个答案:

答案 0 :(得分:0)

您需要在PHPUnit_Framework_TestCase之前添加反斜杠以从根命名空间加载类:

class FooTest extends \PHPUnit_Framework_TestCase