View source
<?php
namespace Drupal\Tests\crm_core_match\Unit;
use Drupal\crm_core_match\Plugin\crm_core_match\engine\DefaultMatchingEngine;
use Drupal\Tests\UnitTestCase;
class DefaultEngineTest extends UnitTestCase {
protected $pluginManager;
protected $entityTypeManager;
protected $entityFieldManager;
protected $individual;
protected $matcher;
protected $engine;
protected $field;
protected $matchHandler;
protected function setUp() {
$this->pluginManager = $this
->getMock('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$this->entityTypeManager = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface')
->disableOriginalConstructor()
->getMock();
$this->entityFieldManager = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityFieldManagerInterface')
->disableOriginalConstructor()
->getMock();
$this->individual = $this
->getMockBuilder('Drupal\\crm_core_contact\\Entity\\Individual')
->disableOriginalConstructor()
->getMock();
$this->individual
->expects($this
->any())
->method('bundle')
->will($this
->returnValue('dogs'));
$this->matcher = $this
->getMockBuilder('Drupal\\crm_core_match\\Entity\\Matcher')
->disableOriginalConstructor()
->getMock();
$this->matcher
->expects($this
->any())
->method('status')
->will($this
->returnValue(TRUE));
$storage = $this
->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$storage
->expects($this
->any())
->method('load')
->with('dogs')
->will($this
->returnValue($this->matcher));
$this->entityTypeManager
->expects($this
->any())
->method('getStorage')
->will($this
->returnValue($storage));
$this->field = $this
->getMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
$this->matchHandler = $this
->getMock('Drupal\\crm_core_match\\Plugin\\crm_core_match\\field\\FieldHandlerInterface');
$this->matchHandler
->expects($this
->any())
->method('getPropertyNames')
->will($this
->returnValue(array(
'value',
)));
$this->engine = new DefaultMatchingEngine([
'rules' => [
'foo' => [],
'bar' => [],
],
'threshold' => 50,
], 'default', array(), $this->pluginManager, $this->entityTypeManager, $this->entityFieldManager);
}
public function testMatch() {
$this->field
->expects($this
->any())
->method('getType')
->will($this
->returnValue('crap'));
$this->individual
->expects($this
->any())
->method('getFieldDefinitions')
->will($this
->returnValue(array(
'foo' => $this->field,
)));
$this->pluginManager
->expects($this
->any())
->method('hasDefinition')
->will($this
->returnValue(TRUE));
$this->matchHandler
->expects($this
->any())
->method('match')
->will($this
->returnValue(array(
'42' => array(
'value' => 100,
),
)));
$this->pluginManager
->expects($this
->once())
->method('createInstance')
->will($this
->returnValue($this->matchHandler));
$ids = $this->engine
->match($this->individual);
$this
->assertEquals([
42,
], $ids);
}
public function testMultipleMatch() {
$this->field
->expects($this
->any())
->method('getType')
->will($this
->returnValue('crap'));
$this->individual
->expects($this
->any())
->method('getFieldDefinitions')
->will($this
->returnValue(array(
'foo' => $this->field,
'bar' => $this->field,
)));
$this->pluginManager
->expects($this
->any())
->method('hasDefinition')
->will($this
->returnValue(TRUE));
$this->matchHandler
->expects($this
->any())
->method('match')
->will($this
->returnValue(array(
'42' => array(
'value' => 40,
),
)))
->will($this
->returnValue(array(
'42' => array(
'value' => 40,
),
'30' => array(
'value' => 40,
),
)));
$this->pluginManager
->expects($this
->any())
->method('createInstance')
->will($this
->returnValue($this->matchHandler));
$ids = $this->engine
->match($this->individual);
$this
->assertEquals([
42,
], $ids);
}
}