Skip to content

Commit

Permalink
Move output check to a specific method
Browse files Browse the repository at this point in the history
  • Loading branch information
Herzult committed Jul 11, 2011
1 parent 1f5743a commit c20537e
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 29 deletions.
43 changes: 29 additions & 14 deletions src/Knp/Snappy/Media.php
Expand Up @@ -117,22 +117,10 @@ public function generate($input, $output, array $options = array(), $overwrite =
$this->prepareOutput($output, $overwrite);

$command = $this->getCommand($input, $output, $options);
$this->executeCommand($command);

// todo manage the conversion error output. Currently, we simply do a
// small diagnostic of the file after the conversion

if (!$this->fileExists($output)) {
throw new \RuntimeException(sprintf(
'The file \'%s\' was not created. Command: %s', $output, $command
));
}
$this->executeCommand($command);

if (0 === $this->filesize($output)) {
throw new \RuntimeException(sprintf(
'The file \'%s\' was created but is empty. Command: %s', $output, $command
));
}
$this->checkOutput($output, $command);
}

/**
Expand Down Expand Up @@ -273,6 +261,33 @@ protected function mergeOptions(array $options)
return $mergedOptions;
}

/**
* Checks the specified output
*
* @param string $output The output filename
* @param string $command The generation command
*
* @throws RuntimeException if the output file generation failed
*/
protected function checkOutput($output, $command)
{
// the output file must exist
if (!$this->fileExists($output)) {
throw new \RuntimeException(sprintf(
'The file \'%s\' was not created (command: %s).',
$output, $command
));
}

// the output file must not be empty
if (0 === $this->filesize($output)) {
throw new \RuntimeException(sprintf(
'The file \'%s\' was created but is empty (command: %s).',
$output, $command
));
}
}

/**
* Creates a temporary file.
* The file is not created if the $content argument is null
Expand Down
133 changes: 118 additions & 15 deletions test/Knp/Snappy/MediaTest.php
Expand Up @@ -139,10 +139,9 @@ public function testGenerate()
array(
'configure',
'prepareOutput',
'fileExists',
'fileSize',
'getCommand',
'executeCommand'
'executeCommand',
'checkOutput'
),
array(
'the_binary',
Expand All @@ -154,18 +153,6 @@ public function testGenerate()
->method('prepareOutput')
->with($this->equalTo('the_output_file'))
;
$media
->expects($this->once())
->method('fileExists')
->with($this->equalTo('the_output_file'))
->will($this->returnValue(true))
;
$media
->expects($this->once())
->method('fileSize')
->with($this->equalTo('the_output_file'))
->will($this->returnValue(true))
;
$media
->expects($this->any())
->method('getCommand')
Expand All @@ -181,6 +168,14 @@ public function testGenerate()
->method('executeCommand')
->with($this->equalTo('the command'))
;
$media
->expects($this->once())
->method('checkOutput')
->with(
$this->equalTo('the_output_file'),
$this->equalTo('the command')
)
;

$media->generate('the_input_file', 'the_output_file', array('foo' => 'bar'));
}
Expand Down Expand Up @@ -423,4 +418,112 @@ public function dataForBuildCommand()
),
);
}

public function testCheckOutput()
{
$media = $this->getMock(
'Knp\Snappy\Media',
array(
'configure',
'fileExists',
'filesize'
),
array(),
'',
false
);
$media
->expects($this->once())
->method('fileExists')
->with($this->equalTo('the_output_file'))
->will($this->returnValue(true))
;
$media
->expects($this->once())
->method('filesize')
->with($this->equalTo('the_output_file'))
->will($this->returnValue(123))
;

$r = new \ReflectionMethod($media, 'checkOutput');
$r->setAccessible(true);

$message = '->checkOutput() checks both file existence and size';
try {
$r->invokeArgs($media, array('the_output_file', 'the command'));
$this->anything($message);
} catch (\RuntimeException $e) {
$this->fail($message);
}
}

public function testCheckOutputWhenTheFileDoesNotExist()
{
$media = $this->getMock(
'Knp\Snappy\Media',
array(
'configure',
'fileExists',
'filesize'
),
array(),
'',
false
);
$media
->expects($this->once())
->method('fileExists')
->with($this->equalTo('the_output_file'))
->will($this->returnValue(false))
;

$r = new \ReflectionMethod($media, 'checkOutput');
$r->setAccessible(true);

$message = '->checkOutput() throws an InvalidArgumentException when the file does not exist';
try {
$r->invokeArgs($media, array('the_output_file', 'the command'));
$this->fail($message);
} catch (\RuntimeException $e) {
$this->anything($message);
}
}

public function testCheckOutputWhenTheFileIsEmpty()
{
$media = $this->getMock(
'Knp\Snappy\Media',
array(
'configure',
'fileExists',
'filesize'
),
array(),
'',
false
);
$media
->expects($this->once())
->method('fileExists')
->with($this->equalTo('the_output_file'))
->will($this->returnValue(true))
;
$media
->expects($this->once())
->method('filesize')
->with($this->equalTo('the_output_file'))
->will($this->returnValue(0))
;

$r = new \ReflectionMethod($media, 'checkOutput');
$r->setAccessible(true);

$message = '->checkOutput() throws an InvalidArgumentException when the file is empty';
try {
$r->invokeArgs($media, array('the_output_file', 'the command'));
$this->fail($message);
} catch (\RuntimeException $e) {
$this->anything($message);
}
}
}

0 comments on commit c20537e

Please sign in to comment.