You are here

class ShowTextWhenEmptyProcessorTest in Facets 8

Class ShowTextWhenEmptyProcessorTest.

@group facets @coversDefaultClass \Drupal\facets_summary\Plugin\facets_summary\processor\ShowTextWhenEmptyProcessor

Hierarchy

Expanded class hierarchy of ShowTextWhenEmptyProcessorTest

File

modules/facets_summary/tests/src/Unit/Plugin/Processor/ShowTextWhenEmptyProcessorTest.php, line 18

Namespace

Drupal\Tests\facets_summary\Unit\Plugin\Processor
View source
class ShowTextWhenEmptyProcessorTest extends UnitTestCase {

  /**
   * The processor we're testing.
   *
   * @var \Drupal\facets_summary\Processor\ProcessorInterface|\Drupal\facets_summary\Processor\BuildProcessorInterface
   */
  protected $processor;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $string_translation = $this
      ->prophesize(TranslationInterface::class);
    $container = new ContainerBuilder();
    $container
      ->set('string_translation', $string_translation
      ->reveal());
    \Drupal::setContainer($container);
    $this->processor = new ShowTextWhenEmptyProcessor([], 'show_text_when_empty', []);
  }

  /**
   * Tests the is hidden method.
   *
   * @covers ::isHidden
   */
  public function testIsHidden() {
    $this
      ->assertFalse($this->processor
      ->isHidden());
  }

  /**
   * Tests the is locked method.
   *
   * @covers ::isLocked
   */
  public function testIsLocked() {
    $this
      ->assertFalse($this->processor
      ->isLocked());
  }

  /**
   * Tests the build method.
   *
   * @covers ::build
   */
  public function testBuild() {
    $summary = new FacetsSummary([], 'facets_summary');
    $summary
      ->setFacetSourceId('foo');
    $result = $this->processor
      ->build($summary, [
      'foo',
    ], []);
    $this
      ->assertSame('array', gettype($result));
    $this
      ->assertArrayHasKey('#theme', $result);
    $this
      ->assertEquals('facets_summary_empty', $result['#theme']);
    $this
      ->assertArrayHasKey('#message', $result);
    $configuration = [
      'text' => [
        'value' => 'llama',
        'format' => 'html',
      ],
    ];
    $this->processor
      ->setConfiguration($configuration);
    $result = $this->processor
      ->build($summary, [
      'foo',
    ], []);
    $this
      ->assertEquals('llama', $result['#message']['#text']);
  }

  /**
   * Tests the build method.
   *
   * @covers ::build
   */
  public function testBuildWithEmptyItems() {
    $summary = new FacetsSummary([], 'facets_summary');
    $summary
      ->setFacetSourceId('foo');
    $build = [
      '#items' => [],
    ];
    $result = $this->processor
      ->build($summary, $build, []);
    $this
      ->assertSame('array', gettype($result));
    $this
      ->assertArrayHasKey('#theme', $result);
    $this
      ->assertEquals('facets_summary_empty', $result['#theme']);
    $this
      ->assertArrayHasKey('#message', $result);
    $this
      ->assertArrayHasKey('#text', $result['#message']);
    $this
      ->assertEquals(new TranslatableMarkup('No results found.'), (string) $result['#message']['#text']);
    $this
      ->assertEquals('plain_text', $result['#message']['#format']);
  }

  /**
   * Tests build with config changes.
   *
   * @covers ::build
   */
  public function testBuildWithConfigChange() {
    $summary = new FacetsSummary([], 'facets_summary');
    $summary
      ->setFacetSourceId('foo');
    $build = [
      '#items' => [],
    ];
    $this->processor
      ->setConfiguration([
      'text' => [
        'value' => 'Owl',
        'format' => 'llama',
      ],
    ]);
    $result = $this->processor
      ->build($summary, $build, []);
    $this
      ->assertSame('array', gettype($result));
    $this
      ->assertArrayHasKey('#text', $result['#message']);
    $this
      ->assertEquals('Owl', (string) $result['#message']['#text']);
    $this
      ->assertEquals('llama', $result['#message']['#format']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
ShowTextWhenEmptyProcessorTest::$processor protected property The processor we're testing.
ShowTextWhenEmptyProcessorTest::setUp public function Overrides UnitTestCase::setUp
ShowTextWhenEmptyProcessorTest::testBuild public function Tests the build method.
ShowTextWhenEmptyProcessorTest::testBuildWithConfigChange public function Tests build with config changes.
ShowTextWhenEmptyProcessorTest::testBuildWithEmptyItems public function Tests the build method.
ShowTextWhenEmptyProcessorTest::testIsHidden public function Tests the is hidden method.
ShowTextWhenEmptyProcessorTest::testIsLocked public function Tests the is locked method.
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.