. */ namespace SP\Tests\Storage; use Faker\Factory; use PHPUnit\Framework\TestCase; use SP\Storage\File\FileCache; use SP\Storage\File\FileException; use stdClass; /** * Class FileCacheTest * * @package SP\Tests\Storage */ class FileCacheTest extends TestCase { const CACHE_FILE = TMP_PATH . DIRECTORY_SEPARATOR . 'test.cache'; private static $data; /** * This method is called before the first test of this test class is run. */ public static function setUpBeforeClass(): void { self::$data = []; $i = 0; $faker = Factory::create(); do { $data = new stdClass(); $data->id = uniqid(); $data->name = $faker->name; $data->values = [1, 2, 3]; $data->object = new stdClass(); $data->object->uid = uniqid(); $data->object->type = $faker->address; $data->object->notes = $faker->text; self::$data[] = $data; $i++; } while ($i < 100); } /** * @throws FileException */ public function testDeleteInvalid() { $this->expectException(FileException::class); $cache = new FileCache(self::CACHE_FILE); $cache->delete(); } /** * @throws FileException */ public function testSave() { $cache = new FileCache(self::CACHE_FILE); $cache->save(self::$data); $this->assertFileExists(self::CACHE_FILE); } /** * @throws FileException */ public function testLoad() { $cache = new FileCache(self::CACHE_FILE); $data = $cache->load(); $this->assertEquals(self::$data, $data); } /** * @throws FileException */ public function testIsExpired() { // Sleep for 3 seconds before checking whether is expired sleep(3); $cache = new FileCache(self::CACHE_FILE); $this->assertTrue($cache->isExpired(2)); } /** * @throws FileException */ public function testIsExpiredDate() { // Sleep for 3 seconds before checking whether is expired sleep(3); $cache = new FileCache(self::CACHE_FILE); $this->assertTrue($cache->isExpiredDate(time())); } /** * @throws FileException */ public function testDelete() { $cache = new FileCache(self::CACHE_FILE); $cache->delete(); $this->assertTrue(true); } }