2014-09-19 72 views
6

由於自動加載器無法解析Doctrine \ ORM \ Mapping \ Table,因此我對composer的自動加載感到煩惱。 因爲我已經創建學說實體類典型註解的單元測試:自動加載無法正確使用供應商目錄中的autoloader.php

<?php 

namespace OmniSearchTest\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Picture 
* 
* @ORM\Table(name="picture") 
* @ORM\Entity 
*/ 
class Picture 
{ 

,並通過使用該實體創建一個新的實體管理器。但即時得到消息:

Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "@Doctrine\ORM\Mapping\Table" in class OmniSearchTest\Entity\Picture does not exist, or could not be auto-loaded. 

對於一些單元測試

首先,我有以下項目結構:

/src 
    /OmniSearch 
     SomeClass.php 
/tests 
    /OmniSearchTest 
     SomeClassTest.php 
/composer.json 
/phpunit.xml.dist 

我composer.json看起來是這樣的:

{ 
    /* ... */ 

    "require": { 
     "php": ">=5.4", 
     "doctrine/orm": "2.*" 
    }, 
    "require-dev": { 
     "phpunit/phpunit": "4.*" 
    }, 
    "autoload": { 
     "psr-0": { 
      "OmniSearch\\": "src/" 
     } 
    }, 
    "autoload-dev": { 
     "psr-0": { 
      "OmniSearchTest\\": "tests/" 
     } 
    } 
} 

雖然我的phpunit看起來非常像這樣:

<?xml version="1.0" encoding="UTF-8"?> 
<phpunit backupGlobals="false" 
    backupStaticAttributes="false" 
    bootstrap="vendor/autoload.php" 
    strict="true" 
    verbose="true"> 
    <testsuites> 
     <testsuite name="omnisearch"> 
      <directory>./tests/OmniSearchTest</directory> 
     </testsuite> 
    </testsuites> 
</phpunit> 

我從自動加載工作正常的另一個zf2項目中截掉了這個項目。林不知道,因爲自動生成的autoload_namespaces.php包含的映射關係究竟是什麼出了問題:

'Doctrine\\ORM\\' => array($vendorDir . '/doctrine/orm/lib'), 

回答

13

這是一種在黑暗中,但Symfony的2個應用的鏡頭包括其中明確加載註釋註冊表的autoload.php文件。

// autoload.php 
use Doctrine\Common\Annotations\AnnotationRegistry; 
use Composer\Autoload\ClassLoader; 

/** 
* @var ClassLoader $loader 
*/ 
$loader = require __DIR__.'/../vendor/autoload.php'; 

AnnotationRegistry::registerLoader(array($loader, 'loadClass')); 

return $loader; 

我從來沒有真正研究過爲什麼在任何細節,因爲我不使用註釋。但試試看吧。不能傷害。

+0

whohaaaa,你說得對......我已經創建了一個測試目錄中的bootstrap.php和修改phpunit.xml來引導這個文件 – torsten 2014-09-19 19:11:35

+1

我只是用作曲家的autoload.php從'供應商'目錄,它的工作(我甚至不使用bootstrap.php):'$ loader = require_once __DIR __。'/ vendor/autoload.php'; AnnotationRegistry :: registerLoader(數組($ loader,'loadClass'));' – 2014-11-23 21:40:30

相關問題