class DefaultEngineTest in CRM Core 8
Same name and namespace in other branches
- 8.3 modules/crm_core_match/tests/src/Unit/DefaultEngineTest.php \Drupal\Tests\crm_core_match\Unit\DefaultEngineTest
- 8.2 modules/crm_core_match/tests/src/Unit/DefaultEngineTest.php \Drupal\Tests\crm_core_match\Unit\DefaultEngineTest
Tests the default matching engine.
@coversDefaultClass \Drupal\crm_core_match\Plugin\crm_core_match\engine\DefaultMatchingEngine
@group crm_core
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\crm_core_match\Unit\DefaultEngineTest
Expanded class hierarchy of DefaultEngineTest
File
- modules/
crm_core_match/ tests/ src/ Unit/ DefaultEngineTest.php, line 15
Namespace
Drupal\Tests\crm_core_match\UnitView source
class DefaultEngineTest extends UnitTestCase {
/**
* The mocked match field plugin manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $pluginManager;
/**
* The mocked entity manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityTypeManager;
/**
* The entity field manager service.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* A mocked individual entity used to get matches.
*
* @var \Drupal\crm_core_contact\Entity\Individual|\PHPUnit_Framework_MockObject_MockObject
*/
protected $individual;
/**
* A mocked matcher.
*
* @var \Drupal\crm_core_match\Entity\Matcher|\PHPUnit_Framework_MockObject_MockObject
*/
protected $matcher;
/**
* The tested engine.
*
* @var \Drupal\crm_core_match\Plugin\crm_core_match\engine\DefaultMatchingEngine
*/
protected $engine;
/**
* A mocked field definition.
*
* @var \Drupal\Core\Field\FieldDefinitionInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $field;
/**
* A mocked match field handler.
*
* @var \Drupal\crm_core_match\Plugin\crm_core_match\field\FieldHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $matchHandler;
/**
* {@inheritdoc}
*/
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([
'value',
]));
$this->engine = new DefaultMatchingEngine([
'rules' => [
'foo' => [],
'bar' => [],
],
'threshold' => 50,
], 'default', [], $this->pluginManager, $this->entityTypeManager, $this->entityFieldManager);
}
/**
* Tests the match method.
*/
public function testMatch() {
$this->field
->expects($this
->any())
->method('getType')
->will($this
->returnValue('crap'));
$this->individual
->expects($this
->any())
->method('getFieldDefinitions')
->will($this
->returnValue([
'foo' => $this->field,
]));
$this->pluginManager
->expects($this
->any())
->method('hasDefinition')
->will($this
->returnValue(TRUE));
$this->matchHandler
->expects($this
->any())
->method('match')
->will($this
->returnValue([
'42' => [
'value' => 100,
],
]));
$this->pluginManager
->expects($this
->once())
->method('createInstance')
->will($this
->returnValue($this->matchHandler));
$ids = $this->engine
->match($this->individual);
$this
->assertEquals([
42,
], $ids);
}
/**
* Tests the match method with multiple fields.
*/
public function testMultipleMatch() {
$this->field
->expects($this
->any())
->method('getType')
->will($this
->returnValue('crap'));
$this->individual
->expects($this
->any())
->method('getFieldDefinitions')
->will($this
->returnValue([
'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([
'42' => [
'value' => 40,
],
]))
->will($this
->returnValue([
'42' => [
'value' => 40,
],
'30' => [
'value' => 40,
],
]));
$this->pluginManager
->expects($this
->any())
->method('createInstance')
->will($this
->returnValue($this->matchHandler));
$ids = $this->engine
->match($this->individual);
$this
->assertEquals([
42,
], $ids);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DefaultEngineTest:: |
protected | property | The tested engine. | |
DefaultEngineTest:: |
protected | property | The entity field manager service. | |
DefaultEngineTest:: |
protected | property | The mocked entity manager service. | |
DefaultEngineTest:: |
protected | property | A mocked field definition. | |
DefaultEngineTest:: |
protected | property | A mocked individual entity used to get matches. | |
DefaultEngineTest:: |
protected | property | A mocked matcher. | |
DefaultEngineTest:: |
protected | property | A mocked match field handler. | |
DefaultEngineTest:: |
protected | property | The mocked match field plugin manager. | |
DefaultEngineTest:: |
protected | function |
Overrides UnitTestCase:: |
|
DefaultEngineTest:: |
public | function | Tests the match method. | |
DefaultEngineTest:: |
public | function | Tests the match method with multiple fields. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |