通过PSR-0自动加载类



我有以下目录结构

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 PHPUnitTutsFetchTask;
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致命错误:类'PHPUnitTutsFetchTask'没有找到/home/raheel/code/oop/tests/fetchtasktest .php第9行

请告诉我哪里做错了。由于

我猜测试的声明是缺失的,你可以把它放在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 TestsPHPUnitTutsFetchTask

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

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

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

你把PSR-0和PSR-4的概念混在一起了。

使用PSR-0

移动src/FetchTask.phpsrc/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');
    }
}

相关内容

  • 没有找到相关文章

最新更新