You are here

class JQueryColorpickerServiceTest in Jquery Colorpicker 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/Service/jQueryColorpickerServiceTest.php \Drupal\Test\jquery_colorpicker\Service\JQueryColorpickerServiceTest

@coversDefaultClass \Drupal\jquery_colorpicker\Service\JQueryColorpickerService @group jquery_colorpicker

Hierarchy

Expanded class hierarchy of JQueryColorpickerServiceTest

File

tests/src/Unit/Service/jQueryColorpickerServiceTest.php, line 14

Namespace

Drupal\Test\jquery_colorpicker\Service
View source
class JQueryColorpickerServiceTest extends UnitTestCase {
  use StringTranslationTrait;

  /**
   * The JQuery Colorpicker Service.
   *
   * @var \Drupal\jquery_colorpicker\Service\JQueryColorpickerService
   */
  protected $JQueryColorpickerService;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $container = new ContainerBuilder();
    $container
      ->set('string_translation', $this
      ->getStringTranslationStub());
    \Drupal::setContainer($container);
    $this->JQueryColorpickerService = new JQueryColorpickerService();
  }

  /**
   * @covers ::formatColor
   *
   * @dataProvider providerTestFormatColor
   */
  public function testFormatColor($expected, $color) {
    $this
      ->assertSame($expected, $this->JQueryColorpickerService
      ->formatColor($color));
  }

  /**
   * Provides data for the testFormatColor test.
   */
  public function providerTestFormatcolor() {
    $data = [];
    $data[] = [
      '',
      NULL,
    ];
    $data[] = [
      '',
      '',
    ];
    $data[] = [
      "1",
      TRUE,
    ];
    $data[] = [
      "",
      FALSE,
    ];
    $data[] = [
      "123",
      123,
    ];
    $data[] = [
      "1.23",
      1.23,
    ];
    $data[] = [
      "123456",
      "123456",
    ];
    $data[] = [
      "123456",
      "#123456",
    ];
    $data[] = [
      "",
      [],
    ];
    $test_class = new \stdClass();
    $data[] = [
      "",
      $test_class,
    ];
    return $data;
  }

  /**
   * @covers ::validateColor
   *
   * @dataProvider providerTestValidateColor
   */
  public function testValidateColor($expected, $color) {
    $this
      ->assertEquals($expected, $this->JQueryColorpickerService
      ->validateColor($color));
  }

  /**
   * Provides data for the testValidateColor test.
   */
  public function providerTestValidateColor() {
    $container = new ContainerBuilder();
    $container
      ->set('string_translation', $this
      ->getStringTranslationStub());
    \Drupal::setContainer($container);
    $type_error = $this
      ->t('Color must be a string or an integer');
    $length_error = $this
      ->t('Color values must be exactly six characters in length');
    $hex_error = $this
      ->t("You entered an invalid value for the color. Colors must be hexadecimal, and can only contain the characters '0-9', 'a-f' and/or 'A-F'.");
    $data = [];
    $data[] = [
      $type_error,
      TRUE,
    ];
    $data[] = [
      $type_error,
      FALSE,
    ];
    $data[] = [
      $type_error,
      [],
    ];
    $test = new \stdClass();
    $data[] = [
      $type_error,
      $test,
    ];
    $data[] = [
      $type_error,
      1.23,
    ];
    $data[] = [
      $length_error,
      12345,
    ];
    $data[] = [
      $length_error,
      "12345",
    ];
    $data[] = [
      $hex_error,
      "11111g",
    ];
    $data[] = [
      $hex_error,
      "11111G",
    ];
    $data[] = [
      $hex_error,
      "fffffg",
    ];
    $data[] = [
      $hex_error,
      "fffffG",
    ];
    $data[] = [
      $hex_error,
      "FFFFFg",
    ];
    $data[] = [
      $hex_error,
      "FFFFFG",
    ];

    // Valid submissions.
    $data[] = [
      FALSE,
      123456,
    ];
    $data[] = [
      FALSE,
      "123456",
    ];
    $data[] = [
      FALSE,
      "11111f",
    ];
    $data[] = [
      FALSE,
      "11111F",
    ];
    $data[] = [
      FALSE,
      "FFFFF1",
    ];
    $data[] = [
      FALSE,
      "fffff1",
    ];
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JQueryColorpickerServiceTest::$JQueryColorpickerService protected property The JQuery Colorpicker Service.
JQueryColorpickerServiceTest::providerTestFormatcolor public function Provides data for the testFormatColor test.
JQueryColorpickerServiceTest::providerTestValidateColor public function Provides data for the testValidateColor test.
JQueryColorpickerServiceTest::setUp public function Overrides UnitTestCase::setUp
JQueryColorpickerServiceTest::testFormatColor public function @covers ::formatColor
JQueryColorpickerServiceTest::testValidateColor public function @covers ::validateColor
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.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
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.