You are here

class EntityPagerAnalyzerTest in Entity Pager 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/EntityPagerAnalyzerTest.php \Drupal\Tests\entity_pager\Unit\EntityPagerAnalyzerTest

@coversDefaultClass \Drupal\entity_pager\EntityPagerAnalyzer @group entity_pager

Hierarchy

Expanded class hierarchy of EntityPagerAnalyzerTest

File

tests/src/Unit/EntityPagerAnalyzerTest.php, line 19

Namespace

Drupal\Tests\entity_pager\Unit
View source
class EntityPagerAnalyzerTest extends UnitTestCase {

  /**
   * Log messages to use in tests.
   *
   * @var string[]
   */
  protected $logs;

  /**
   * Entity pager stub.
   *
   * @var \Drupal\entity_pager\EntityPagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityPager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->entityPager = $this
      ->createMock(EntityPagerInterface::class);
    for ($i = 0; $i < random_int(3, 6); $i++) {
      $this->logs[] = $this
        ->randomMachineName();
    }
  }

  /**
   * @covers ::__construct
   * @covers ::analyze
   */
  public function testAnalyze() {
    $event_dispatcher = $this
      ->createMock(EventDispatcherInterface::class);
    $event_dispatcher
      ->expects($this
      ->once())
      ->method('dispatch')
      ->with(EntityPagerEvents::ENTITY_PAGER_ANALYZE, $this
      ->callback([
      $this,
      'mockSubscriberLog',
    ]));

    // @todo Refactor \Drupal\entity_pager\EntityPagerAnalyzer to use dependency
    //   injection for its logging.
    $logger = $this
      ->createMock(LoggerChannelInterface::class);
    $logger
      ->expects($this
      ->exactly(count($this->logs)))
      ->method('notice')
      ->withConsecutive(...array_map(function ($a) {
      return [
        $a,
      ];
    }, $this->logs));
    $logger_factory = $this
      ->createMock(LoggerChannelFactoryInterface::class);
    $logger_factory
      ->method('get')
      ->willReturn($logger);
    $container = new ContainerBuilder();
    $container
      ->set('logger.factory', $logger_factory);
    \Drupal::setContainer($container);
    $analyzer = new EntityPagerAnalyzer($event_dispatcher);
    $analyzer
      ->analyze($this->entityPager);
  }

  /**
   * Event subscription mock function for ::testAnalyze().
   *
   * @param mixed $event
   *   The analyze event.
   *
   * @return bool
   *   Returns TRUE if given an
   *   \Drupal\entity_pager\Event\EntityPagerAnalyzeEvent, FALSE otherwise.
   */
  public function mockSubscriberLog($event) {
    if ($event instanceof EntityPagerAnalyzeEvent) {
      $event
        ->log($this->logs);
      $this
        ->assertSame($this->entityPager, $event
        ->getEntityPager());
      return TRUE;
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityPagerAnalyzerTest::$entityPager protected property Entity pager stub.
EntityPagerAnalyzerTest::$logs protected property Log messages to use in tests.
EntityPagerAnalyzerTest::mockSubscriberLog public function Event subscription mock function for ::testAnalyze().
EntityPagerAnalyzerTest::setUp protected function Overrides UnitTestCase::setUp
EntityPagerAnalyzerTest::testAnalyze public function @covers ::__construct @covers ::analyze
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.