You are here

class ChartsDefaultSettingsTest in Charts 8.4

Same name and namespace in other branches
  1. 8.3 tests/src/Unit/Settings/ChartsDefaultSettingsTest.php \Drupal\Tests\charts\Unit\Settings\ChartsDefaultSettingsTest
  2. 5.0.x tests/src/Unit/Settings/ChartsDefaultSettingsTest.php \Drupal\Tests\charts\Unit\Settings\ChartsDefaultSettingsTest

@coversDefaultClass \Drupal\charts\Settings\ChartsDefaultSettings @group charts

Hierarchy

Expanded class hierarchy of ChartsDefaultSettingsTest

File

tests/src/Unit/Settings/ChartsDefaultSettingsTest.php, line 13

Namespace

Drupal\Tests\charts\Unit\Settings
View source
class ChartsDefaultSettingsTest extends UnitTestCase {

  /**
   * The chart default settings.
   *
   * @var \Drupal\charts\Settings\ChartsDefaultSettings
   */
  private $chartsDefaultSettings;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $chartsDefaultColorsMock = $this
      ->getDefaultColorsMock();
    $this->chartsDefaultSettings = new ChartsDefaultSettings();
    $colorsProperty = new \ReflectionProperty(ChartsDefaultSettings::class, 'colors');
    $colorsProperty
      ->setAccessible(TRUE);
    $colorsProperty
      ->setValue($this->chartsDefaultSettings, $chartsDefaultColorsMock);
  }

  /**
   * Get a default colors mock.
   */
  private function getDefaultColorsMock() {
    $chartsDefaultColors = $this
      ->prophesize(ChartsDefaultColors::class);
    $chartsDefaultColors
      ->getDefaultColors()
      ->willReturn([
      '#2f7ed8',
    ]);
    return $chartsDefaultColors
      ->reveal();
  }

  /**
   * {@inheritdoc}
   */
  public function tearDown() {
    parent::tearDown();
    $this->chartsDefaultSettings = NULL;
  }

  /**
   * Tests the number of defaults settings.
   */
  public function testNumberOfDefaultSettings() {
    $this
      ->assertCount(39, $this->chartsDefaultSettings
      ->getDefaults());
    $this
      ->assertCount(7, $this->chartsDefaultSettings
      ->getDefaults(TRUE));
  }

  /**
   * Tests getter and setter for defaults.
   *
   * @param array $defaults
   *   Array of default settings.
   *
   * @dataProvider defaultSettingsProvider
   */
  public function testDefaults(array $defaults) {

    // Legacy config.
    $this->chartsDefaultSettings
      ->setDefaults($defaults);
    $this
      ->assertArrayEquals($defaults, $this->chartsDefaultSettings
      ->getDefaults());

    // New format config. This also allow us to test the transform to new;
    // Format.
    $keys_mapping = ChartsDefaultSettings::getLegacySettingsMappingKeys();
    $keys_mapping['colors'] = 'display_colors';
    $new_format = ChartsDefaultSettings::transformLegacySettingsToNew($defaults, $keys_mapping);
    $this
      ->assertArrayEquals($new_format, $this->chartsDefaultSettings
      ->getDefaults(TRUE));
  }

  /**
   * Data provider for setDefaults.
   */
  public function defaultSettingsProvider() {
    (yield [
      [
        'width' => 400,
        'width_units' => 'px',
        'height' => 300,
        'height_units' => 'px',
        'colors' => [
          '#2f7ed8',
        ],
      ],
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ChartsDefaultSettingsTest::$chartsDefaultSettings private property The chart default settings.
ChartsDefaultSettingsTest::defaultSettingsProvider public function Data provider for setDefaults.
ChartsDefaultSettingsTest::getDefaultColorsMock private function Get a default colors mock.
ChartsDefaultSettingsTest::setUp public function Overrides UnitTestCase::setUp
ChartsDefaultSettingsTest::tearDown public function
ChartsDefaultSettingsTest::testDefaults public function Tests getter and setter for defaults.
ChartsDefaultSettingsTest::testNumberOfDefaultSettings public function Tests the number of defaults settings.
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.