abstract class PhpStorageTestBase in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php \Drupal\Tests\Component\PhpStorage\PhpStorageTestBase
Base test for PHP storages.
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\Component\PhpStorage\PhpStorageTestBase
Expanded class hierarchy of PhpStorageTestBase
File
- core/
tests/ Drupal/ Tests/ Component/ PhpStorage/ PhpStorageTestBase.php, line 17 - Contains \Drupal\Tests\Component\PhpStorage\PhpStorageTestBase.
Namespace
Drupal\Tests\Component\PhpStorageView source
abstract class PhpStorageTestBase extends UnitTestCase {
/**
* A unique per test class directory path to test php storage.
*
* @var string
*/
protected $directory;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
vfsStream::setup('exampleDir');
$this->directory = vfsStream::url('exampleDir');
}
/**
* Assert that a PHP storage's load/save/delete operations work.
*/
public function assertCRUD($php) {
$name = $this
->randomMachineName() . '/' . $this
->randomMachineName() . '.php';
// Find a global that doesn't exist.
do {
$random = mt_rand(10000, 100000);
} while (isset($GLOBALS[$random]));
// Write out a PHP file and ensure it's successfully loaded.
$code = "<?php\n\$GLOBALS[{$random}] = TRUE;";
$success = $php
->save($name, $code);
$this
->assertTrue($success, 'Saved php file');
$php
->load($name);
$this
->assertTrue($GLOBALS[$random], 'File saved correctly with correct value');
// Run additional asserts.
$this
->additionalAssertCRUD($php, $name);
// If the file was successfully loaded, it must also exist, but ensure the
// exists() method returns that correctly.
$this
->assertTrue($php
->exists($name), 'Exists works correctly');
// Delete the file, and then ensure exists() returns FALSE.
$this
->assertTrue($php
->delete($name), 'Delete succeeded');
$this
->assertFalse($php
->exists($name), 'Delete deleted file');
// Ensure delete() can be called on a non-existing file. It should return
// FALSE, but not trigger errors.
$this
->assertFalse($php
->delete($name), 'Delete fails on missing file');
unset($GLOBALS[$random]);
}
/**
* Additional asserts to be run.
*
* @param \Drupal\Component\PhpStorage\PhpStorageInterface $php
* The PHP storage object.
* @param string $name
* The name of an object. It should exist in the storage.
*/
protected function additionalAssertCRUD(PhpStorageInterface $php, $name) {
// By default do not do any additional asserts. This is a way of extending
// tests in contrib.
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpStorageTestBase:: |
protected | property | A unique per test class directory path to test php storage. | |
PhpStorageTestBase:: |
protected | function | Additional asserts to be run. | |
PhpStorageTestBase:: |
public | function | Assert that a PHP storage's load/save/delete operations work. | |
PhpStorageTestBase:: |
protected | function |
Overrides UnitTestCase:: |
3 |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed in 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. |