You are here

class ConvertBooleanTest in Tamper 8

Tests the convert boolean plugin.

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

Hierarchy

Expanded class hierarchy of ConvertBooleanTest

File

tests/src/Unit/Plugin/Tamper/ConvertBooleanTest.php, line 13

Namespace

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

  /**
   * {@inheritdoc}
   */
  protected function instantiatePlugin() {
    $config = [
      ConvertBoolean::SETTING_TRUTH_VALUE => 'A',
      ConvertBoolean::SETTING_FALSE_VALUE => 'B',
      ConvertBoolean::SETTING_MATCH_CASE => FALSE,
      ConvertBoolean::SETTING_NO_MATCH => 'No match',
      ConvertBoolean::SETTING_OTHER_TEXT => '',
    ];
    return new ConvertBoolean($config, 'convert_boolean', [], $this
      ->getMockSourceDefinition());
  }

  /**
   * Test convert to boolean basic functionality.
   */
  public function testConvertBooleanBasicFunctionality() {
    $this
      ->assertEquals(TRUE, $this->plugin
      ->tamper('A'));
    $this
      ->assertEquals(TRUE, $this->plugin
      ->tamper('a'));
    $this
      ->assertEquals(FALSE, $this->plugin
      ->tamper('B'));
    $this
      ->assertEquals(FALSE, $this->plugin
      ->tamper('b'));
    $this
      ->assertEquals('No match', $this->plugin
      ->tamper('c'));
    $this
      ->assertEquals('No match', $this->plugin
      ->tamper('C'));
  }

  /**
   * Test convert to boolean no match false case.
   */
  public function testConvertBooleanNoMatchFalse() {
    $config = [
      ConvertBoolean::SETTING_TRUTH_VALUE => 'A',
      ConvertBoolean::SETTING_FALSE_VALUE => 'B',
      ConvertBoolean::SETTING_MATCH_CASE => FALSE,
      ConvertBoolean::SETTING_NO_MATCH => 'pass',
      ConvertBoolean::SETTING_OTHER_TEXT => '',
    ];
    $plugin = new ConvertBoolean($config, 'convert_boolean', [], $this
      ->getMockSourceDefinition());
    $this
      ->assertEquals(TRUE, $plugin
      ->tamper('A'));
    $this
      ->assertEquals(TRUE, $plugin
      ->tamper('a'));
    $this
      ->assertEquals(FALSE, $plugin
      ->tamper('B'));
    $this
      ->assertEquals(FALSE, $plugin
      ->tamper('b'));
    $this
      ->assertEquals('c', $plugin
      ->tamper('c'));
    $this
      ->assertEquals('C', $plugin
      ->tamper('C'));
  }

  /**
   * Test convert to boolean no match true case.
   */
  public function testConvertBooleanNoMatchTrue() {
    $config = [
      ConvertBoolean::SETTING_TRUTH_VALUE => 'A',
      ConvertBoolean::SETTING_FALSE_VALUE => 'B',
      ConvertBoolean::SETTING_MATCH_CASE => TRUE,
      ConvertBoolean::SETTING_NO_MATCH => 'No match',
      ConvertBoolean::SETTING_OTHER_TEXT => '',
    ];
    $plugin = new ConvertBoolean($config, 'convert_boolean', [], $this
      ->getMockSourceDefinition());
    $this
      ->assertEquals(TRUE, $plugin
      ->tamper('A'));
    $this
      ->assertNotEquals(TRUE, $plugin
      ->tamper('a'));
    $this
      ->assertEquals(FALSE, $plugin
      ->tamper('B'));
    $this
      ->assertNotEquals(FALSE, $plugin
      ->tamper('b'));
  }

  /**
   * Test convert to boolean no match true case.
   */
  public function testConvertBooleanNoMatchNull() {
    $config = [
      ConvertBoolean::SETTING_TRUTH_VALUE => 'A',
      ConvertBoolean::SETTING_FALSE_VALUE => 'B',
      ConvertBoolean::SETTING_MATCH_CASE => TRUE,
      ConvertBoolean::SETTING_NO_MATCH => NULL,
      ConvertBoolean::SETTING_OTHER_TEXT => '',
    ];
    $plugin = new ConvertBoolean($config, 'convert_boolean', [], $this
      ->getMockSourceDefinition());
    $this
      ->assertEquals(TRUE, $plugin
      ->tamper('A'));
    $this
      ->assertEquals(NULL, $plugin
      ->tamper('a'));
    $this
      ->assertEquals(FALSE, $plugin
      ->tamper('B'));
    $this
      ->assertEquals(NULL, $plugin
      ->tamper('b'));
    $this
      ->assertEquals(NULL, $plugin
      ->tamper('c'));
    $this
      ->assertEquals(NULL, $plugin
      ->tamper('C'));
  }

  /**
   * Test convert to boolean other text case.
   */
  public function testConvertBooleanOtherText() {
    $config = [
      ConvertBoolean::SETTING_TRUTH_VALUE => 'A',
      ConvertBoolean::SETTING_FALSE_VALUE => 'B',
      ConvertBoolean::SETTING_MATCH_CASE => TRUE,
      ConvertBoolean::SETTING_NO_MATCH => 'other text',
      ConvertBoolean::SETTING_OTHER_TEXT => 'other text',
    ];
    $plugin = new ConvertBoolean($config, 'convert_boolean', [], $this
      ->getMockSourceDefinition());
    $this
      ->assertEquals('other text', $plugin
      ->tamper('a'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConvertBooleanTest::instantiatePlugin protected function Instantiates a plugin. Overrides TamperPluginTestBase::instantiatePlugin
ConvertBooleanTest::testConvertBooleanBasicFunctionality public function Test convert to boolean basic functionality.
ConvertBooleanTest::testConvertBooleanNoMatchFalse public function Test convert to boolean no match false case.
ConvertBooleanTest::testConvertBooleanNoMatchNull public function Test convert to boolean no match true case.
ConvertBooleanTest::testConvertBooleanNoMatchTrue public function Test convert to boolean no match true case.
ConvertBooleanTest::testConvertBooleanOtherText public function Test convert to boolean other text case.
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.
TamperPluginTestBase::$plugin protected property The tamper plugin under test.
TamperPluginTestBase::getMockSourceDefinition protected function Returns a mocked source definition.
TamperPluginTestBase::setUp protected function Overrides UnitTestCase::setUp 3
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.