class StorageFactoryTest in Radioactivity 8.3
Same name and namespace in other branches
- 4.0.x tests/src/Unit/StorageFactoryTest.php \Drupal\Tests\radioactivity\Unit\StorageFactoryTest
@coversDefaultClass \Drupal\radioactivity\StorageFactory @group radioactivity
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait- class \Drupal\Tests\radioactivity\Unit\StorageFactoryTest
 
Expanded class hierarchy of StorageFactoryTest
File
- tests/src/ Unit/ StorageFactoryTest.php, line 17 
Namespace
Drupal\Tests\radioactivity\UnitView 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
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| PhpunitCompatibilityTrait:: | public | function | Returns a mock object for the specified class using the available method. | |
| PhpunitCompatibilityTrait:: | public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
| StorageFactoryTest:: | private | property | Mocked class resolver. | |
| StorageFactoryTest:: | private | property | Mocked immutable configuration object. | |
| StorageFactoryTest:: | private | property | Mocked config factory. | |
| StorageFactoryTest:: | public | function | Data provider for testGet. | |
| StorageFactoryTest:: | public | function | Data provider for testGet. | |
| StorageFactoryTest:: | private | function | Sets mock configuration for StorageFactory. | |
| StorageFactoryTest:: | protected | function | Overrides UnitTestCase:: | |
| StorageFactoryTest:: | public | function | @covers ::get @dataProvider providerGet | |
| StorageFactoryTest:: | public | function | @covers ::getConfiguredStorage @dataProvider providerGetConfiguredStorage | |
| UnitTestCase:: | protected | property | The random generator. | |
| UnitTestCase:: | protected | property | The app root. | 1 | 
| UnitTestCase:: | protected | function | Asserts if two arrays are equal by sorting them first. | |
| UnitTestCase:: | protected | function | Mocks a block with a block plugin. | 1 | 
| UnitTestCase:: | protected | function | Returns a stub class resolver. | |
| UnitTestCase:: | public | function | Returns a stub config factory that behaves according to the passed array. | |
| UnitTestCase:: | public | function | Returns a stub config storage that returns the supplied configuration. | |
| UnitTestCase:: | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase:: | protected | function | Gets the random generator for the utility methods. | |
| UnitTestCase:: | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase:: | public | function | Generates a unique random string containing letters and numbers. | 
