You are here

class FileCacheFactoryTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php \Drupal\Tests\Component\FileCache\FileCacheFactoryTest

@coversDefaultClass \Drupal\Component\FileCache\FileCacheFactory @group FileCache

Hierarchy

Expanded class hierarchy of FileCacheFactoryTest

File

core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php, line 17
Contains \Drupal\Tests\Component\FileCache\FileCacheFactoryTest.

Namespace

Drupal\Tests\Component\FileCache
View source
class FileCacheFactoryTest extends UnitTestCase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $settings = [
      'collection' => 'test-23',
      'cache_backend_class' => '\\Drupal\\Tests\\Component\\FileCache\\StaticFileCacheBackend',
      'cache_backend_configuration' => [
        'bin' => 'dog',
      ],
    ];
    $configuration = FileCacheFactory::getConfiguration();
    if (!$configuration) {
      $configuration = [];
    }
    $configuration += [
      'test_foo_settings' => $settings,
    ];
    FileCacheFactory::setConfiguration($configuration);
    FileCacheFactory::setPrefix('prefix');
  }

  /**
   * @covers ::get
   */
  public function testGet() {
    $file_cache = FileCacheFactory::get('test_foo_settings', []);

    // Ensure the right backend and configuration is used.
    $filename = __DIR__ . '/Fixtures/llama-23.txt';
    $realpath = realpath($filename);
    $cid = 'prefix:test-23:' . $realpath;
    $file_cache
      ->set($filename, 23);
    $static_cache = new StaticFileCacheBackend([
      'bin' => 'dog',
    ]);
    $result = $static_cache
      ->fetch([
      $cid,
    ]);
    $this
      ->assertNotEmpty($result);

    // Cleanup static caches.
    $file_cache
      ->delete($filename);
  }

  /**
   * @covers ::get
   *
   * @expectedException \InvalidArgumentException
   * @expectedExceptionMessage Required prefix configuration is missing
   */
  public function testGetNoPrefix() {
    FileCacheFactory::setPrefix(NULL);
    FileCacheFactory::get('test_foo_settings', []);
  }

  /**
   * @covers ::getConfiguration
   * @covers ::setConfiguration
   */
  public function testGetSetConfiguration() {
    $configuration = FileCacheFactory::getConfiguration();
    $configuration['test_foo_bar'] = [
      'bar' => 'llama',
    ];
    FileCacheFactory::setConfiguration($configuration);
    $configuration = FileCacheFactory::getConfiguration();
    $this
      ->assertEquals([
      'bar' => 'llama',
    ], $configuration['test_foo_bar']);
  }

  /**
   * @covers ::getPrefix
   * @covers ::setPrefix
   */
  public function testGetSetPrefix() {
    $prefix = $this
      ->randomMachineName();
    FileCacheFactory::setPrefix($prefix);
    $this
      ->assertEquals($prefix, FileCacheFactory::getPrefix());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileCacheFactoryTest::setUp protected function Overrides UnitTestCase::setUp
FileCacheFactoryTest::testGet public function @covers ::get
FileCacheFactoryTest::testGetNoPrefix public function @covers ::get
FileCacheFactoryTest::testGetSetConfiguration public function @covers ::getConfiguration @covers ::setConfiguration
FileCacheFactoryTest::testGetSetPrefix public function @covers ::getPrefix @covers ::setPrefix
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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.