2016-01-18 23 views
1

我想建立一個新的PHP項目。由於我是phpunit和composer的新手,所以我對這個項目很感興趣。設置與作曲家和phpunit的PHP項目

我的文件夾結構如下所示:

- 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. 
+0

你在項目中添加作曲家'autoload.php'?如果不是,則需要__DIR__。 '/vendor/autoload.php';' –

+0

我在'phpunit.xml'中添加了'bootstrap =「./ tests/start.php」'。它包含'require_once __DIR__。 '/../供應商/ autoload.php';'。這應該夠了? – Pascal

+0

作曲家的'autoload.php'通常載入您項目的根目錄(而不是'phpunit.xml')。看看Laravel,Drupal和其他流行的PHP項目。 –

回答

0

您需要PHPUnit_Framework_TestCase前加一個反斜槓加載從根命名空間中的類:

class FooTest extends \PHPUnit_Framework_TestCase 
+0

如果我這樣做,我得到'未捕獲PHPUnit_Framework_Exception:類「PHPUnit_Extensions_RepeatedTest」不擴展PHPUnit_Framework_TestCase.' – Pascal

+0

這是另一個問題。將自動加載添加到phpunit.xml中:'

+0

我向phpunit.xml添加了 。 'bootstrap.php'文件包含:'require_once __DIR__。 '/../供應商/ autoload.php';' – Pascal