简体   繁体   中英

Autoloading class via PSR-0

I have the following directory structure

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>

now when i am running $ vendor/bin/phpunit it says

PHP Fatal error: Class 'PHPUnitTuts\\FetchTask' not found in /home/raheel/code/oop/tests/FetchTaskTest.php on line 9

Please advice what i am doing wrong. Thanks

I guess the declaration for tests is missing, you could place it in the autoload-dev block:

{
    "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/" 
        }
    },
}

And your tests should live in the Tests namespace then.

use Tests\PHPUnitTuts\FetchTask

For PSR-0 the folder structure must be changed, move the classes in a folder PHPUnitTuts:

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

Btw, PSR-0 is deprecated, why not use PSR-4?

You're mixing concepts of PSR-0 and PSR-4.

To use PSR-0

Move src/FetchTask.php to src/PHPUnitTuts/FetchTask.php .

To use PSR-4

Change

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

To

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

Suggested Structure

.
├── 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');
    }
}

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