2017-01-27 1 views
1

내 프로젝트의 구조는이 :phpunit을 오토로더 클래스

--/ 
--src 
--tests 
--phpunit.xml 
--composer.json 

나는 시험에서 src 폴더에서 내 수업을 자동 로딩을 위해 작곡가를 사용하고 싶습니다. 내 composer.json가 :

{ 
"name": "codewars/pack", 
"description": "Codewars project", 
"type": "project", 
"require": { 
    "fxp/composer-asset-plugin": "^1.2.0", 
    "phpunit/phpunit": "5.5.*", 
    "phpunit/dbunit": "2.0.*" 
}, 
"autoload": { 
    "psr-4": {"Source\\": "src/" 
    } 
} 

} 

Autoloder 파일이 생성 :

<?php 

// autoload_psr4.php @generated by Composer 

$vendorDir = dirname(dirname(__FILE__)); 
$baseDir = dirname($vendorDir); 

return array(
'Source\\' => array($baseDir . '/src'), 
); 

내 phpunit.xml :

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

그리고 내 테스트 파일 예 :

class Task2Test extends PHPUnit_Framework_TestCase 
{ 
public function testTask2(){ 
    $list=[1,3,5,9,11]; 
    $this->assertEquals(7,\Source\findMissing($list)); 
    $list=[1,5,7]; 
    $this->assertEquals(3,\Source\findMissing($list)); 
} 

} 

그리고 내가 시험을 볼 때 정의되지 않은 기능 소스 \ findMissing()를 호출

이, 도와, 어떻게이 문제를 해결할 수 있습니다하십시오의 I는 다음과 같은 치명적인 오류 오류 얻을?

답변

0

PSR-4는 기능을위한 것이 아니라 클래스 자동로드를 위해 설계되었습니다. 왜냐하면 php cant는 함수로 파일을 찾습니다.

더 나은 방법은 클래스 및 정적 메서드 코드를 작성할 수 있습니까? PSR-4 자동 로딩을 사용할 수 있습니다. 또 다른 방법은 참조 자세한 내용은 autoload 섹션

{ 
    "name": "codewars/pack", 
    "description": "Codewars project", 
    "type": "project", 
    "require": { 
     "fxp/composer-asset-plugin": "^1.2.0", 
     "phpunit/phpunit": "5.5.*", 
     "phpunit/dbunit": "2.0.*" 
}, 
"autoload": { 
    "files": [ 
     "src/my_cool_function1.php", 
     "src/my_cool_function2.php" 
    ], 
    "psr-4": { 
     "Source\\": "src/" 
    } 
} 

에 "파일"섹션을 지정할 수 있습니다 this question