Add ImportController Tests and fix bugs

modifié:         core/lib/Thelia/Controller/Admin/ImportController.php
	modifié:         core/lib/Thelia/Exception/FileNotFoundException.php
	modifié:         core/lib/Thelia/Tests/Controller/ImportControllerTest.php
	modifié:         core/lib/Thelia/Tests/Controller/ImportExportControllerTest.php
This commit is contained in:
Benjamin Perche
2014-07-21 10:36:40 +02:00
parent ae750866aa
commit 14cf745a89
4 changed files with 201 additions and 12 deletions

View File

@@ -24,6 +24,7 @@ use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Core\Template\Element\LoopResult;
use Thelia\Core\Template\Loop\Import as ImportLoop;
use Thelia\Exception\FileNotFoundException;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Form\ImportForm;
use Thelia\ImportExport\Import\ImportHandler;
@@ -160,7 +161,7 @@ class ImportController extends BaseAdminController
return $this->importView($id);
}
protected function getFileContentInArchive(
public function getFileContentInArchive(
AbstractArchiveBuilder $archiveBuilder,
FormatterManager $formatterManager,
array $types
@@ -184,7 +185,7 @@ class ImportController extends BaseAdminController
}
if ($content === null) {
throw new \ErrorException(
throw new FileNotFoundException(
$this->getTranslator()->trans(
"Your archive must contain one of these file and doesn't: %files",
[
@@ -252,14 +253,14 @@ class ImportController extends BaseAdminController
public function checkFileExtension($fileName, $uploadFormat)
{
$splitName = explode(".", $fileName);
$ext = "";
if (1 < $limit = count($splitName)) {
$ext = "." . $splitName[$limit-1];
}
if ($uploadFormat === null) {
$splitName = explode(".", $fileName);
$ext = "";
if (1 < $limit = count($splitName)) {
$ext = "." . $splitName[$limit-1];
}
throw new FormValidationException(
$this->getTranslator()->trans(
"The extension \"%ext\" is not allowed",

View File

@@ -17,7 +17,7 @@ namespace Thelia\Exception;
* @package Thelia\Exception
* @author Benjamin Perche <bperche@openstudio.fr>
*/
class FileNotFoundException extends \Exception
class FileNotFoundException extends \ErrorException
{
}

View File

@@ -12,6 +12,11 @@
namespace Thelia\Tests\Controller;
use Thelia\Controller\Admin\ImportController;
use Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveBuilder;
use Thelia\Core\FileFormat\Archive\ArchiveBuilderManagerTrait;
use Thelia\Core\FileFormat\Formatting\Formatter\XMLFormatter;
use Thelia\Core\FileFormat\Formatting\FormatterManagerTrait;
use Thelia\Core\FileFormat\FormatType;
/**
* Class ImportControllerTest
@@ -20,6 +25,9 @@ use Thelia\Controller\Admin\ImportController;
*/
class ImportControllerTest extends ImportExportControllerTest
{
use FormatterManagerTrait;
use ArchiveBuilderManagerTrait;
/**
* @return \Thelia\Controller\BaseController The controller you want to test
*/
@@ -27,6 +35,186 @@ class ImportControllerTest extends ImportExportControllerTest
{
return new ImportController();
}
public function testCheckFileExtension()
{
$this->controller->checkFileExtension("a.zip", "zip");
}
/**
* @expectedException \Thelia\Form\Exception\FormValidationException
* @expectedExceptionMessage The extension ".zip" is not allowed
*/
public function testCheckFileExtensionFail()
{
$this->controller->checkFileExtension("a.zip", null);
}
/**
* @expectedException \Thelia\Form\Exception\FormValidationException
* @expectedExceptionMessage The extension ".bz2" is not allowed
*/
public function testCheckFileExtensionFailMultipleExt()
{
$this->controller->checkFileExtension("a.tar.bz2", null);
}
/**
* @expectedException \Thelia\Form\Exception\FormValidationException
* @expectedExceptionMessage The extension "" is not allowed
*/
public function testCheckFileExtensionFailNoExt()
{
$this->controller->checkFileExtension("file", null);
}
public function testGetFileContentInArchive()
{
/** @var ZipArchiveBuilder $archive */
$archive = $this->getArchiveBuilderManager($this->container)->get("ZIP");
$formatter = new XMLFormatter();
$archive->addFileFromString("foo", $formatter::FILENAME . "." . $formatter->getExtension());
$content = $this->controller->getFileContentInArchive(
$archive,
$this->getFormatterManager($this->container),
[$formatter->getHandledType()]
);
$this->assertEquals("foo", $content);
}
/**
* @expectedException \Thelia\Exception\FileNotFoundException
* @expectedExceptionMessage Your archive must contain one of these file and doesn't:
*/
public function testGetFileContentInArchiveFail()
{
/** @var ZipArchiveBuilder $archive */
$archive = $this->getArchiveBuilderManager($this->container)->get("ZIP");
$formatter = new XMLFormatter();
$archive->addFileFromString("foo", "bar");
$this->controller->getFileContentInArchive(
$archive,
$this->getFormatterManager($this->container),
[$formatter->getHandledType()]
);
}
public function testRetrieveFormatTools()
{
$handler = $this
->getMock(
"\\Thelia\\ImportExport\\Import\\ImportHandler",
[],
[
$this->container
]
)
;
$handler
->expects($this->any())
->method("getHandledTypes")
->willReturn(FormatType::UNBOUNDED)
;
$tools = $this->controller->retrieveFormatTools(
"foo.xml",
$handler,
$this->getFormatterManager($this->container),
$this->getArchiveBuilderManager($this->container)
);
$this->assertArrayHasKey("formatter", $tools);
$this->assertInstanceOf(
"Thelia\\Core\\FileFormat\\Formatting\\AbstractFormatter",
$tools["formatter"]
);
$this->assertArrayHasKey("archive_builder", $tools);
$this->assertNull($tools["archive_builder"]);
$this->assertArrayHasKey("extension", $tools);
$this->assertEquals(".xml", $tools["extension"]);
$this->assertArrayHasKey("types", $tools);
$this->assertEquals(
FormatType::UNBOUNDED,
$tools["types"]
);
$handler = $this
->getMock(
"\\Thelia\\ImportExport\\Import\\ImportHandler",
[],
[
$this->container
]
)
;
$handler
->expects($this->any())
->method("getHandledTypes")
->willReturn([FormatType::UNBOUNDED])
;
$tools = $this->controller->retrieveFormatTools(
"foo.zip",
$handler,
$this->getFormatterManager($this->container),
$this->getArchiveBuilderManager($this->container)
);
$this->assertArrayHasKey("formatter", $tools);
$this->assertNull($tools["formatter"]);
$this->assertArrayHasKey("archive_builder", $tools);
$this->assertInstanceOf(
"Thelia\\Core\\FileFormat\\Archive\\AbstractArchiveBuilder",
$tools["archive_builder"]
);
$this->assertArrayHasKey("extension", $tools);
$this->assertEquals(".zip", $tools["extension"]);
$this->assertArrayHasKey("types", $tools);
$this->assertEquals(
[FormatType::UNBOUNDED],
$tools["types"]
);
}
/**
* @expectedException \Thelia\Form\Exception\FormValidationException
*/
public function testRetrieveFormatToolsFail()
{
$handler = $this
->getMock(
"\\Thelia\\ImportExport\\Import\\ImportHandler",
[],
[
$this->container
]
)
;
$handler
->expects($this->any())
->method("getHandledTypes")
->willReturn(FormatType::UNBOUNDED)
;
$this->controller->retrieveFormatTools(
"foo.csv",
$handler,
$this->getFormatterManager($this->container),
$this->getArchiveBuilderManager($this->container)
);
}
}

View File

@@ -49,7 +49,7 @@ abstract class ImportExportControllerTest extends ControllerTestBase
->add(new CSVFormatter())
;
$container->set("thelia.manager.formatter_manager", $archiveBuilderManager);
$container->set("thelia.manager.formatter_manager", $formatterManager);
}
}