You are here

class MatcherTest in CRM Core 8.2

Tests the matcher service.

@covers \Drupal\crm_core_match\Matcher

@group crm_core

Hierarchy

Expanded class hierarchy of MatcherTest

File

modules/crm_core_match/tests/src/Unit/MatcherTest.php, line 20
Contains \Drupal\Tests\crm_core_match\Unit\MatcherTest.

Namespace

Drupal\Tests\crm_core_match\Unit
View source
class MatcherTest extends UnitTestCase {

  /**
   * The tested matcher.
   *
   * @var \Drupal\crm_core_match\Entity\Matcher
   */
  protected $matcher;

  /**
   * A set mocked match engines keyed by id.
   *
   * @var \Drupal\crm_core_match\Matcher\MatcherConfigInterface|\PHPUnit_Framework_MockObject_MockObject[]
   */
  protected $engine = array();

  /**
   * An individual entity used to get matches.
   *
   * @var \Drupal\crm_core_contact\Entity\Individual
   */
  protected $individual;

  /**
   * A mocked instance of the engine plugin manager.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $pluginManager;

  /**
   * A mocked instance of the config.
   *
   * @var \Drupal\Core\Config\Config|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $config;

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Matcher',
      'description' => 'Tests the matcher.',
      'group' => 'CRM Core',
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->engine['a'] = $this
      ->getMock('Drupal\\crm_core_match\\Plugin\\crm_core_match\\engine\\MatchEngineInterface');
    $this->engine['b'] = $this
      ->getMock('Drupal\\crm_core_match\\Plugin\\crm_core_match\\engine\\MatchEngineInterface');
    $this->engine['c'] = $this
      ->getMock('Drupal\\crm_core_match\\Plugin\\crm_core_match\\engine\\MatchEngineInterface');
    $this->pluginManager = $this
      ->getMock('Drupal\\Component\\Plugin\\PluginManagerInterface');

    //    $this->config = $this->getMockBuilder('\Drupal\Core\Config\Config')
    //      ->disableOriginalConstructor()
    //      ->getMock();
    //    $this->matcher = $this->getMock('Drupal\crm_core_match\Matcher');
    $this->individual = $this
      ->getMockBuilder('Drupal\\crm_core_contact\\Entity\\Individual')
      ->disableOriginalConstructor()
      ->getMock();
  }

  /**
   * Tests the sorting of engines.
   */
  public function testEngineSort() {

    //    $engine_config = $this->getMockBuilder('\Drupal\Core\Config\Config')
    //      ->disableOriginalConstructor()
    //      ->getMock();
    //
    //    $this->config->expects($this->once())
    //      ->method('get')
    //      ->with('engines')
    //      ->will($this->returnValue($engine_config));
    //
    //    $engine_config->expects($this->exactly(3))
    //      ->method('get')
    //      ->will($this->returnValue(TRUE));
    //
    //    $definitions = array(
    //      'a' => array('priority' => 5),
    //      'b' => array('priority' => 11),
    //      'c' => array('priority' => -1),
    //    );
    //    $this->pluginManager->expects($this->once())
    //      ->method('getDefinitions')
    //      ->will($this->returnValue($definitions));
    //
    //    $this->pluginManager->expects($this->at(1))
    //      ->method('createInstance')
    //      ->with('a', $definitions['a'])
    //      ->will($this->returnValue($this->engine['a']));
    //
    //    $this->pluginManager->expects($this->at(2))
    //      ->method('createInstance')
    //      ->with('b', $definitions['b'])
    //      ->will($this->returnValue($this->engine['b']));
    //
    //    $this->pluginManager->expects($this->at(3))
    //      ->method('createInstance')
    //      ->with('c', $definitions['c'])
    //      ->will($this->returnValue($this->engine['c']));
    //
    //    $engines = $this->matcher->getEngines();
    //
    //    $this->assertTrue(is_array($engines));
    //    $this->assertTrue(count($engines) == 3);
    //    $this->assertEquals($this->engine['b'], array_shift($engines));
    //    $this->assertEquals($this->engine['a'], array_shift($engines));
    //    $this->assertEquals($this->engine['c'], array_shift($engines));
  }

