. */ namespace Tests; use PHPUnit\Framework\TestCase; use SP\Storage\FileException; use SP\Storage\FileHandler; /** * Class FileHandlerTest * * Tests unitarios para comprobar el funcionamiento de la clase SP\Storage\FileHandler */ class FileHandlerTest extends TestCase { /** * @var string Archvivo de prueba válido */ protected $validFile; /** * @var string Archvivo de prueba inválido */ protected $invalidFile; /** * Comprobar la escritura de texto en un archivo * * @doesNotPerformAssertions * @throws FileException */ public function testWrite() { $handler = new FileHandler($this->validFile); $handler->write('valid_file'); $this->assertEquals('valid_file', $handler->readString()); $handler->close(); } /** * Comprobar si es posible escribir en el archivo * * @depends testWrite * @doesNotPerformAssertions * @throws FileException */ public function testCheckIsWritable() { (new FileHandler($this->validFile)) ->clearCache() ->checkIsWritable(); $this->expectException(FileException::class); (new FileHandler($this->invalidFile)) ->clearCache() ->checkIsWritable(); } /** * Comprobar el tamaño del archivo * * @depends testWrite * @throws FileException */ public function testGetFileSize() { $size = (new FileHandler($this->validFile))->getFileSize(); $this->assertEquals(10, $size); } /** * Comprobar un archivo válido * * @depends testWrite * @doesNotPerformAssertions * @throws FileException */ public function testCheckFileExists() { (new FileHandler($this->validFile)) ->clearCache() ->checkFileExists(); $this->expectException(FileException::class); (new FileHandler($this->invalidFile)) ->clearCache() ->checkFileExists(); } /** * Abrir un archivo * * @depends testWrite * @throws FileException */ public function testOpenAndRead() { $handler = new FileHandler($this->validFile); $handler->open('rb'); $this->assertEquals('valid_file', $handler->read()); $this->assertEquals('valid_file', $handler->readString()); } /** * Comprobar a cerrar un archivo * * @depends testWrite * @throws FileException */ public function testClose() { $handler = new FileHandler($this->validFile); $handler->open('rb'); $handler->close(); $this->expectException(FileException::class); $handler->close(); } /** * Comprobar si es posible leer el archivo * * @depends testWrite * @doesNotPerformAssertions * @throws FileException */ public function testCheckIsReadable() { (new FileHandler($this->validFile)) ->clearCache() ->checkIsReadable(); $this->expectException(FileException::class); (new FileHandler($this->invalidFile)) ->clearCache() ->checkIsReadable(); } /** * Comprobar la eliminación de un archivo * * @depends testWrite * @doesNotPerformAssertions * @throws FileException */ public function testDelete() { (new FileHandler($this->validFile))->delete(); $this->expectException(FileException::class); (new FileHandler($this->invalidFile))->delete(); } protected function setUp() { $this->validFile = TEST_ROOT . DIRECTORY_SEPARATOR . 'res' . DIRECTORY_SEPARATOR . 'valid_file.test'; $this->invalidFile = TEST_ROOT . DIRECTORY_SEPARATOR . 'res' . DIRECTORY_SEPARATOR . 'invalid_file.test'; } }