You are here

class DefaultEngineTest in CRM Core 8

Same name and namespace in other branches
  1. 8.3 modules/crm_core_match/tests/src/Unit/DefaultEngineTest.php \Drupal\Tests\crm_core_match\Unit\DefaultEngineTest
  2. 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

Expanded class hierarchy of DefaultEngineTest

File

modules/crm_core_match/tests/src/Unit/DefaultEngineTest.php, line 15

Namespace

Drupal\Tests\crm_core_match\Unit
View 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

Namesort descending Modifiers Type Description Overrides
DefaultEngineTest::$engine protected property The tested engine.
DefaultEngineTest::$entityFieldManager protected property The entity field manager service.
DefaultEngineTest::$entityTypeManager protected property The mocked entity manager service.
DefaultEngineTest::$field protected property A mocked field definition.
DefaultEngineTest::$individual protected property A mocked individual entity used to get matches.
DefaultEngineTest::$matcher protected property A mocked matcher.
DefaultEngineTest::$matchHandler protected property A mocked match field handler.
DefaultEngineTest::$pluginManager protected property The mocked match field plugin manager.
DefaultEngineTest::setUp protected function Overrides UnitTestCase::setUp
DefaultEngineTest::testMatch public function Tests the match method.
DefaultEngineTest::testMultipleMatch public function Tests the match method with multiple fields.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.