通过PSR-0自动加载课程

时间:2016-08-19 14:32:49

标签: php composer-php

我有以下目录结构

oop
 - src
   - FetchTask.php
 - tests
   - FetchTaskTest.php
 - vendor
 - composer.json
 - composer.lock
 - phpunit.xml

// FetchTask.php

<?php 

namespace PHPUnitTuts;

class FetchTask 
{

}

// FetchTaskTest.php

<?php

use PHPUnitTuts\FetchTask;

class Fetch_Test extends AbstractTest
{
    public function setUp() 
    {
        $this->fetch = new FetchTask;
    }

    public function testStoresListOfAssets($value='')
    {
        $this->classHasStaticAttribute('paths', 'FetchTask');
    }
}

// composer.json

{
    "name": "raheel/code",
    "require-dev": {
        "phpunit/phpunit": "^5.5",
        "phpunit/php-code-coverage": "^4",
        "squizlabs/php_codesniffer": "2.*"
    },
    "autoload": {
        "psr-0": { 
            "PHPUnitTuts\\": "src/" 
        }
    },
}

// phpunit.xml

<phpunit bootstrap="./vendor/autoload.php">
    <testsuites>
        <testsuite name="oop">
             <directory>./tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

现在,当我正在运行$ vendor/bin/phpunit时,它说

  

PHP致命错误:找不到类'PHPUnitTuts \ FetchTask'   第9行/home/raheel/code/oop/tests/FetchTaskTest.php

请告知我做错了什么。感谢

2 个答案:

答案 0 :(得分:2)

我想缺少测试声明,你可以把它放在autoload-dev块中:

{
    "name": "raheel/code",
    "require-dev": {
        "phpunit/phpunit": "^5.5",
        "phpunit/php-code-coverage": "^4",
        "squizlabs/php_codesniffer": "2.*"
    },
    "autoload": {
        "psr-0": { 
            "PHPUnitTuts\\": "src/" 
        }
    },
    "autoload-dev": {
        "psr-0": { 
            "Tests\\PHPUnitTuts\\": "tests/" 
        }
    },
}

然后你的测试应该存在于Tests命名空间中。

use Tests\PHPUnitTuts\FetchTask

对于PSR-0,必须更改文件夹结构,将类移动到PHPUnitTuts文件夹中:

oop
 - src
   - PHPUnitTuts/FetchTask.php
 - tests
   - PHPUnitTuts/FetchTaskTest.php
 - vendor
 - composer.json
 - composer.lock
 - phpunit.xml

顺便说一句,PSR-0已被弃用,为什么不使用PSR-4?

答案 1 :(得分:0)

您正在混合PSR-0和PSR-4的概念。

使用PSR-0

src/FetchTask.php移至src/PHPUnitTuts/FetchTask.php

使用PSR-4

更改

"psr-0": { 
    "PHPUnitTuts\\": "src/" 
}

"psr-4": { 
    "PHPUnitTuts\\": "src" 
}

建议结构

.
├── composer.json
├── phpunit.xml.dist
├── src
│   └── FetchTask.php
└── test
    └── FetchTaskTest.php

composer.json

{
    "name": "raheel/code",
    "require-dev": {
        "phpunit/phpunit": "^5.5",
        "phpunit/php-code-coverage": "^4",
        "squizlabs/php_codesniffer": "2.*"
    },
    "autoload": {
        "psr-4": {
            "PHPUnitTuts\\": "src"
        }
    }
}

phpunit.xml.dist

<phpunit bootstrap="vendor/autoload.php">
    <testsuites>
        <testsuite name="oop">
             <directory>test</directory>
        </testsuite>
    </testsuites>
</phpunit>

src/FetchTask.php

<?php

namespace PHPUnitTuts;

class FetchTask extends \PHPUnit_Framework_TestCase
{
    public static $paths = [];
}

test/FetchTaskTest.php

<?php

namespace PHPUnitTuts;

class FetchTaskTest extends \PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        $this->fetch = new FetchTask();
    }

    public function testStoresListOfAssets()
    {
        $this->assertClassHasStaticAttribute('paths', FetchTask::class);
        // or
        $this->assertClassHasStaticAttribute('paths', get_class($this->fetch));
        // or
        $this->assertClassHasStaticAttribute('paths', 'PHPUnitTuts\\FetchTask');
    }
}