. */ namespace SP\Tests\Storage; use PHPUnit\Framework\TestCase; use SP\Storage\File\FileException; use SP\Storage\File\FileHandler; /** * Class FileHandlerTest * * Tests unitarios para comprobar el funcionamiento de la clase SP\Storage\File\FileHandler * * @package SP\Tests */ class FileHandlerTest extends TestCase { /** * @var string Archvivo de prueba válido */ protected static $validFile = RESOURCE_PATH . DIRECTORY_SEPARATOR . 'valid_file.test'; /** * @var string Archvivo de prueba inmutable */ protected static $immutableFile = RESOURCE_PATH . DIRECTORY_SEPARATOR . 'immutable_file.test'; /** * @var string Archivo de prueba no existente */ protected static $missingFile = RESOURCE_PATH . DIRECTORY_SEPARATOR . 'missing_file.test'; /** * Comprobar la escritura de texto en un archivo * * @throws FileException */ public function testWrite() { $handler = new FileHandler(self::$validFile); $handler->write('valid_file'); $this->assertEquals('valid_file', $handler->readToString()); $handler->close(); $this->assertFileExists(self::$validFile); } /** * Comprobar si es posible escribir en el archivo * * @throws FileException */ public function testCheckIsWritable() { (new FileHandler(self::$validFile)) ->clearCache() ->checkIsWritable(); $this->assertTrue(true); } /** * Comprobar el tamaño del archivo * * @throws FileException */ public function testGetFileSize() { $size = (new FileHandler(self::$validFile))->getFileSize(); $this->assertEquals(10, $size); } /** * Comprobar un archivo válido * * @throws FileException */ public function testCheckFileExists() { $this->markTestSkipped(); (new FileHandler(self::$validFile)) ->clearCache() ->checkFileExists(); $this->assertTrue(true); } /** * Comprobar un archivo válido * * @throws FileException */ public function testCheckFileDoesNotExists() { $this->expectException(FileException::class); (new FileHandler(self::$missingFile)) ->clearCache() ->checkFileExists(); } /** * Abrir un archivo * * @throws FileException */ public function testOpenAndRead() { $handler = new FileHandler(self::$validFile); $handler->open('rb'); $this->assertEquals('valid_file', $handler->read()); $this->assertEquals('valid_file', $handler->readToString()); } /** * Comprobar a cerrar un archivo * * @throws FileException */ public function testClose() { $handler = new FileHandler(self::$validFile); $handler->open('rb'); $handler->close(); $this->expectException(FileException::class); $handler->close(); } /** * Comprobar si es posible leer el archivo * * @throws FileException */ public function testCheckIsReadable() { (new FileHandler(self::$validFile)) ->clearCache() ->checkIsReadable(); $this->assertTrue(true); } /** * Comprobar la eliminación de un archivo * * @throws FileException */ public function testDelete() { (new FileHandler(self::$validFile))->delete(); $this->assertFileDoesNotExist(self::$validFile); } }