You are here

class BlazyManagerUnitTest in Blazy 8.2

Same name and namespace in other branches
  1. 8 tests/src/Unit/BlazyManagerUnitTest.php \Drupal\Tests\blazy\Unit\BlazyManagerUnitTest

@coversDefaultClass \Drupal\blazy\BlazyManager

@group blazy

Hierarchy

Expanded class hierarchy of BlazyManagerUnitTest

File

tests/src/Unit/BlazyManagerUnitTest.php, line 14

Namespace

Drupal\Tests\blazy\Unit
View source
class BlazyManagerUnitTest extends UnitTestCase {
  use BlazyUnitTestTrait;
  use BlazyManagerUnitTestTrait;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this
      ->setUpUnitServices();
    $this
      ->setUpUnitContainer();
    $this
      ->setUpUnitImages();
  }

  /**
   * Tests cases for various methods.
   *
   * @covers ::getEntityTypeManager
   * @covers ::getModuleHandler
   * @covers ::getRenderer
   * @covers ::getCache
   * @covers ::getConfigFactory
   */
  public function testBlazyManagerServiceInstances() {
    $this
      ->assertInstanceOf('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface', $this->blazyManager
      ->getEntityTypeManager());
    $this
      ->assertInstanceOf('\\Drupal\\Core\\Extension\\ModuleHandlerInterface', $this->blazyManager
      ->getModuleHandler());
    $this
      ->assertInstanceOf('\\Drupal\\Core\\Render\\RendererInterface', $this->blazyManager
      ->getRenderer());
    $this
      ->assertInstanceOf('\\Drupal\\Core\\Config\\ConfigFactoryInterface', $this->blazyManager
      ->getConfigFactory());
    $this
      ->assertInstanceOf('\\Drupal\\Core\\Cache\\CacheBackendInterface', $this->blazyManager
      ->getCache());
  }

  /**
   * Tests cases for config.
   *
   * @covers ::configLoad
   */
  public function testConfigLoad() {
    $this->blazyManager
      ->expects($this
      ->any())
      ->method('configLoad')
      ->with('blazy')
      ->willReturn([
      'loadInvisible' => FALSE,
    ]);
    $blazy = $this->blazyManager
      ->configLoad('blazy');
    $this
      ->assertArrayHasKey('loadInvisible', $blazy);
    $this->blazyManager
      ->expects($this
      ->any())
      ->method('configLoad')
      ->with('admin_css')
      ->willReturn(TRUE);
    $this->blazyManager
      ->expects($this
      ->any())
      ->method('configLoad')
      ->with('responsive_image')
      ->willReturn(TRUE);
  }

  /**
   * Tests cases for config.
   *
   * @covers ::entityLoad
   * @covers ::entityLoadMultiple
   */
  public function testEntityLoadImageStyle() {
    $styles = $this
      ->setUpImageStyle();
    $ids = array_keys($styles);
    $this->blazyManager
      ->expects($this
      ->any())
      ->method('entityLoadMultiple')
      ->with('image_style')
      ->willReturn($styles);
    $multiple = $this->blazyManager
      ->entityLoadMultiple('image_style', $ids);
    $this
      ->assertArrayHasKey('large', $multiple);
    $this->blazyManager
      ->expects($this
      ->any())
      ->method('entityLoad')
      ->with('large')
      ->willReturn($multiple['large']);
    $expected = $this->blazyManager
      ->entityLoad('large', 'image_style');
    $this
      ->assertEquals($expected, $multiple['large']);
  }

  /**
   * Tests for \Drupal\blazy\BlazyManager::getBlazy().
   *
   * @covers ::getBlazy
   * @dataProvider providerTestGetBlazy
   */
  public function testGetBlazy($uri, $content, $expected_image, $expected_render) {
    $build = [];
    $build['item'] = NULL;
    $build['content'] = $content;
    $build['settings']['uri'] = $uri;
    $theme = [
      '#theme' => 'blazy',
      '#build' => [],
    ];
    $this->blazyManager
      ->expects($this
      ->any())
      ->method('getBlazy')
      ->willReturn($expected_image ? $theme : []);
    $image = $this->blazyManager
      ->getBlazy($build);
    $check_image = !$expected_image ? empty($image) : !empty($image);
    $this
      ->assertTrue($check_image);
  }

  /**
   * Provide test cases for ::testPreRenderImage().
   *
   * @return array
   *   An array of tested data.
   */
  public function providerTestGetBlazy() {
    $data[] = [
      '',
      '',
      FALSE,
      FALSE,
    ];
    $data[] = [
      'core/misc/druplicon.png',
      '',
      TRUE,
      TRUE,
    ];
    $data[] = [
      'core/misc/druplicon.png',
      '<iframe src="//www.youtube.com/watch?v=E03HFA923kw" class="b-lazy"></iframe>',
      FALSE,
      TRUE,
    ];
    return $data;
  }

  /**
   * Tests cases for attachments.
   *
   * @covers ::attach
   * @depends testConfigLoad
   */
  public function testAttach() {
    $attach = [
      'blazy' => TRUE,
      'grid' => 0,
      'media' => TRUE,
      'media_switch' => 'media',
      'ratio' => 'fluid',
      'style' => 'column',
    ];
    $this->blazyManager
      ->expects($this
      ->any())
      ->method('attach')
      ->with($attach)
      ->willReturn([
      'drupalSettings' => [
        'blazy' => [],
      ],
    ]);
    $attachments = $this->blazyManager
      ->attach($attach);
    $this->blazyManager
      ->expects($this
      ->any())
      ->method('attach')
      ->with($attach)
      ->willReturn([
      'drupalSettings' => [
        'blazy' => [],
      ],
    ]);
    $this
      ->assertArrayHasKey('blazy', $attachments['drupalSettings']);
  }

  /**
   * Tests cases for lightboxes.
   *
   * @covers ::getLightboxes
   */
  public function testGetLightboxes() {
    $this->blazyManager
      ->expects($this
      ->any())
      ->method('getLightboxes')
      ->willReturn([]);
    $lightboxes = $this->blazyManager
      ->getLightboxes();
    $this
      ->assertNotContains('nixbox', $lightboxes);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlazyManagerUnitTest::providerTestGetBlazy public function Provide test cases for ::testPreRenderImage().
BlazyManagerUnitTest::setUp protected function Overrides UnitTestCase::setUp
BlazyManagerUnitTest::testAttach public function Tests cases for attachments.
BlazyManagerUnitTest::testBlazyManagerServiceInstances public function Tests cases for various methods.
BlazyManagerUnitTest::testConfigLoad public function Tests cases for config.
BlazyManagerUnitTest::testEntityLoadImageStyle public function Tests cases for config.
BlazyManagerUnitTest::testGetBlazy public function Tests for \Drupal\blazy\BlazyManager::getBlazy().
BlazyManagerUnitTest::testGetLightboxes public function Tests cases for lightboxes.
BlazyManagerUnitTestTrait::setUpImageStyle protected function Prepare image styles.
BlazyManagerUnitTestTrait::setUpResponsiveImageStyle protected function Prepare Responsive image styles.
BlazyManagerUnitTestTrait::setUpUnitContainer protected function Setup the unit manager.
BlazyManagerUnitTestTrait::setUpUnitServices protected function Setup the unit manager.
BlazyPropertiesTestTrait::$blazyAdmin protected property The blazy admin service.
BlazyPropertiesTestTrait::$blazyAdminFormatter protected property The blazy admin service.
BlazyPropertiesTestTrait::$blazyEntity protected property The blazy entity service.
BlazyPropertiesTestTrait::$blazyManager protected property The blazy manager service.
BlazyPropertiesTestTrait::$bundle protected property The bundle name.
BlazyPropertiesTestTrait::$display protected property The entity display.
BlazyPropertiesTestTrait::$entities protected property The entity.
BlazyPropertiesTestTrait::$entity protected property The entity.
BlazyPropertiesTestTrait::$entityFieldManager protected property The entity manager.
BlazyPropertiesTestTrait::$entityPluginId protected property The tested entity reference formatter ID.
BlazyPropertiesTestTrait::$entityType protected property The tested entity type.
BlazyPropertiesTestTrait::$filterFormatFull protected property The filter format.
BlazyPropertiesTestTrait::$filterFormatRestricted protected property The filter format.
BlazyPropertiesTestTrait::$formatterDefinition protected property The formatter definition.
BlazyPropertiesTestTrait::$formatterPluginManager protected property The formatter plugin manager.
BlazyPropertiesTestTrait::$image protected property The created image item.
BlazyPropertiesTestTrait::$maxItems protected property The maximum number of created images.
BlazyPropertiesTestTrait::$maxParagraphs protected property The maximum number of created paragraphs.
BlazyPropertiesTestTrait::$node protected property The node entity.
BlazyPropertiesTestTrait::$referencedEntity protected property The referenced node entity.
BlazyPropertiesTestTrait::$referencingEntity protected property The node entity.
BlazyPropertiesTestTrait::$skins protected property The tested skins.
BlazyPropertiesTestTrait::$targetBundles protected property The target bundle names.
BlazyPropertiesTestTrait::$testEmptyName protected property The tested empty field name.
BlazyPropertiesTestTrait::$testEmptyType protected property The tested empty field type.
BlazyPropertiesTestTrait::$testFieldName protected property The tested field name.
BlazyPropertiesTestTrait::$testFieldType protected property The tested field type.
BlazyPropertiesTestTrait::$testItem protected property The created item.
BlazyPropertiesTestTrait::$testItems protected property The created items.
BlazyPropertiesTestTrait::$testPluginId protected property The tested formatter ID.
BlazyPropertiesTestTrait::$typeDefinition protected property The tested type definitions.
BlazyUnitTestTrait::$formatterSettings protected property The formatter settings.
BlazyUnitTestTrait::doPreRenderImage protected function Pre render Blazy image.
BlazyUnitTestTrait::getCacheMetaData protected function Return dummy cache metadata.
BlazyUnitTestTrait::getDefaulEntityFormatterDefinition protected function Returns the default field formatter definition.
BlazyUnitTestTrait::getDefaultFieldDefinition protected function Returns the default field definition.
BlazyUnitTestTrait::getDefaultFields protected function Returns dummy fields for an entity reference.
BlazyUnitTestTrait::getDefaultFormatterDefinition protected function Returns the default field formatter definition.
BlazyUnitTestTrait::getFormatterDefinition protected function Returns the field formatter definition along with settings.
BlazyUnitTestTrait::getFormatterSettings protected function Returns sensible formatter settings for testing purposes.
BlazyUnitTestTrait::setFormatterDefinition protected function Sets the field formatter definition.
BlazyUnitTestTrait::setFormatterSetting protected function Sets formatter setting.
BlazyUnitTestTrait::setFormatterSettings protected function Sets formatter settings.
BlazyUnitTestTrait::setUpMockImage protected function Setup the unit images.
BlazyUnitTestTrait::setUpUnitImages protected function Setup the unit images.
BlazyUnitTestTrait::setUpVariables protected function Set up Blazy variables.
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.