  /**
   * Tests the execution of match engines.
   */
  public function testEngineExecution() {

    //    $engine_config = $this->getMockBuilder('\Drupal\Core\Config\Config')
    //      ->disableOriginalConstructor()
    //      ->getMock();
    //
    //    $this->config->expects($this->once())
    //      ->method('get')
    //      ->with('engines')
    //      ->will($this->returnValue($engine_config));
    //
    //    $engine_config->expects($this->exactly(2))
    //      ->method('get')
    //      ->will($this->returnValue(TRUE));
    //    $definitions = array(
    //      'a' => array('priority' => 5),
    //      'b' => array('priority' => 11),
    //    );
    //    $this->pluginManager->expects($this->once())
    //      ->method('getDefinitions')
    //      ->will($this->returnValue($definitions));
    //
    //    $this->pluginManager->expects($this->at(1))
    //      ->method('createInstance')
    //      ->with('a', $definitions['a'])
    //      ->will($this->returnValue($this->engine['a']));
    //
    //    $this->pluginManager->expects($this->at(2))
    //      ->method('createInstance')
    //      ->with('b', $definitions['b'])
    //      ->will($this->returnValue($this->engine['b']));
    //    $this->engine['a']->expects($this->once())
    //      ->method('match')
    //      ->with($this->contact)
    //      ->will($this->returnValue(array(1, 2, 3, 5, 8, 13)));
    //
    //    $this->engine['b']->expects($this->once())
    //      ->method('match')
    //      ->with($this->contact)
    //      ->will($this->returnValue(array(3, 8, 21, 34)));
    //    $this->matcher->expects($this->once())
    //      ->method('match')
    //      ->with($this->contact)
    //      ->will($this->)
    //
    //    $ids = $this->matcher->match($this->contact);
    //    $ids = array_values($ids);
    //    sort($ids);
    //    $this->assertEquals(array(1, 2, 3, 5, 8, 13, 21, 34), $ids);
  }

  /**
   * Tests disabled engines are not executed.
   */
  public function testDisabledEngines() {

    //    $engine_config = $this->getMockBuilder('\Drupal\Core\Config\Config')
    //      ->disableOriginalConstructor()
    //      ->getMock();
    //
    //    $this->config->expects($this->once())
    //      ->method('get')
    //      ->with('engines')
    //      ->will($this->returnValue($engine_config));
    //
    //    $engine_config->expects($this->exactly(2))
    //      ->method('get')
    //      ->will($this->returnValueMap(array(
    //        array('a.status', TRUE),
    //        array('b.status', FALSE),
    //      )));
    //
    //    $definitions = array(
    //      'a' => array('priority' => 5),
    //      'b' => array('priority' => 11),
    //    );
    //    $this->pluginManager->expects($this->once())
    //      ->method('getDefinitions')
    //      ->will($this->returnValue($definitions));
    //
    //    $this->pluginManager->expects($this->once())
    //      ->method('createInstance')
    //      ->with('a', $definitions['a'])
    //      ->will($this->returnValue($this->engine['a']));
    //
    //    $this->engine['a']->expects($this->once())
    //      ->method('match')
    //      ->with($this->contact)
    //      ->will($this->returnValue(array()));
    //
    //    $this->engine['b']->expects($this->never())
    //      ->method('match');
    //
    //    $this->assertEquals(1, count($this->matcher->getEngines()));
    //
    //    $this->matcher->match($this->contact);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MatcherTest::$config protected property A mocked instance of the config.
MatcherTest::$engine protected property A set mocked match engines keyed by id.
MatcherTest::$individual protected property An individual entity used to get matches.
MatcherTest::$matcher protected property The tested matcher.
MatcherTest::$pluginManager protected property A mocked instance of the engine plugin manager.
MatcherTest::getInfo public static function
MatcherTest::setUp protected function Overrides UnitTestCase::setUp
MatcherTest::testDisabledEngines public function Tests disabled engines are not executed.
MatcherTest::testEngineExecution public function Tests the execution of match engines.
MatcherTest::testEngineSort public function Tests the sorting of engines.
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.