View source
<?php
namespace Drupal\Tests\feeds_para_mapper\Unit;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\feeds\FeedInterface;
use Drupal\feeds\Feeds\Processor\EntityProcessorBase;
use Drupal\feeds\FeedTypeInterface;
use Drupal\feeds\Plugin\Type\FeedsPluginManager;
use Drupal\feeds\Plugin\Type\Target\FieldTargetBase;
use Drupal\feeds_para_mapper\Feeds\Target\WrapperTarget;
use Drupal\feeds_para_mapper\Mapper;
use Drupal\feeds_para_mapper\Utility\TargetInfo;
use Drupal\Tests\feeds_para_mapper\Unit\Helpers\EntityHelper;
use Drupal\Tests\feeds_para_mapper\Unit\Helpers\FieldHelper;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use ReflectionClass;
use Drupal\Core\DependencyInjection\ContainerBuilder;
abstract class FpmTestBase extends UnitTestCase {
protected $fieldHelper;
protected $entityHelper;
protected $wrapperTarget;
protected $feedType;
protected $node;
protected $fields;
protected $services;
protected $instanceMock;
protected $target;
protected $messenger;
protected function setUp() {
$this->fieldHelper = new FieldHelper($this
->getTargetInfo());
$this->fields = $this->fieldHelper->fields;
$this->entityHelper = new EntityHelper($this->fieldHelper);
$this->node = $this->entityHelper->node;
$services = array(
'feeds_para_mapper.mapper' => $this
->getMapperObject(),
'entity_type.manager' => $this->entityHelper
->getEntityTypeManagerMock()
->reveal(),
'entity_field.manager' => $this->fieldHelper
->getEntityFieldManagerMock(),
'string_translation' => $this
->getStringTranslationStub(),
);
$args = func_get_args();
if (isset($args[0])) {
$services = array_merge($args[0], $services);
}
$this->services = $services;
$this
->addServices($this->services);
$this->messenger = $this
->getMessengerMock();
$this
->initWrapper();
parent::setUp();
}
abstract function getTargetInfo();
abstract function getInstanceMock();
protected function addServices(array $services) {
$container = new ContainerBuilder();
foreach ($services as $id => $service) {
$container
->set($id, $service);
}
\Drupal::setContainer($container);
}
private function initWrapper() {
$method = $this
->getMethod('Drupal\\feeds_para_mapper\\Feeds\\Target\\WrapperTarget', 'prepareTarget')
->getClosure();
$this->feedType = $this
->getFeedTypeMock();
$field = $this->fields[1]
->reveal();
$field
->set('target_info', $this
->getTargetInfo());
$configuration = [
'feed_type' => $this->feedType,
'target_definition' => $method($field),
];
$id = "wrapper_target";
$plugin_definition = array();
$target = $this
->getInstanceMock();
$this->instanceMock = $target;
$this->target = $target
->reveal();
$plugin_manager = $this
->getPluginManagerMock();
$mapper = $this
->getMapperObject();
$wrapperTarget = new WrapperTarget($configuration, $id, $plugin_definition, $this->messenger
->reveal(), $plugin_manager, $mapper);
$this->wrapperTarget = $wrapperTarget;
}
private function getMessengerMock() {
$messenger = $this
->prophesize(MessengerInterface::class);
$messenger
->addWarning(Argument::any());
$messenger
->addError(Argument::any());
return $messenger;
}
protected function getFeedMock() {
$feed = $this
->prophesize(FeedInterface::class);
$feed
->getType()
->willReturn($this
->getFeedTypeMock());
return $feed
->reveal();
}
protected function getFeedTypeMock() {
$feed_type = $this
->createMock(FeedTypeInterface::class);
$processor = $this
->getProcessorMock();
$feed_type->id = 'test_feed_type';
$feed_type->description = 'This is a test feed type';
$feed_type->label = 'Test feed type';
$feed_type
->expects($this
->any())
->method('label')
->will($this
->returnValue($feed_type->label));
$feed_type
->expects($this
->any())
->method('getProcessor')
->will($this
->returnValue($processor));
return $feed_type;
}
private function getProcessorMock() {
$processor = $this
->getMockBuilder(EntityProcessorBase::class)
->disableOriginalConstructor()
->setMethods(array(
'entityType',
'bundle',
))
->getMock();
$processor
->expects($this
->any())
->method('entityType')
->will($this
->returnValue("node"));
$processor
->expects($this
->any())
->method('bundle')
->will($this
->returnValue("product"));
return $processor;
}
private function getPluginManagerMock() {
$manager = $this
->prophesize(FeedsPluginManager::class);
try {
$manager
->createInstance(Argument::type('string'), Argument::type('array'))
->willReturn($this->target);
} catch (PluginException $e) {
}
$manager
->getDefinitions()
->willReturn($this
->generatePluginDefinitions());
return $manager
->reveal();
}
protected function generatePluginDefinitions() {
$definitions = array(
'text' => array(
'id' => 'text',
'field_types' => array(
'text',
'text_long',
'text_with_summary',
),
'arguments' => array(
'@current_user',
),
'class' => 'Drupal\\feeds\\Feeds\\Target\\Text',
'provider' => 'feeds',
'plugin_type' => 'target',
'form' => array(
'configuration' => 'Drupal\\feeds\\Feeds\\Target\\Text',
),
),
);
return $definitions;
}
protected function getMethod($class, $name) {
try {
$class = new ReflectionClass($class);
} catch (\ReflectionException $e) {
}
$method = $class
->getMethod($name);
$method
->setAccessible(TRUE);
return $method;
}
protected function updateProperty($class, &$object, $property, $value) {
try {
$ref = new \ReflectionClass($class);
$prop = $ref
->getProperty($property);
$prop
->setAccessible(true);
$prop
->setValue($object, $value);
} catch (\ReflectionException $e) {
}
}
protected function getProperty($object, $property) {
$ref = new \ReflectionObject($object);
$prop = $ref
->getProperty($property);
$prop
->setAccessible(true);
return $prop
->getValue($object);
}
protected function getMapperObject() {
$plugin_manager = $this
->getPluginManagerMock();
$field_manager = $this->fieldHelper
->getEntityFieldManagerMock();
$bundleInfo = $this->fieldHelper
->getEntityTypeBundleInfoMock();
return new Mapper($plugin_manager, $field_manager, $bundleInfo);
}
}