[NEW] Introduced CopyFileException.

This commit is contained in:
Roman Syroeshko 2014-07-02 17:21:43 +04:00
parent 369f55a71f
commit 75c8e7e816
5 changed files with 89 additions and 7 deletions

View File

@ -44,7 +44,7 @@ This release added form fields (textinput, checkbox, and dropdown), drawing shap
- PclZip: Remove temporary file after used - @andrew-kzoo GH-265 - PclZip: Remove temporary file after used - @andrew-kzoo GH-265
- Autoloader: Add the ability to set the autoloader options - @bskrtich GH-267 - Autoloader: Add the ability to set the autoloader options - @bskrtich GH-267
- Element: Refactor elements to move set relation Id from container to element - @ivanlanin - Element: Refactor elements to move set relation Id from container to element - @ivanlanin
- Introduced CreateTemporaryFileException - @RomanSyroeshko - Introduced CreateTemporaryFileException, CopyFileException - @RomanSyroeshko
## 0.11.1 - 2 June 2014 ## 0.11.1 - 2 June 2014

View File

@ -0,0 +1,39 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Exception;
/**
* @since 0.12.0
*/
final class CopyFileException extends Exception
{
/**
* @param string $source The fully qualified source file name.
* @param string $destination The fully qualified destination file name.
* @param integer $code The user defined exception code.
* @param \Exception $previous The previous exception used for the exception chaining.
*/
final public function __construct($source, $destination, $code = 0, \Exception $previous = null)
{
parent::__construct(
sprintf('Could not copy \'%s\' file to \'%s\'.', $source, $destination),
$code,
$previous
);
}
}

View File

@ -17,6 +17,7 @@
namespace PhpOffice\PhpWord; namespace PhpOffice\PhpWord;
use PhpOffice\PhpWord\Exception\CopyFileException;
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException; use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Shared\String;
@ -58,11 +59,11 @@ class Template
/** /**
* Create a new Template Object * Create a new Template Object
* *
* @since 0.12.0 Throws CreateTemporaryFileException instead of Exception. * @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception.
* *
* @param string $strFilename * @param string $strFilename
* @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException * @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException
* @throws \PhpOffice\PhpWord\Exception\Exception * @throws \PhpOffice\PhpWord\Exception\CopyFileException
*/ */
public function __construct($strFilename) public function __construct($strFilename)
{ {
@ -72,8 +73,8 @@ class Template
} }
// Copy the source File to the temp File // Copy the source File to the temp File
if (!copy($strFilename, $this->tempFileName)) { if (false === copy($strFilename, $this->tempFileName)) {
throw new Exception("Could not copy the template from {$strFilename} to {$this->tempFileName}."); throw new CopyFileException($strFilename, $this->tempFileName);
} }
$this->zipClass = new ZipArchive(); $this->zipClass = new ZipArchive();

View File

@ -17,6 +17,7 @@
namespace PhpOffice\PhpWord\Writer; namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\Exception\CopyFileException;
use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\ZipArchive; use PhpOffice\PhpWord\Shared\ZipArchive;
@ -233,14 +234,16 @@ abstract class AbstractWriter implements WriterInterface
/** /**
* Cleanup temporary file * Cleanup temporary file
*
* @throws \PhpOffice\PhpWord\Exception\CopyFileException
*/ */
protected function cleanupTempFile() protected function cleanupTempFile()
{ {
if ($this->originalFilename != $this->tempFilename) { if ($this->originalFilename != $this->tempFilename) {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
// Can't find any test case. Uncomment when found. // Can't find any test case. Uncomment when found.
if (copy($this->tempFilename, $this->originalFilename) === false) { if (false === copy($this->tempFilename, $this->originalFilename)) {
throw new Exception("Could not copy temporary zip file."); throw new CopyFileException($this->tempFilename, $this->originalFilename);
} }
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
@unlink($this->tempFilename); @unlink($this->tempFilename);

View File

@ -0,0 +1,39 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Exception;
use PhpOffice\PhpWord\Exception\CopyFileException;
/**
* @covers \PhpOffice\PhpWord\Exception\CopyFileException
* @coversDefaultClass \PhpOffice\PhpWord\Exception\CopyFileException
*/
class CopyFileExceptionTest extends \PHPUnit_Framework_TestCase
{
/**
* CopyFileException can be thrown.
*
* @covers ::__construct()
* @expectedException \PhpOffice\PhpWord\Exception\CopyFileException
* @test
*/
public function testCopyFileExceptionCanBeThrown()
{
throw new CopyFileException('C:\source\dummy.txt', 'C:\destination\dummy.txt');
}
}