Skip to content

Commit

Permalink
[format guesser] Fixed format guesser for multiple extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
raulfraile committed Aug 31, 2014
1 parent 758a534 commit cb4e1bf
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Distill/Format/FormatGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ class FormatGuesser implements FormatGuesserInterface
*/
public function guess($path)
{
$extension = pathinfo($path, PATHINFO_EXTENSION);
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
$filename = pathinfo($path, PATHINFO_FILENAME);

if (in_array($extension, array('bz', 'bz2', 'gz', 'xz'))) {
$subextension = pathinfo($filename, PATHINFO_EXTENSION);

if ("" !== $subextension) {
$extension = sprintf('%s.%s', $subextension, strtolower($extension));
}
}

if (!array_key_exists($extension, $this->extensionMap)) {
throw new ExtensionNotSupportedException($extension);
Expand Down
88 changes: 88 additions & 0 deletions tests/Distill/Tests/Format/FormatGuesserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Distill\Tests\Format;

use Distill\File;
use Distill\Format\Zip;
use Distill\Format\FormatGuesser;

class FormatGuesserTest extends \PHPUnit_Framework_TestCase
{

/** @var FormatGuesser $formatGuesser */
protected $formatGuesser;

protected $filesPath;

public function setUp()
{
$this->formatGuesser = new FormatGuesser();
$this->filesPath = __DIR__ . '/../../../../files/';
}

protected function guessFormat($file, $formatClass)
{
$format = $this->formatGuesser->guess($file);

$this->assertInstanceOf($formatClass, $format);
}

public function testBzip2FileGuesser()
{
$this->guessFormat($this->filesPath . 'file_ok.bz2', '\\Distill\\Format\\Bz2');
$this->guessFormat($this->filesPath . 'file_ok.bz', '\\Distill\\Format\\Bz2');
$this->guessFormat($this->filesPath . 'file_ok.BZ2', '\\Distill\\Format\\Bz2');
}

public function testGzipFileGuesser()
{
$this->guessFormat($this->filesPath . 'file_ok.gz', '\\Distill\\Format\\Gz');
}

public function testPharFileGuesser()
{
$this->guessFormat($this->filesPath . 'file_ok.phar', '\\Distill\\Format\\Phar');
}

public function testRarFileGuesser()
{
$this->guessFormat($this->filesPath . 'file_ok.rar', '\\Distill\\Format\\Rar');
}

public function testTarFileGuesser()
{
$this->guessFormat($this->filesPath . 'file_ok.tar', '\\Distill\\Format\\Tar');
}

public function testTarBz2FileGuesser()
{
$this->guessFormat($this->filesPath . 'file_ok.tar.bz2', '\\Distill\\Format\\TarBz2');
}

public function testTarGzFileGuesser()
{
$this->guessFormat('test.tar.gz', '\\Distill\\Format\\TarGz');
$this->guessFormat('test.tgz', '\\Distill\\Format\\TarGz');
}

public function testTarXzFileGuesser()
{
$this->guessFormat($this->filesPath . 'file_ok.tar.xz', '\\Distill\\Format\\TarXz');
}

public function testTar7zFileGuesser()
{
$this->guessFormat($this->filesPath . 'file_ok.7z', '\\Distill\\Format\\X7z');
}

public function testXzFileGuesser()
{
$this->guessFormat('test.xz', '\\Distill\\Format\\Xz');
}

public function testZipFileGuesser()
{
$this->guessFormat($this->filesPath . 'file_ok.zip', '\\Distill\\Format\\Zip');
}

}

0 comments on commit cb4e1bf

Please sign in to comment.