PhpStorageTestBase.php in Drupal 10
File
core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php
View source
<?php
namespace Drupal\Tests\Component\PhpStorage;
use Drupal\Component\PhpStorage\PhpStorageInterface;
use Drupal\Component\Utility\Random;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
abstract class PhpStorageTestBase extends TestCase {
protected $directory;
protected function setUp() : void {
parent::setUp();
vfsStream::setup('exampleDir');
$this->directory = vfsStream::url('exampleDir');
}
public function assertCRUD($php) {
$random_generator = new Random();
$name = $random_generator
->name(8, TRUE) . '/' . $random_generator
->name(8, TRUE) . '.php';
do {
$random = mt_rand(10000, 100000);
} while (isset($GLOBALS[$random]));
$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');
$this
->additionalAssertCRUD($php, $name);
$this
->assertTrue($php
->exists($name), 'Exists works correctly');
$this
->assertTrue($php
->delete($name), 'Delete succeeded');
$this
->assertFalse($php
->exists($name), 'Delete deleted file');
$this
->assertFalse($php
->delete($name), 'Delete fails on missing file');
unset($GLOBALS[$random]);
}
protected function additionalAssertCRUD(PhpStorageInterface $php, $name) {
}
}