You are here

class StorageFactoryTest in Radioactivity 8.3

Same name and namespace in other branches
  1. 4.0.x tests/src/Unit/StorageFactoryTest.php \Drupal\Tests\radioactivity\Unit\StorageFactoryTest

@coversDefaultClass \Drupal\radioactivity\StorageFactory @group radioactivity

Hierarchy

Expanded class hierarchy of StorageFactoryTest

File

tests/src/Unit/StorageFactoryTest.php, line 17

Namespace

Drupal\Tests\radioactivity\Unit
View source
class StorageFactoryTest extends UnitTestCase {

  /**
   * Mocked immutable configuration object.
   *
   * @var \PHPUnit\Framework\MockObject\MockObject|\Drupal\Core\Config\ImmutableConfig
   */
  private $config;

  /**
   * Mocked class resolver.
   *
   * @var \PHPUnit\Framework\MockObject\MockObject|\Drupal\Core\DependencyInjection\ClassResolverInterface
   */
  private $classResolver;

  /**
   * Mocked config factory.
   *
   * @var \PHPUnit\Framework\MockObject\MockObject|\Drupal\Core\Config\ConfigFactory
   */
  private $configFactory;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();

    // Mock the radioactivity.storage configuration.
    $this->config = $this
      ->getMockBuilder(ImmutableConfig::class)
      ->disableOriginalConstructor()
      ->setMethods([
      'get',
    ])
      ->getMock();
    $this->configFactory = $this
      ->getMockBuilder(ConfigFactory::class)
      ->disableOriginalConstructor()
      ->setMethods([
      'get',
    ])
      ->getMock();
    $this->configFactory
      ->expects($this
      ->any())
      ->method('get')
      ->will($this
      ->returnValue($this->config));

    // Mock the class resolver and the classes it provides.
    $mockRestStorage = $this
      ->getMockBuilder(RestIncidentStorage::class)
      ->getMock();
    $mockDefaultStorage = $this
      ->getMockBuilder(DefaultIncidentStorage::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->classResolver = $this
      ->getMockBuilder(ClassResolverInterface::class)
      ->setMethods([
      'getInstanceFromDefinition',
    ])
      ->getMock();
    $this->classResolver
      ->expects($this
      ->any())
      ->method('getInstanceFromDefinition')
      ->will($this
      ->returnValueMap([
      [
        'radioactivity.rest_incident_storage',
        $mockRestStorage,
      ],
      [
        'radioactivity.default_incident_storage',
        $mockDefaultStorage,
      ],
    ]));
  }

  /**
   * @covers ::get
   * @dataProvider providerGet
   */
  public function testGet($storageType, $storageClass) {
    $sut = $this
      ->getMockBuilder(StorageFactory::class)
      ->setMethods()
      ->setConstructorArgs([
      $this->configFactory,
      $this->classResolver,
    ])
      ->getMock();
    $result = $sut
      ->get($storageType);
    $this
      ->assertInstanceOf($storageClass, $result);
  }

  /**
   * Data provider for testGet.
   *
   * @return array
   *   Storage type, storage class.
   */
  public function providerGet() {
    return [
      [
        'rest_local',
        RestIncidentStorage::class,
      ],
      [
        'rest_remote',
        RestIncidentStorage::class,
      ],
      [
        'default',
        DefaultIncidentStorage::class,
      ],
      [
        'unknown_type',
        DefaultIncidentStorage::class,
      ],
    ];
  }

  /**
   * @covers ::getConfiguredStorage
   * @dataProvider providerGetConfiguredStorage
   */
  public function testGetConfiguredStorage($configType, $storageType) {
    $sut = $this
      ->getMockBuilder('Drupal\\radioactivity\\StorageFactory')
      ->setMethods([
      'get',
    ])
      ->setConstructorArgs([
      $this->configFactory,
      $this->classResolver,
    ])
      ->getMock();
    $sut
      ->expects($this
      ->once())
      ->method('get')
      ->with($this
      ->equalTo($storageType));
    $this
      ->setConfig($configType, '');
    $sut
      ->getConfiguredStorage();
  }

  /**
   * Data provider for testGet.
   *
   * @return array
   *   Configured type, storage type.
   */
  public function providerGetConfiguredStorage() {
    return [
      [
        'rest_local',
        'rest_local',
      ],
      [
        'rest_remote',
        'rest_remote',
      ],
      [
        'default',
        'default',
      ],
      [
        NULL,
        'default',
      ],
    ];
  }

  /**
   * Sets mock configuration for StorageFactory.
   *
   * @param mixed $storageType
   *   The configured storage type.
   * @param mixed $endpoint
   *   The configured endpoint.
   */
  private function setConfig($storageType, $endpoint) {
    $this->config
      ->expects($this
      ->any())
      ->method('get')
      ->will($this
      ->returnValueMap([
      [
        'type',
        $storageType,
      ],
      [
        'endpoint',
        $endpoint,
      ],
    ]));
  }

}

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.
StorageFactoryTest::$classResolver private property Mocked class resolver.
StorageFactoryTest::$config private property Mocked immutable configuration object.
StorageFactoryTest::$configFactory private property Mocked config factory.
StorageFactoryTest::providerGet public function Data provider for testGet.
StorageFactoryTest::providerGetConfiguredStorage public function Data provider for testGet.
StorageFactoryTest::setConfig private function Sets mock configuration for StorageFactory.
StorageFactoryTest::setUp protected function Overrides UnitTestCase::setUp
StorageFactoryTest::testGet public function @covers ::get @dataProvider providerGet
StorageFactoryTest::testGetConfiguredStorage public function @covers ::getConfiguredStorage @dataProvider providerGetConfiguredStorage
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.