简体   繁体   中英

Setting up php project with composer and phpunit

I am trying to set up a new php project. Since I am new to phpunit and composer I am struggeling with the project setup.

My folder structure looks like this:

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

My composer file:

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

My 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());
    }
}

But now I am not sure what I am missing to get ./vendor/bin/phpunit . running. Currently it gives me an error:

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

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

class FooTest extends \PHPUnit_Framework_TestCase

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM