0

我在这里使用这个例子来做我的 DependencyInjection 测试单元,但是,当我测试时,我得到以下错误:

Myapp\MyBundle\Tests\DependencyInjection\MybundleExtensionTest::test Load
Argument 1 passed to Myapp\MyBundle\DependencyInjection\Configuration::buil
dPathNode() must be an instance of Myapp\MyBundle\DependencyInjection\NodeD
efinition, instance of Symfony\Component\Config\Definition\Builder\ArrayNodeDefi
nition given, called in E:\wamp\www\testingUnit\src\Myapp\MyBundle\Dependen
cyInjection\Configuration.php on line 33 and defined

DependencyInjection/MyappMybundleExtensions.php

<?php
namespace Myapp\Mybundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Config\Definition\Processor;


class MybundleExtension extends Extension
{
/**
 * Build the extension services
 *
 * @param array $configs
 * @param ContainerBuilder $container
 */

public function load(array $configs, ContainerBuilder $container)
{
    $processor = new Processor();
    $configuration = new Configuration();

    $config = $this->processConfiguration($configuration, $configs);

    $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
    $loader->load('file.yml');

    $config = $processor->processConfiguration($configuration, $configs);


    $container->setParameter('FashionWeb\ProductBundle\Controller\FamilyController::showFamilyAction', $config['action']);
}

}

测试/依赖注入/MyappMyBundleExtensionsTest.php

 namespace Myapp\Mybundle\Tests\DependencyInjection;

  use Symfony\Component\DependencyInjection\ContainerBuilder;

  class Myapp\MybundleExtensionTest extends \PHPUnit_Framework_TestCase
 {
public function testLoad()
{

    $container = new ContainerBuilder();

    $extension = new MyappMybundleExtension();

    $extension->load(array(), $container);


    $this->assertEquals('FashionWeb\ProductBundle\Controller\FamilyController::showFamilyAction', $container->getParameter('myapp_mybundle.action'), sprintf('%s parameter is correct', 'myapp_mybundle.action'));

}

}
4

1 回答 1

1

use Symfony\Component\Config\Definition\Builder\NodeDefinition;的 Configuration.php 文件中有吗?

错误似乎是由于缺少 use 语句引起的。

于 2012-09-19T16:35:15.033 回答