You are here

class RequiredTest in Tamper 8

Test the required plugin.

@coversDefaultClass \Drupal\tamper\Plugin\Tamper\Required @group tamper

Hierarchy

Expanded class hierarchy of RequiredTest

File

tests/src/Unit/Plugin/Tamper/RequiredTest.php, line 14

Namespace

Drupal\Tests\tamper\Unit\Plugin\Tamper
View source
class RequiredTest extends TamperPluginTestBase {

  /**
   * Instantiates a plugin.
   *
   * @return \Drupal\tamper\TamperInterface
   *   A tamper plugin.
   */
  protected function instantiatePlugin() {
    return $this
      ->getRequiredPlugin();
  }

  /**
   * Plugin instance, configured to require empty values.
   *
   * @var \Drupal\tamper\Plugin\Tamper\Required
   */
  protected $invertedPlugin;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->invertedPlugin = $this
      ->getInvertedPlugin();
    parent::setUp();
  }

  /**
   * Test required with a string.
   */
  public function testRequiredWithSingleValue() {
    $this
      ->assertEquals('Expected', $this->plugin
      ->tamper('Expected'));
  }

  /**
   * Test required with a string.
   */
  public function testInvertedRequiredWithSingleValue() {
    $this
      ->expectException(SkipTamperItemException::class);
    $this->invertedPlugin
      ->tamper('Expected');
  }

  /**
   * Test required with empty string.
   */
  public function testRequiredWithEmptyString() {
    $this
      ->expectException(SkipTamperItemException::class);
    $this->plugin
      ->tamper('');
  }

  /**
   * Test required with empty string.
   */
  public function testInvertedRequiredWithEmptyString() {
    $this
      ->assertEquals('', $this->invertedPlugin
      ->tamper(''));
  }

  /**
   * Test required with an array.
   */
  public function testRequiredWithMultipleValues() {
    $expected = [
      'foo' => 'bar',
      'zip' => '',
      'baz' => FALSE,
    ];
    $this
      ->assertEquals($expected, $this->plugin
      ->tamper($expected));
  }

  /**
   * Test required with an array.
   */
  public function testInvertedRequiredWithMultipleValues() {
    $this
      ->expectException(SkipTamperItemException::class);
    $expected = [
      'foo' => 'bar',
      'zip' => '',
      'baz' => FALSE,
    ];
    $this->invertedPlugin
      ->tamper($expected);
  }

  /**
   * Test required with false.
   */
  public function testRequiredWithBool() {
    $this
      ->expectException(SkipTamperItemException::class);
    $this->plugin
      ->tamper(FALSE);
  }

  /**
   * Test required with false.
   */
  public function testInvertedRequiredWithBool() {
    $this
      ->assertEquals(FALSE, $this->invertedPlugin
      ->tamper(FALSE));
  }

  /**
   * Test required with empty array.
   */
  public function testRequiredWithEmptyArray() {
    $this
      ->expectException(SkipTamperItemException::class);
    $this->plugin
      ->tamper([]);
  }

  /**
   * Test required with empty array.
   */
  public function testInvertedRequiredWithEmptyArray() {
    $this
      ->assertArrayEquals([], $this->invertedPlugin
      ->tamper([]));
  }

  /**
   * Test required with empty array.
   */
  public function testRequiredWithNull() {
    $this
      ->expectException(SkipTamperItemException::class);
    $this->plugin
      ->tamper(NULL);
  }

  /**
   * Test required with null.
   */
  public function testInvertedRequiredWithNull() {
    $this
      ->assertEquals(NULL, $this->invertedPlugin
      ->tamper(NULL));
  }

  /**
   * Get a plugin configured to require items.
   *
   * @return \Drupal\tamper\Plugin\Tamper\Required
   *   Plugin instance.
   */
  protected function getRequiredPlugin() {
    $config = [
      Required::SETTING_INVERT => FALSE,
    ];
    $plugin = new Required($config, 'required', [], $this
      ->getMockSourceDefinition());
    return $plugin;
  }

  /**
   * Get a plugin configured to require empty items.
   *
   * @return \Drupal\tamper\Plugin\Tamper\Required
   *   Plugin instance.
   */
  protected function getInvertedPlugin() {
    $config = [
      Required::SETTING_INVERT => TRUE,
    ];
    $plugin = new Required($config, 'required', [], $this
      ->getMockSourceDefinition());
    return $plugin;
  }

}

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.
RequiredTest::$invertedPlugin protected property Plugin instance, configured to require empty values.
RequiredTest::getInvertedPlugin protected function Get a plugin configured to require empty items.
RequiredTest::getRequiredPlugin protected function Get a plugin configured to require items.
RequiredTest::instantiatePlugin protected function Instantiates a plugin. Overrides TamperPluginTestBase::instantiatePlugin
RequiredTest::setUp protected function Overrides TamperPluginTestBase::setUp
RequiredTest::testInvertedRequiredWithBool public function Test required with false.
RequiredTest::testInvertedRequiredWithEmptyArray public function Test required with empty array.
RequiredTest::testInvertedRequiredWithEmptyString public function Test required with empty string.
RequiredTest::testInvertedRequiredWithMultipleValues public function Test required with an array.
RequiredTest::testInvertedRequiredWithNull public function Test required with null.
RequiredTest::testInvertedRequiredWithSingleValue public function Test required with a string.
RequiredTest::testRequiredWithBool public function Test required with false.
RequiredTest::testRequiredWithEmptyArray public function Test required with empty array.
RequiredTest::testRequiredWithEmptyString public function Test required with empty string.
RequiredTest::testRequiredWithMultipleValues public function Test required with an array.
RequiredTest::testRequiredWithNull public function Test required with empty array.
RequiredTest::testRequiredWithSingleValue public function Test required with a string.
TamperPluginTestBase::$plugin protected property The tamper plugin under test.
TamperPluginTestBase::getMockSourceDefinition protected function Returns a mocked source definition.
TamperPluginTestBase::testBuildConfigurationForm public function @covers ::buildConfigurationForm
TamperPluginTestBase::testDefaultConfiguration public function @covers ::defaultConfiguration
TamperPluginTestBase::testGetConfiguration public function @covers ::getConfiguration
TamperPluginTestBase::testGetPluginDefinition public function @covers ::getPluginDefinition
TamperPluginTestBase::testGetPluginId public function @covers ::getPluginId
TamperPluginTestBase::testMultiple public function @covers ::multiple
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.