You are here

class CarouselServiceTest in bootstrap simple carousel 8

@coversDefaultClass \Drupal\bootstrap_simple_carousel\Service\CarouselService

@group bootstrap_simple_carousel

Hierarchy

Expanded class hierarchy of CarouselServiceTest

File

tests/src/Unit/Service/CarouselServiceTest.php, line 17

Namespace

Drupal\Tests\bootstrap_simple_carousel\Unit\Service
View source
class CarouselServiceTest extends UnitTestCase {
  use PhpunitCompatibilityTrait;

  /**
   * The mocked renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $renderer;

  /**
   * The mocked entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->renderer = $this
      ->createMock('\\Drupal\\Core\\Render\\RendererInterface');
    $this->entityTypeManager = $this
      ->createMock('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface');
  }

  /**
   * Tests the renderLink() method.
   *
   * @covers ::renderLink
   *
   * @dataProvider providerTestRenderLink
   */
  public function testRenderLink(string $url, string $title, array $attributes, array $with, string $expected) {
    $this->renderer
      ->expects($this
      ->exactly(1))
      ->method('render')
      ->with($with)
      ->willReturn($expected);
    $carouselService = new CarouselService($this->renderer, $this->entityTypeManager);
    $actual = $carouselService
      ->renderLink($url, $title, $attributes);
    $this
      ->assertSame($expected, $actual);
  }

  /**
   * Tests the renderImageById() method.
   *
   * @covers ::renderImageById
   *
   * @dataProvider providerTestRenderImageById
   */
  public function testRenderImageById(int $imageId, $file, string $imageStyle, array $params, array $with, int $renderCount, string $expected) {
    $storage = $this
      ->createMock(EntityStorageInterface::class);
    $storage
      ->expects($this
      ->once())
      ->method('load')
      ->with($imageId)
      ->willReturn($file);
    $this->entityTypeManager
      ->expects($this
      ->once())
      ->method('getStorage')
      ->willReturn($storage);
    $this->renderer
      ->expects($this
      ->exactly($renderCount))
      ->method('render')
      ->with($with)
      ->willReturn($expected);
    $carouselService = new CarouselService($this->renderer, $this->entityTypeManager);
    $actual = $carouselService
      ->renderImageById($imageId, $imageStyle, $params);
    $this
      ->assertSame($expected, $actual);
  }

  /**
   * Tests the getStatuses() method.
   *
   * @covers ::getStatuses
   */
  public function testGetStatuses() {
    $stringTranslation = $this
      ->createMock(TranslationInterface::class);
    $carouselService = new CarouselService($this->renderer, $this->entityTypeManager);
    $carouselService
      ->setStringTranslation($stringTranslation);
    $this
      ->assertSame(count([
      'Inactive',
      'Active',
    ]), count($carouselService
      ->getStatuses()));
  }

  /**
   * Provides test data for providerTestRenderImageById.
   *
   * @return array
   *   The test data.
   */
  public function providerTestRenderImageById() {
    $url = 'public://directory/file.jpg';
    $file = $this
      ->createMock(FileInterface::class);
    $file
      ->expects($this
      ->once())
      ->method('getFileUri')
      ->willReturn($url);
    $imageStyle = 'style_slider';
    $params = [
      'alt' => 'alt',
      'title' => 'title',
    ];
    $with = [
      '#theme' => 'image_style',
      '#style_name' => $imageStyle,
      '#uri' => $url,
      '#alt' => $params['alt'],
      '#title' => $params['title'],
    ];
    return [
      [
        5,
        $file,
        $imageStyle,
        $params,
        $with,
        1,
        '/directory/file.jpg',
      ],
      [
        33,
        NULL,
        $imageStyle,
        $params,
        $with,
        0,
        '',
      ],
    ];
  }

  /**
   * Provides test data for providerTestRenderLink.
   *
   * @return array
   *   The test data.
   */
  public function providerTestRenderLink() {
    $url = 'http://example.com';
    $title = 'example';
    $expected = '<a href="http://example.com">example</a>';
    $with = [
      '#type' => 'link',
      '#title' => $title,
      '#url' => $url,
      '#options' => [
        'attributes' => [],
        'html' => FALSE,
      ],
    ];
    return [
      [
        $url,
        $title,
        [],
        $with,
        $expected,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CarouselServiceTest::$entityTypeManager protected property The mocked entity type manager.
CarouselServiceTest::$renderer protected property The mocked renderer.
CarouselServiceTest::providerTestRenderImageById public function Provides test data for providerTestRenderImageById.
CarouselServiceTest::providerTestRenderLink public function Provides test data for providerTestRenderLink.
CarouselServiceTest::setUp protected function Overrides UnitTestCase::setUp
CarouselServiceTest::testGetStatuses public function Tests the getStatuses() method.
CarouselServiceTest::testRenderImageById public function Tests the renderImageById() method.
CarouselServiceTest::testRenderLink public function Tests the renderLink() method.
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.