将zf-hal水化器与抽象工厂结合使用时遇到问题。这是我的模块配置:
public function getConfig()
{
return [
'hydrators' => [
'abstract_factories' => [
AbstractHydratorFactory::class,
]
],
'service_manager' => [
'factories' => [
ModuleOptions::class => ModuleOptionsFactory::class,
],
],
'zf-hal' => [
'renderer' => [
'default_hydrator' => 'reflection'
],
]
];
}
我的抽象工厂看起来像这样:
class AbstractHydratorFactory implements AbstractFactoryInterface
{
public function canCreate(ContainerInterface $container, $requestedName)
{
$moduleOptions = $container->get(ModuleOptions::class);
$configuration = $moduleOptions->getClass();
return isset($configuration[$requestedName]['property_name_translation']);
}
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$moduleOptions = $container->get(ModuleOptions::class);
$configuration = $moduleOptions->getClass();
$hydrator = $container->get($moduleOptions->getHydrator());
$hydrator->setNamingStrategy(
new ArrayMapNamingStrategy(
$configuration[$requestedName]['property_name_translation']
)
);
return $hydrator;
}
}
为了测试我的模块,我创建了一些单元测试。其中之一是:
class HalEntityHydratorTest extends TestCase
{
protected $moduleLoader;
protected function setUp()
{
$this->moduleLoader = new ModuleLoader([
'modules' => [
'Zend\Hydrator',
'Zend\Router',
'ZF\Hal',
'MyHalHydratorModule',
'MyHalHydratorModuleTest\Integration\Hydrator\HalEntityHydratorTest',
],
'module_listener_options' => [],
]);
$this->moduleLoader->getApplication()->bootstrap();
}
public function testHalRendererWithHalEntities()
{
$halPlugin = $this->moduleLoader->getServiceManager()->get('ViewHelperManager')->get('Hal');
$rootTestEntity = new RootTestEntity();
$childTestEntity = new ChildTestEntity();
$rootTestEntity->setChildEntity(new Entity($childTestEntity));
$rootTestEntity->setUnkownChildEntity(new Entity(new UnkownChildTestEntity()));
$expectedArray = [
'_embedded' => [
'phpunit:test-entity' => [
'_links' => [],
],
'unkownChildEntity' => [
'unkownChildTestProperty' => 'phpunit',
'_links' => [],
],
],
'_links' => [],
];
$this->assertSame($expectedArray, $halPlugin->renderEntity(new Entity($rootTestEntity)));
}
}
这些是我的测试实体:
class ChildTestEntity
{
}
class UnkownChildTestEntity
{
protected $unkownChildTestProperty = 'phpunit';
}
class RootTestEntity
{
protected $childEntity;
protected $unkownChildEntity;
public function setUnkownChildEntity($unkownChildEntity)
{
$this->unkownChildEntity = $unkownChildEntity;
}
public function setChildEntity($childEntity)
{
$this->childEntity = $childEntity;
}
}
然后最好知道我的测试模块配置是什么样的:
public function getConfig()
{
return [
'zf-hal' => [
'metadata_map' => [
ChildTestEntity::class => [
'force_self_link' => false,
],
UnkownChildTestEntity::class => [
'force_self_link' => false,
],
],
],
'my-hal-hydrator-module' => [
'class' => [
RootTestEntity::class => [
'property_name_translation' => [
'childEntity' => 'phpunit:test-entity',
],
],
],
],
];
}
好的,足够的源代码。现在会发生什么?
我运行测试套件,但由于阵列不同,上述测试失败。这就是为什么结果数组的第一个键仍然是'childEntity'而不是'phpunit:test-entity'的原因。
因此,我认为还没有进行财产置换,但我不知道为什么。