PhpStorageTestBase.php 1.37 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Tests\Component\PhpStorage\PhpStorageTestBase.
6 7
 */

8
namespace Drupal\Tests\Component\PhpStorage;
9

10
use Drupal\Tests\UnitTestCase;
11 12

/**
13
 * Base test for PHP storages.
14
 */
15
abstract class PhpStorageTestBase extends UnitTestCase {
16 17

  /**
18
   * Assert that a PHP storage's load/save/delete operations work.
19 20
   */
  public function assertCRUD($php) {
21
    $name = $this->randomMachineName() . '/' . $this->randomMachineName() . '.php';
22 23 24 25 26 27 28 29 30

    // 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);
31
    $this->assertSame($success, TRUE);
32 33 34 35 36
    $php->load($name);
    $this->assertTrue($GLOBALS[$random]);

    // If the file was successfully loaded, it must also exist, but ensure the
    // exists() method returns that correctly.
37
    $this->assertSame($php->exists($name), TRUE);
38 39 40

    // Delete the file, and then ensure exists() returns FALSE.
    $success = $php->delete($name);
41 42
    $this->assertSame($success, TRUE);
    $this->assertSame($php->exists($name), FALSE);
43 44 45

    // Ensure delete() can be called on a non-existing file. It should return
    // FALSE, but not trigger errors.
46
    $this->assertSame($php->delete($name), FALSE);
47
  }
48

49
}