You are here

class FileCacheTest in Zircon Profile 8

Same name in this branch
  1. 8 core/tests/Drupal/Tests/Component/FileCache/FileCacheTest.php \Drupal\Tests\Component\FileCache\FileCacheTest
  2. 8 vendor/doctrine/cache/tests/Doctrine/Tests/Common/Cache/FileCacheTest.php \Doctrine\Tests\Common\Cache\FileCacheTest
Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Component/FileCache/FileCacheTest.php \Drupal\Tests\Component\FileCache\FileCacheTest

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

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase

Expanded class hierarchy of FileCacheTest

File

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

Namespace

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

  /**
   * FileCache object used for the tests.
   *
   * @var \Drupal\Component\FileCache\FileCacheInterface
   */
  protected $fileCache;

  /**
   * Static FileCache object used for verification of tests.
   *
   * @var \Drupal\Component\FileCache\FileCacheBackendInterface
   */
  protected $staticFileCache;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->fileCache = new FileCache('prefix', 'test', '\\Drupal\\Tests\\Component\\FileCache\\StaticFileCacheBackend', [
      'bin' => 'llama',
    ]);
    $this->staticFileCache = new StaticFileCacheBackend([
      'bin' => 'llama',
    ]);
  }

  /**
   * @covers ::get
   * @covers ::__construct
   */
  public function testGet() {

    // Test a cache miss.
    $result = $this->fileCache
      ->get(__DIR__ . '/Fixtures/no-llama-42.yml');
    $this
      ->assertNull($result);

    // Test a cache hit.
    $filename = __DIR__ . '/Fixtures/llama-42.txt';
    $realpath = realpath($filename);
    $cid = 'prefix:test:' . $realpath;
    $data = [
      'mtime' => filemtime($realpath),
      'filepath' => $realpath,
      'data' => 42,
    ];
    $this->staticFileCache
      ->store($cid, $data);
    $result = $this->fileCache
      ->get($filename);
    $this
      ->assertEquals(42, $result);

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

  /**
   * @covers ::getMultiple
   */
  public function testGetMultiple() {

    // Test a cache miss.
    $result = $this->fileCache
      ->getMultiple([
      __DIR__ . '/Fixtures/no-llama-42.yml',
    ]);
    $this
      ->assertEmpty($result);

    // Test a cache hit.
    $filename = __DIR__ . '/Fixtures/llama-42.txt';
    $realpath = realpath($filename);
    $cid = 'prefix:test:' . $realpath;
    $data = [
      'mtime' => filemtime($realpath),
      'filepath' => $realpath,
      'data' => 42,
    ];
    $this->staticFileCache
      ->store($cid, $data);
    $result = $this->fileCache
      ->getMultiple([
      $filename,
    ]);
    $this
      ->assertEquals([
      $filename => 42,
    ], $result);

    // Test a static cache hit.
    $file2 = __DIR__ . '/Fixtures/llama-23.txt';
    $this->fileCache
      ->set($file2, 23);
    $result = $this->fileCache
      ->getMultiple([
      $filename,
      $file2,
    ]);
    $this
      ->assertEquals([
      $filename => 42,
      $file2 => 23,
    ], $result);

    // Cleanup static caches.
    $this->fileCache
      ->delete($filename);
    $this->fileCache
      ->delete($file2);
  }

  /**
   * @covers ::set
   */
  public function testSet() {
    $filename = __DIR__ . '/Fixtures/llama-23.txt';
    $realpath = realpath($filename);
    $cid = 'prefix:test:' . $realpath;
    $data = [
      'mtime' => filemtime($realpath),
      'filepath' => $realpath,
      'data' => 23,
    ];
    $this->fileCache
      ->set($filename, 23);
    $result = $this->staticFileCache
      ->fetch([
      $cid,
    ]);
    $this
      ->assertEquals([
      $cid => $data,
    ], $result);

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

  /**
   * @covers ::delete
   */
  public function testDelete() {
    $filename = __DIR__ . '/Fixtures/llama-23.txt';
    $realpath = realpath($filename);
    $cid = 'prefix:test:' . $realpath;
    $this->fileCache
      ->set($filename, 23);

    // Ensure data is removed after deletion.
    $this->fileCache
      ->delete($filename);
    $result = $this->staticFileCache
      ->fetch([
      $cid,
    ]);
    $this
      ->assertEquals([], $result);
    $result = $this->fileCache
      ->get($filename);
    $this
      ->assertNull($result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileCacheTest::$fileCache protected property FileCache object used for the tests.
FileCacheTest::$staticFileCache protected property Static FileCache object used for verification of tests.
FileCacheTest::setUp protected function Overrides UnitTestCase::setUp
FileCacheTest::testDelete public function @covers ::delete
FileCacheTest::testGet public function @covers ::get @covers ::__construct
FileCacheTest::testGetMultiple public function @covers ::getMultiple
FileCacheTest::testSet public function @covers ::set
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.