1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-20 06:00:08 -07:00

Add LoaderBundle tests

This commit is contained in:
Unknown 2017-06-23 22:21:09 +02:00
parent edcb7fa98d
commit 9089929e66
38 changed files with 1742 additions and 214 deletions

View File

@ -0,0 +1,7 @@
<?php
namespace LoaderBundle\Exception\Loader;
class LoadingRebrickableFailedException extends \Exception
{
}

View File

@ -0,0 +1,7 @@
<?php
namespace LoaderBundle\Exception\RelationMapper;
class InvalidDomainException extends \LogicException
{
}

View File

@ -5,11 +5,9 @@ namespace LoaderBundle\Service;
use AppBundle\Transformer\FormatTransformer; use AppBundle\Transformer\FormatTransformer;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use LoaderBundle\Exception\FileNotFoundException;
use LoaderBundle\Exception\WriteErrorException; use LoaderBundle\Exception\WriteErrorException;
use Monolog\Logger; use Monolog\Logger;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\Asset\Exception\LogicException;
use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@ -101,27 +99,21 @@ abstract class BaseLoader
* *
* @param $url * @param $url
* *
* @throws FileNotFoundException * @throws WriteErrorException
* *
* @return bool|string * @return bool|string
*/ */
protected function downloadFile($url) protected function downloadFile($url)
{ {
$this->output->writeln('Loading file from: <comment>'.$url.'</comment>'); $this->writeOutput(['Loading file from: <comment>'.$url.'</comment>']);
$temp = tempnam(sys_get_temp_dir(), 'printabrick.'); $temp = tempnam(sys_get_temp_dir(), 'printabrick.');
$ctx = stream_context_create([], [ $ctx = stream_context_create([], [
'notification' => [$this, 'progressCallback'], 'notification' => [$this, 'progressCallback'],
]); ]);
try { if (false === file_put_contents($temp, fopen($url, 'r', 0, $ctx))) {
if (false === file_put_contents($temp, fopen($url, 'r', 0, $ctx))) { throw new WriteErrorException($temp);
throw new WriteErrorException($temp);
}
} catch (\ErrorException $e) {
throw new FileNotFoundException($url);
} catch (\Exception $e) {
throw new LogicException($e);
} }
return $temp; return $temp;

View File

@ -5,6 +5,7 @@ namespace LoaderBundle\Service;
use AppBundle\Entity\LDraw\Model; use AppBundle\Entity\LDraw\Model;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use League\Flysystem\FilesystemInterface; use League\Flysystem\FilesystemInterface;
use LoaderBundle\Exception\FileException;
use LoaderBundle\Service\Stl\StlRendererService; use LoaderBundle\Service\Stl\StlRendererService;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -32,6 +33,8 @@ class ImageLoader extends BaseLoader
* Download ZIP file with part images from rebrickable and unzip file to filesystem. * Download ZIP file with part images from rebrickable and unzip file to filesystem.
* *
* @param int $color color id used by rebrickable * @param int $color color id used by rebrickable
*
* @throws FileException
*/ */
public function loadColorFromRebrickable($color) public function loadColorFromRebrickable($color)
{ {
@ -41,14 +44,15 @@ class ImageLoader extends BaseLoader
$zip = new \ZipArchive($file); $zip = new \ZipArchive($file);
if ($zip->open($file) === true) { if ($zip->open($file) === true) {
$this->output->writeln([ $this->writeOutput([
"Extracting ZIP file into {$this->mediaFilesystem->getAdapter()->getPathPrefix()}images/{$color}", "Extracting ZIP file into {$this->mediaFilesystem->getAdapter()->getPathPrefix()}images/{$color}",
]); ]);
$zip->extractTo($this->mediaFilesystem->getAdapter()->getPathPrefix().'images'.DIRECTORY_SEPARATOR.$color); $zip->extractTo($this->mediaFilesystem->getAdapter()->getPathPrefix().'images'.DIRECTORY_SEPARATOR.$color);
$zip->close(); $zip->close();
$this->output->writeln(['Done!']); $this->writeOutput(['Done!']);
} else { } else {
$this->logger->error('Extraction of file failed!'); $this->logger->error('Extraction of file failed!');
throw new FileException($file);
} }
} }
@ -69,7 +73,7 @@ class ImageLoader extends BaseLoader
unset($models); unset($models);
// Render images // Render images
$this->output->writeln([ $this->writeOutput([
'Rendering missing images of models', 'Rendering missing images of models',
]); ]);
$this->initProgressBar(count($missing)); $this->initProgressBar(count($missing));
@ -79,7 +83,7 @@ class ImageLoader extends BaseLoader
try { try {
$this->loadModelImage($this->mediaFilesystem->getAdapter()->getPathPrefix().$model->getPath()); $this->loadModelImage($this->mediaFilesystem->getAdapter()->getPathPrefix().$model->getPath());
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error('Error rendering model '.$model->getId().' image', $e); $this->logger->error('Error rendering model '.$model->getId().' image');
} }
$this->progressBar->advance(); $this->progressBar->advance();
} }

View File

@ -102,27 +102,29 @@ class ModelLoader extends BaseLoader
'<fg=cyan>------------------------------------------------------------------------------</>', '<fg=cyan>------------------------------------------------------------------------------</>',
]); ]);
$libraryZip = $this->downloadFile($url); try {
$libraryZip = $this->downloadFile($url);
$temp_dir = tempnam(sys_get_temp_dir(), 'printabrick.'); $temp_dir = tempnam(sys_get_temp_dir(), 'printabrick.');
if (file_exists($temp_dir)) { if (file_exists($temp_dir)) {
unlink($temp_dir); unlink($temp_dir);
} }
mkdir($temp_dir); mkdir($temp_dir);
$zip = new \ZipArchive(); $zip = new \ZipArchive();
if ($zip->open($libraryZip) != 'true') { $zip->open($libraryZip);
echo 'Error :- Unable to open the Zip File'; $zip->extractTo($temp_dir);
} $zip->close();
$zip->extractTo($temp_dir); unlink($libraryZip);
$zip->close();
unlink($libraryZip);
$this->writeOutput(['<info>LDraw libary downloaded</info>']); $this->writeOutput(['<info>LDraw libary downloaded</info>']);
// return ldraw directory if in zip file // return ldraw directory if in zip file
if (file_exists($temp_dir.'/ldraw/')) { if (file_exists($temp_dir.'/ldraw/')) {
return $temp_dir.'/ldraw/'; return $temp_dir.'/ldraw/';
}
} catch (\Exception $exception) {
throw new \LogicException('Falied to open Zip archive');
} }
return $temp_dir; return $temp_dir;

View File

@ -6,6 +6,7 @@ use AppBundle\Entity\Rebrickable\Part;
use AppBundle\Entity\Rebrickable\Set; use AppBundle\Entity\Rebrickable\Set;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Expr\Join; use Doctrine\ORM\Query\Expr\Join;
use LoaderBundle\Exception\Loader\LoadingRebrickableFailedException;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
//TODO Refactor + validate csv files //TODO Refactor + validate csv files
@ -30,9 +31,9 @@ class RebrickableLoader extends BaseLoader
} }
/** /**
* Truncates and loads all rebrickable tables from csv files * Truncates and loads all rebrickable tables from csv files.
* *
* @throws \Exception * @throws LoadingRebrickableFailedException
*/ */
public function loadAll() public function loadAll()
{ {
@ -67,19 +68,19 @@ class RebrickableLoader extends BaseLoader
$this->writeOutput(['Rebrickable database loaded successfully!']); $this->writeOutput(['Rebrickable database loaded successfully!']);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->writeOutput(['Rollback back']); // $this->writeOutput(['Rollback back']);
$connection->rollBack(); // $connection->rollBack();
throw $e;
throw new LoadingRebrickableFailedException();
} }
} }
/** /**
* Downloads csv files from rebrickable_url specified in config.yml * Downloads csv files from rebrickable_url specified in config.yml.
*/ */
private function loadCSVFiles() private function loadCSVFiles()
{ {
$array = ['inventories', 'inventory_parts', 'inventory_sets', 'sets', 'themes', 'parts', 'part_categories', 'colors']; $array = ['inventories', 'inventory_parts', 'inventory_sets', 'sets', 'themes', 'parts', 'part_categories'];
$this->writeOutput([ $this->writeOutput([
'<fg=cyan>------------------------------------------------------------------------------</>', '<fg=cyan>------------------------------------------------------------------------------</>',
@ -133,6 +134,9 @@ class RebrickableLoader extends BaseLoader
return $this->em->getConnection()->prepare($query)->execute(); return $this->em->getConnection()->prepare($query)->execute();
} }
/**
* Creates missing Part entites for foreign keys form inventory_parts table.
*/
private function addMissingParts() private function addMissingParts()
{ {
$connection = $this->em->getConnection(); $connection = $this->em->getConnection();
@ -185,8 +189,8 @@ class RebrickableLoader extends BaseLoader
return $this->loadCsvFile($csv, 'rebrickable_category', '(`id`,`name`)'); return $this->loadCsvFile($csv, 'rebrickable_category', '(`id`,`name`)');
} }
private function loadColorTable($csv) // private function loadColorTable($csv)
{ // {
return $this->loadCsvFile($csv, 'color', '(`id`,`name`,`rgb`, @var) SET transparent = IF(@var=\'t\',1,0)'); // return $this->loadCsvFile($csv, 'color', '(`id`,`name`,`rgb`, @var) SET transparent = IF(@var=\'t\',1,0)');
} // }
} }

View File

@ -93,65 +93,9 @@ class StlConverterService
return $this->mediaFilesystem->get($newFile); return $this->mediaFilesystem->get($newFile);
} }
} else {
return $this->mediaFilesystem->get($newFile);
} }
throw new ConvertingFailedException($file, 'STL'); return $this->mediaFilesystem->get($newFile);
}
/**
* Convert LDraw model from .dat format to .stl by using LDView
* stores created file to $stlStorage filesystem.
*
* @param string $file
* @param bool $rewrite
*
* @throws ConvertingFailedException
*
* @return File
*/
public function datToPng($file, $rewrite = false)
{
if (!$this->ldrawLibraryContext) {
throw new LDLibraryMissingException();
}
if (!$this->mediaFilesystem->has('images')) {
$this->mediaFilesystem->createDir('images');
}
$newFile = 'images'.DIRECTORY_SEPARATOR.basename($file, '.dat').'.png';
if (!$this->mediaFilesystem->has($newFile) || $rewrite) {
$this->runLDView([
$file,
'-LDrawDir='.$this->ldrawLibraryContext->getAdapter()->getPathPrefix(),
'-AutoCrop=0',
'-SaveAlpha=0',
'-BackgroundColor3=0xFFFFFF',
'-DefaultColor3=0x136FC3',
'-SnapshotSuffix=.png',
'-HiResPrimitives=1',
'-UseQualityStuds=1',
'-UseQualityLighting=1',
'-SaveHeight=600',
'-SaveWidth=800',
'-CurveQuality=12',
'-DefaultLatLong=45,40',
'-SaveDir='.$this->mediaFilesystem->getAdapter()->getPathPrefix().'images',
'-SaveSnapshots=1',
]);
// Check if file created successfully
if ($this->mediaFilesystem->has($newFile)) {
return $this->mediaFilesystem->get($newFile);
}
} else {
return $this->mediaFilesystem->get($newFile);
}
throw new ConvertingFailedException($file, 'PNG');
} }
/** /**

View File

@ -5,6 +5,7 @@ namespace LoaderBundle\Service\Stl;
use LoaderBundle\Exception\ConvertingFailedException; use LoaderBundle\Exception\ConvertingFailedException;
use LoaderBundle\Exception\FileNotFoundException; use LoaderBundle\Exception\FileNotFoundException;
use LoaderBundle\Exception\RenderFailedException; use LoaderBundle\Exception\RenderFailedException;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder; use Symfony\Component\Process\ProcessBuilder;
@ -101,9 +102,6 @@ class StlRendererService
if (!file_exists($file)) { if (!file_exists($file)) {
throw new FileNotFoundException($file); throw new FileNotFoundException($file);
} }
if (pathinfo($file, PATHINFO_EXTENSION) != 'stl') {
throw new ConvertingFailedException($file, 'POV', 'Wrong input filetype');
}
// Save the current working directory and change directory to tmp dir // Save the current working directory and change directory to tmp dir
// stl2pov outputs converted file to current directory and destination can not be changed // stl2pov outputs converted file to current directory and destination can not be changed
@ -195,7 +193,8 @@ class StlRendererService
//+AMn - use non-adaptive (n=1) or adaptive (n=2) supersampling //+AMn - use non-adaptive (n=1) or adaptive (n=2) supersampling
//+A0.n - perform antialiasing (if color change is above n percent) //+A0.n - perform antialiasing (if color change is above n percent)
//-D - Turns graphic display off //-D - Turns graphic display off
$process = $processBuilder->setPrefix($this->povray) $process = $processBuilder
->setPrefix($this->povray)
->setArguments([ ->setArguments([
"+I\"{$file}\"", "+I\"{$file}\"",
'+FN', '+FN',
@ -207,8 +206,13 @@ class StlRendererService
'+A0.5', '+A0.5',
'-D', '-D',
])->getProcess(); ])->getProcess();
$process->mustRun(); $process->mustRun();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
if (!file_exists($outputFile)) { if (!file_exists($outputFile)) {
throw new RenderFailedException("{$to}{$filename}.png"); throw new RenderFailedException("{$to}{$filename}.png");
} }

View File

@ -3,10 +3,10 @@
namespace LoaderBundle\Util; namespace LoaderBundle\Util;
use Doctrine\Common\Cache\CacheProvider; use Doctrine\Common\Cache\CacheProvider;
use LoaderBundle\Exception\RelationMapper\InvalidDomainException;
use LoaderBundle\Exception\RelationMapper\InvalidResourceException; use LoaderBundle\Exception\RelationMapper\InvalidResourceException;
use LoaderBundle\Exception\RelationMapper\ResourceNotFoundException; use LoaderBundle\Exception\RelationMapper\ResourceNotFoundException;
use Symfony\Component\OptionsResolver\Exception\InvalidArgumentException; use Symfony\Component\OptionsResolver\Exception\InvalidArgumentException;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
@ -69,6 +69,6 @@ class RelationMapper
if (isset($this->relations[$domain])) { if (isset($this->relations[$domain])) {
return isset($this->relations[$domain][$number]) ? $this->relations[$domain][$number] : $number; return isset($this->relations[$domain][$number]) ? $this->relations[$domain][$number] : $number;
} }
throw new InvalidOptionsException(); throw new InvalidDomainException();
} }
} }

View File

@ -0,0 +1,62 @@
<?php
namespace Tests\LoaderBundle\Service;
use League\Flysystem\File;
use LoaderBundle\Service\ImageLoader;
use LoaderBundle\Service\Stl\StlRendererService;
use Monolog\Logger;
use Symfony\Component\Console\Output\NullOutput;
use Tests\AppBundle\BaseTest;
use Tests\AppBundle\Fixtures\LoadBaseData;
class ImageLoaderTest extends BaseTest
{
/**
* @var ImageLoader
*/
private $imageLoader;
public function setUp()
{
parent::setUp();
$this->setUpDb([LoadBaseData::class]);
$file = $this->createMock(File::class);
$file->method('getPath')->willReturn('path');
$stlRenderer = $this->createMock(StlRendererService::class);
$stlRenderer->method('render')->willReturn('image');
$this->imageLoader = new ImageLoader($this->em, $this->get('monolog.logger.event'), $this->filesystem, __DIR__.'/fixtures/', $stlRenderer);
$this->imageLoader->setOutput(new NullOutput());
}
public function testLoadFromRebrickable()
{
$this->imageLoader->loadColorFromRebrickable(-1);
$this->assertCount(8, $this->filesystem->listContents('images/-1/'));
}
/**
* @expectedException \LoaderBundle\Exception\FileException
*/
public function testLoadCorrupted()
{
$this->imageLoader->loadColorFromRebrickable(5);
}
public function testLoadMissingImages()
{
$stlRenderer = $this->createMock(StlRendererService::class);
$stlRenderer->method('render')->willReturn('image');
$stlRenderer->expects($this->exactly(3))->method('render');
$this->imageLoader = new ImageLoader($this->em, $this->get('monolog.logger.event'), $this->filesystem, __DIR__.'/fixtures/', $stlRenderer);
$this->imageLoader->setOutput(new NullOutput());
$this->imageLoader->loadMissingModelImages();
}
}

View File

@ -1,16 +1,16 @@
<?php <?php
namespace Tests\LoaderBundle\Service\ModelLoader; namespace Tests\LoaderBundle\Service;
use AppBundle\DataFixtures\ORM\LoadColors;
use AppBundle\Entity\LDraw\Alias; use AppBundle\Entity\LDraw\Alias;
use AppBundle\Entity\LDraw\Model; use AppBundle\Entity\LDraw\Model;
use AppBundle\Repository\LDraw\AliasRepository; use AppBundle\Repository\LDraw\AliasRepository;
use AppBundle\Repository\LDraw\ModelRepository; use AppBundle\Repository\LDraw\ModelRepository;
use League\Flysystem\File;
use LoaderBundle\Service\ModelLoader; use LoaderBundle\Service\ModelLoader;
use LoaderBundle\Service\Stl\StlConverterService; use LoaderBundle\Service\Stl\StlConverterService;
use LoaderBundle\Util\RelationMapper; use LoaderBundle\Util\RelationMapper;
use League\Flysystem\File;
use League\Flysystem\Filesystem;
use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Output\NullOutput;
use Tests\AppBundle\BaseTest; use Tests\AppBundle\BaseTest;
@ -25,7 +25,7 @@ class ModelLoaderTest extends BaseTest
* @var ModelRepository * @var ModelRepository
*/ */
private $modelRepository; private $modelRepository;
/** @var AliasRepository */ /** @var AliasRepository */
private $aliasRepository; private $aliasRepository;
@ -47,15 +47,15 @@ class ModelLoaderTest extends BaseTest
$relationMapper->method('find') $relationMapper->method('find')
->will($this->returnArgument(0)); ->will($this->returnArgument(0));
$this->modelLoader = new ModelLoader($this->get('doctrine.orm.entity_manager'),$this->get('monolog.logger.event'),$stlConverter,$relationMapper,null); $this->modelLoader = new ModelLoader($this->em, $this->get('monolog.logger.event'), $stlConverter, $relationMapper);
$this->modelLoader->setOutput(new NullOutput()); $this->modelLoader->setOutput(new NullOutput());
$this->setUpDb(); $this->setUpDb([LoadColors::class]);
} }
public function testLoadOne() public function testLoadOne()
{ {
$this->modelLoader->setLDrawLibraryContext(__DIR__ . '/fixtures/librarycontext/'); $this->modelLoader->setLDrawLibraryContext(__DIR__.'/fixtures/librarycontext/');
$this->modelLoader->loadOne(__DIR__ . '/fixtures/librarycontext/parts/3820.dat'); $this->modelLoader->loadOne(__DIR__.'/fixtures/librarycontext/parts/3820.dat');
/** @var Model[] $models */ /** @var Model[] $models */
$models = $this->modelRepository->findAll(); $models = $this->modelRepository->findAll();
@ -67,33 +67,33 @@ class ModelLoaderTest extends BaseTest
public function testFileContext() public function testFileContext()
{ {
$this->modelLoader->setLDrawLibraryContext(__DIR__ . '/fixtures/librarycontext/'); $this->modelLoader->setLDrawLibraryContext(__DIR__.'/fixtures/librarycontext/');
$this->modelLoader->loadOne(__DIR__ . '/fixtures/filecontext/parts/999.dat'); $this->modelLoader->loadOne(__DIR__.'/fixtures/filecontext/parts/999.dat');
/** @var Model[] $models */ /** @var Model[] $models */
$models = $this->modelRepository->findAll(); $models = $this->modelRepository->findAll();
$this->assertEquals(1, count($models)); $this->assertEquals(1, count($models));
$this->assertEquals(3820, $models[0]->getId()); $this->assertEquals(3820, $models[0]->getId());
$this->assertEquals(2,count($models[0]->getAliases())); $this->assertEquals(2, count($models[0]->getAliases()));
} }
public function testLoadAlias() public function testLoadAlias()
{ {
$this->modelLoader->setLDrawLibraryContext(__DIR__ . '/fixtures/librarycontext/'); $this->modelLoader->setLDrawLibraryContext(__DIR__.'/fixtures/librarycontext/');
$this->modelLoader->loadOne(__DIR__ . '/fixtures/filecontext/parts/999.dat'); $this->modelLoader->loadOne(__DIR__.'/fixtures/filecontext/parts/999.dat');
/** @var Model[] $models */ /** @var Model[] $models */
$models = $this->modelRepository->findAll(); $models = $this->modelRepository->findAll();
$this->assertEquals(1, count($models)); $this->assertEquals(1, count($models));
$this->assertEquals(3820, $models[0]->getId()); $this->assertEquals(3820, $models[0]->getId());
$this->assertEquals(2,count($models[0]->getAliases())); $this->assertEquals(2, count($models[0]->getAliases()));
} }
public function testLicense() public function testLicense()
{ {
$this->modelLoader->setLDrawLibraryContext(__DIR__ . '/fixtures/license/'); $this->modelLoader->setLDrawLibraryContext(__DIR__.'/fixtures/license/');
$this->modelLoader->loadAll(); $this->modelLoader->loadAll();
$models = $this->modelRepository->findAll(); $models = $this->modelRepository->findAll();
@ -104,7 +104,7 @@ class ModelLoaderTest extends BaseTest
public function testIsIncluded() public function testIsIncluded()
{ {
$this->modelLoader->setLDrawLibraryContext(__DIR__ . '/fixtures/included/'); $this->modelLoader->setLDrawLibraryContext(__DIR__.'/fixtures/included/');
$this->modelLoader->loadAll(); $this->modelLoader->loadAll();
$models = $this->modelRepository->findAll(); $models = $this->modelRepository->findAll();
@ -113,7 +113,7 @@ class ModelLoaderTest extends BaseTest
public function testLoadAll() public function testLoadAll()
{ {
$this->modelLoader->setLDrawLibraryContext(__DIR__ . '/fixtures/librarycontext/'); $this->modelLoader->setLDrawLibraryContext(__DIR__.'/fixtures/librarycontext/');
$this->modelLoader->loadAll(); $this->modelLoader->loadAll();
$models = $this->modelRepository->findAll(); $models = $this->modelRepository->findAll();
@ -124,17 +124,17 @@ class ModelLoaderTest extends BaseTest
public function testUpdate() public function testUpdate()
{ {
// Load original model // Load original model
$this->modelLoader->setLDrawLibraryContext(__DIR__ . '/fixtures/update/version1/'); $this->modelLoader->setLDrawLibraryContext(__DIR__.'/fixtures/update/version1/');
$this->modelLoader->loadAll(); $this->modelLoader->loadAll();
$this->assertEquals(1, count($this->modelRepository->findAll())); $this->assertEquals(1, count($this->modelRepository->findAll()));
$model = $this->modelRepository->findOneByNumber('983'); $model = $this->modelRepository->findOneByNumber('983');
$this->assertInstanceOf(Model::class, $model); $this->assertInstanceOf(Model::class, $model);
$this->assertEquals('3820',$model->getId()); $this->assertEquals('3820', $model->getId());
$this->assertEquals(2, count($model->getAliases())); $this->assertEquals(2, count($model->getAliases()));
// Load new version // Load new version
$this->modelLoader->setLDrawLibraryContext(__DIR__ . '/fixtures/update/version2/'); $this->modelLoader->setLDrawLibraryContext(__DIR__.'/fixtures/update/version2/');
$this->modelLoader->setRewrite(true); $this->modelLoader->setRewrite(true);
$this->modelLoader->loadAll(); $this->modelLoader->loadAll();
@ -151,31 +151,49 @@ class ModelLoaderTest extends BaseTest
public function testUpdate2() public function testUpdate2()
{ {
// Load original model // Load original model
$this->modelLoader->setLDrawLibraryContext(__DIR__ . '/fixtures/update2/version1/'); $this->modelLoader->setLDrawLibraryContext(__DIR__.'/fixtures/update2/version1/');
$this->modelLoader->loadAll(); $this->modelLoader->loadAll();
// Load new version // Load new version
$this->modelLoader->setLDrawLibraryContext(__DIR__ . '/fixtures/update2/version2/'); $this->modelLoader->setLDrawLibraryContext(__DIR__.'/fixtures/update2/version2/');
$this->modelLoader->setRewrite(false); $this->modelLoader->setRewrite(false);
$this->modelLoader->loadAll(); $this->modelLoader->loadAll();
/** @var Model $model */ /** @var Model $model */
$model = $this->modelRepository->find('3820'); $model = $this->modelRepository->find('3820');
$this->assertEquals(1, count($this->modelRepository->findAll())); $this->assertEquals(1, count($this->modelRepository->findAll()));
$this->assertEquals(2009,$model->getModified()->format('Y')); $this->assertEquals(2009, $model->getModified()->format('Y'));
$this->modelLoader->setRewrite(true); $this->modelLoader->setRewrite(true);
$this->modelLoader->loadAll(); $this->modelLoader->loadAll();
$this->assertEquals(2010,$model->getModified()->format('Y')); $this->assertEquals(2010, $model->getModified()->format('Y'));
} }
public function testLoadOfPrinted() { public function testLoadOfPrinted()
$this->modelLoader->setLDrawLibraryContext(__DIR__ . '/fixtures/printed/'); {
$this->modelLoader->loadOne(__DIR__ . '/fixtures/printed/parts/30367bps7.dat'); $this->modelLoader->setLDrawLibraryContext(__DIR__.'/fixtures/printed/');
$this->modelLoader->loadOne(__DIR__.'/fixtures/printed/parts/30367bps7.dat');
$models = $this->modelRepository->findAll(); $models = $this->modelRepository->findAll();
$this->assertEquals(1, count($models)); $this->assertEquals(1, count($models));
$this->assertEquals('30367b', $models[0]->getId()); $this->assertEquals('30367b', $models[0]->getId());
} }
}
public function testDownload()
{
$library = $this->modelLoader->downloadLibrary(__DIR__.'/fixtures/ldraw.zip');
$this->assertDirectoryExists($library);
}
/**
* @expectedException \LogicException
*/
public function testDownloadFailed()
{
$library = $this->modelLoader->downloadLibrary(__DIR__.'/fixtures/nofile.zip');
$this->assertDirectoryExists($library);
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace Tests\LoaderBundle\Service;
use AppBundle\DataFixtures\ORM\LoadColors;
use AppBundle\Entity\Rebrickable\Set;
use LoaderBundle\Service\RebrickableLoader;
use Tests\AppBundle\BaseTest;
class RebrickableLoaderTest extends BaseTest
{
/**
* @var RebrickableLoader
*/
private $rebrickableLoader;
public function setUp()
{
parent::setUp();
$this->setUpDb([LoadColors::class]);
$this->rebrickableLoader = new RebrickableLoader($this->em, $this->get('monolog.logger.event'), __DIR__.'/fixtures/');
}
public function testLoadAll()
{
$this->assertCount(0, $this->em->getRepository(Set::class)->findAll());
$this->rebrickableLoader->loadAll();
$this->assertCount(1, $this->em->getRepository(Set::class)->findAll());
}
// /**
// * @expectedException LoaderBundle\Exception\Loader\LoadingRebrickableFailedException
// */
// public function testRollback() {
// $this->rebrickableLoader = new RebrickableLoader($this->em,$this->get('monolog.logger.event'),__DIR__.'/corrupt/');
//
// $this->assertCount(0,$this->em->getRepository(Set::class)->findAll());
//
// $this->rebrickableLoader->loadAll();
//
// $this->assertCount(0,$this->em->getRepository(Set::class)->findAll());
// }
}

View File

@ -0,0 +1,2 @@
id,version,set_num
4609,1,0013-1
1 id version set_num
2 4609 1 0013-1

View File

@ -0,0 +1,8 @@
inventory_id,part_num,color_id,quantity,is_spare
4609,3626apr0001,14,2,f
4609,3838,15,2,f
4609,3842a,15,2,f
4609,3962a,0,2,f
4609,970c00,15,2,f
4609,973p90c05,15,2,f
4609,255,15,2,f
1 inventory_id part_num color_id quantity is_spare
2 4609 3626apr0001 14 2 f
3 4609 3838 15 2 f
4 4609 3842a 15 2 f
5 4609 3962a 0 2 f
6 4609 970c00 15 2 f
7 4609 973p90c05 15 2 f
8 4609 255 15 2 f

View File

@ -0,0 +1 @@
inventory_id,set_num,quantity
1 inventory_id set_num quantity

View File

@ -0,0 +1,3 @@
,id,name,dasd
,,13,Minifigs,,
27,Minifig Accessories
1 ,id,name,dasd
2 ,,13,Minifigs,,
3 27,Minifig Accessories

View File

@ -0,0 +1,7 @@
part_num,name,part_cat_id
3626apr0001,Minifig Head Standard Grin Print [Solid Stud],13
3838,Minifig Airtanks,27
3842a,Minifig Helmet Classic with Thin Chin Guard and Visor Dimples,27
3962a,Minifig Radio [Compact Handle],27
970c00,Legs and Hips [Complete Assembly],13
973p90c05,Torso Space Classic Moon Print / White Arms / White Hands,13
1 part_num name part_cat_id
2 3626apr0001 Minifig Head Standard Grin Print [Solid Stud] 13
3 3838 Minifig Airtanks 27
4 3842a Minifig Helmet Classic with Thin Chin Guard and Visor Dimples 27
5 3962a Minifig Radio [Compact Handle] 27
6 970c00 Legs and Hips [Complete Assembly] 13
7 973p90c05 Torso Space Classic Moon Print / White Arms / White Hands 13

View File

@ -0,0 +1,2 @@
set_num,name,year,theme_id,num_parts
0013-1,Space Mini-Figures,1979,143,12
1 set_num name year theme_id num_parts
2 0013-1 Space Mini-Figures 1979 143 12

View File

@ -0,0 +1,3 @@
id,name,parent_id
126,Space,
143,Supplemental,126
1 id name parent_id
2 126 Space
3 143 Supplemental 126

View File

@ -0,0 +1,2 @@
id,version,set_num
4609,1,0013-1
1 id version set_num
2 4609 1 0013-1

View File

@ -0,0 +1,8 @@
inventory_id,part_num,color_id,quantity,is_spare
4609,3626apr0001,14,2,f
4609,3838,15,2,f
4609,3842a,15,2,f
4609,3962a,0,2,f
4609,970c00,15,2,f
4609,973p90c05,15,2,f
4609,255,15,2,f
1 inventory_id part_num color_id quantity is_spare
2 4609 3626apr0001 14 2 f
3 4609 3838 15 2 f
4 4609 3842a 15 2 f
5 4609 3962a 0 2 f
6 4609 970c00 15 2 f
7 4609 973p90c05 15 2 f
8 4609 255 15 2 f

View File

@ -0,0 +1 @@
inventory_id,set_num,quantity
1 inventory_id set_num quantity

View File

@ -0,0 +1,3 @@
id,name
13,Minifigs
27,Minifig Accessories
1 id name
2 13 Minifigs
3 27 Minifig Accessories

View File

@ -0,0 +1,7 @@
part_num,name,part_cat_id
3626apr0001,Minifig Head Standard Grin Print [Solid Stud],13
3838,Minifig Airtanks,27
3842a,Minifig Helmet Classic with Thin Chin Guard and Visor Dimples,27
3962a,Minifig Radio [Compact Handle],27
970c00,Legs and Hips [Complete Assembly],13
973p90c05,Torso Space Classic Moon Print / White Arms / White Hands,13
1 part_num name part_cat_id
2 3626apr0001 Minifig Head Standard Grin Print [Solid Stud] 13
3 3838 Minifig Airtanks 27
4 3842a Minifig Helmet Classic with Thin Chin Guard and Visor Dimples 27
5 3962a Minifig Radio [Compact Handle] 27
6 970c00 Legs and Hips [Complete Assembly] 13
7 973p90c05 Torso Space Classic Moon Print / White Arms / White Hands 13

View File

@ -0,0 +1,2 @@
set_num,name,year,theme_id,num_parts
0013-1,Space Mini-Figures,1979,143,12
1 set_num name year theme_id num_parts
2 0013-1 Space Mini-Figures 1979 143 12

View File

@ -0,0 +1,3 @@
id,name,parent_id
126,Space,
143,Supplemental,126
1 id name parent_id
2 126 Space
3 143 Supplemental 126

View File

@ -0,0 +1,54 @@
<?php
namespace Tests\LoaderBundle\Service;
use AppBundle\Entity\Rebrickable\Part;
use League\Flysystem\File;
use LoaderBundle\Service\RelationLoader;
use LoaderBundle\Util\RelationMapper;
use Symfony\Component\Console\Output\NullOutput;
use Tests\AppBundle\BaseTest;
use Tests\AppBundle\Fixtures\LoadUnmappedData;
class RelationLoaderTest extends BaseTest
{
/**
* @var RelationLoader
*/
private $relationLoader;
public function setUp()
{
parent::setUp();
$this->setUpDb([LoadUnmappedData::class]);
$file = $this->createMock(File::class);
$file->method('getPath')->willReturn('path');
$relationMapper = $this->createMock(RelationMapper::class);
$relationMapper->method('find')->will($this->returnArgument(0));
$this->relationLoader = new RelationLoader($this->get('doctrine.orm.entity_manager'), $this->get('monolog.logger.event'), $relationMapper);
$this->relationLoader->setOutput(new NullOutput());
}
public function testLoadAll()
{
$this->relationLoader->loadAll();
$parts = $this->em->getRepository(Part::class)->findAll();
foreach ($parts as $part) {
$this->assertNotNull($part->getModel());
}
}
public function testLoadNotPaired()
{
$this->relationLoader->loadNotPaired();
$parts = $this->em->getRepository(Part::class)->findAll();
foreach ($parts as $part) {
$this->assertNotNull($part->getModel());
}
}
}

View File

@ -2,10 +2,10 @@
namespace Tests\AppBundle\Service\Stl; namespace Tests\AppBundle\Service\Stl;
use LoaderBundle\Service\Stl\StlConverterService;
use LoaderBundle\Service\Stl\StlFixerService;
use League\Flysystem\Adapter\Local; use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem; use League\Flysystem\Filesystem;
use LoaderBundle\Service\Stl\StlConverterService;
use LoaderBundle\Service\Stl\StlFixerService;
use Tests\AppBundle\BaseTest; use Tests\AppBundle\BaseTest;
class StlConverterTest extends BaseTest class StlConverterTest extends BaseTest
@ -13,7 +13,6 @@ class StlConverterTest extends BaseTest
/** @var StlConverterService */ /** @var StlConverterService */
private $stlConverter; private $stlConverter;
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
@ -23,40 +22,70 @@ class StlConverterTest extends BaseTest
$stlFixer = $this->createMock(StlFixerService::class); $stlFixer = $this->createMock(StlFixerService::class);
$stlFixer->method('fix'); $stlFixer->method('fix');
$this->stlConverter = new StlConverterService($ldview,$this->filesystem,$stlFixer); $this->stlConverter = new StlConverterService($ldview, $this->filesystem, $stlFixer);
} }
public function testConvertToStl() public function testConvertToStl()
{ {
$adapter = new Local(__DIR__ . '/fixtures/ldraw'); $adapter = new Local(__DIR__.'/fixtures/ldraw');
$ldrawLibraryContext = new Filesystem($adapter); $ldrawLibraryContext = new Filesystem($adapter);
$this->stlConverter->setLDrawLibraryContext($ldrawLibraryContext); $this->stlConverter->setLDrawLibraryContext($ldrawLibraryContext);
$this->assertNotNull($this->stlConverter->datToStl(__DIR__ . '/fixtures/ldraw/parts/983.dat')); $this->assertNotNull($this->stlConverter->datToStl(__DIR__.'/fixtures/ldraw/parts/983.dat'));
$this->assertTrue($this->filesystem->has('models/983.stl')); $this->assertTrue($this->filesystem->has('models/983.stl'));
}
$this->filesystem->delete('models/983.stl'); public function testRewriteTrue()
{
$adapter = new Local(__DIR__.'/fixtures/ldraw');
$ldrawLibraryContext = new Filesystem($adapter);
$this->stlConverter->setLDrawLibraryContext($ldrawLibraryContext);
$this->filesystem->write('models/983.stl', file_get_contents(__DIR__.'/fixtures/983.stl'));
$this->assertTrue($this->filesystem->has('models/983.stl'));
$this->stlConverter->datToStl(__DIR__.'/fixtures/ldraw/parts/983.dat', true);
$this->assertEquals(file_get_contents(__DIR__.'/fixtures/expected.stl'), $this->filesystem->read('models/983.stl'));
}
public function testRewriteFalse()
{
$adapter = new Local(__DIR__.'/fixtures/ldraw');
$ldrawLibraryContext = new Filesystem($adapter);
$this->stlConverter->setLDrawLibraryContext($ldrawLibraryContext);
$this->filesystem->write('models/983.stl', file_get_contents(__DIR__.'/fixtures/983.stl'));
$this->assertTrue($this->filesystem->has('models/983.stl'));
$this->stlConverter->datToStl(__DIR__.'/fixtures/ldraw/parts/983.dat');
$this->assertEquals(file_get_contents(__DIR__.'/fixtures/983.stl'), $this->filesystem->read('models/983.stl'));
} }
/** /**
* @expectedException LoaderBundle\Exception\Stl\LDLibraryMissingException * @expectedException \LoaderBundle\Exception\Stl\LDLibraryMissingException
*/ */
public function testLDContextMissing() public function testLDContextMissing()
{ {
$this->stlConverter->datToStl(__DIR__ . '/fixtures/ldraw/parts/983.dat'); $this->stlConverter->datToStl(__DIR__.'/fixtures/ldraw/parts/983.dat');
} }
public function testConvertToPng() /**
* @expectedException \Symfony\Component\Process\Exception\ProcessFailedException
*/
public function testProcessFailedException()
{ {
$adapter = new Local(__DIR__ . '/fixtures/ldraw'); $stlFixer = $this->createMock(StlFixerService::class);
$stlFixer->method('fix');
$this->stlConverter = new StlConverterService('', $this->filesystem, $stlFixer);
$adapter = new Local(__DIR__.'/fixtures/ldraw');
$ldrawLibraryContext = new Filesystem($adapter); $ldrawLibraryContext = new Filesystem($adapter);
$this->stlConverter->setLDrawLibraryContext($ldrawLibraryContext); $this->stlConverter->setLDrawLibraryContext($ldrawLibraryContext);
$this->assertNotNull($this->stlConverter->datToPng(__DIR__ . '/fixtures/ldraw/parts/983.dat')); $this->stlConverter->datToStl(__DIR__.'/fixtures/ldraw/parts/983.dat');
$this->assertTrue($this->filesystem->has('images/983.png'));
$this->filesystem->delete('images/983.png');
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -2,12 +2,7 @@
namespace Tests\AppBundle\Service\Stl; namespace Tests\AppBundle\Service\Stl;
use LoaderBundle\Service\Stl\StlConverterService;
use LoaderBundle\Service\Stl\StlFixerService; use LoaderBundle\Service\Stl\StlFixerService;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemInterface;
use PHPUnit\Framework\TestCase;
use Tests\AppBundle\BaseTest; use Tests\AppBundle\BaseTest;
class StlFixer extends BaseTest class StlFixer extends BaseTest
@ -22,18 +17,30 @@ class StlFixer extends BaseTest
parent::setUp(); parent::setUp();
$this->stlFixer = new StlFixerService($this->getParameter('admesh_bin')); $this->stlFixer = new StlFixerService($this->getParameter('admesh_bin'));
$this->input = __DIR__ . '/fixtures/ascii.stl'; $this->input = __DIR__.'/fixtures/ascii.stl';
}
public function tearDown()
{
$this->filesystem->delete('output.stl');
} }
public function testFixing() public function testFixing()
{ {
$this->stlFixer->fix($this->input,$this->filesystem->getAdapter()->getPathPrefix().'/output.stl'); $this->stlFixer->fix($this->input, $this->filesystem->getAdapter()->getPathPrefix().'/output.stl');
$this->assertTrue($this->filesystem->has('output.stl')); $this->assertTrue($this->filesystem->has('output.stl'));
} }
}
/**
* @expectedException \LoaderBundle\Exception\FileNotFoundException
*/
public function testFileNotFound()
{
$this->stlFixer->fix('', '');
}
/**
* @expectedException \Symfony\Component\Process\Exception\ProcessFailedException
*/
public function testCorruptProcess()
{
$this->stlFixer = new StlFixerService('');
$this->stlFixer->fix($this->input);
}
}

View File

@ -2,13 +2,7 @@
namespace Tests\AppBundle\Service\Stl; namespace Tests\AppBundle\Service\Stl;
use LoaderBundle\Service\Stl\StlConverterService;
use LoaderBundle\Service\Stl\StlFixerService;
use LoaderBundle\Service\Stl\StlRendererService; use LoaderBundle\Service\Stl\StlRendererService;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemInterface;
use PHPUnit\Framework\TestCase;
use Tests\AppBundle\BaseTest; use Tests\AppBundle\BaseTest;
class StlRendererTest extends BaseTest class StlRendererTest extends BaseTest
@ -20,21 +14,37 @@ class StlRendererTest extends BaseTest
{ {
parent::setUp(); parent::setUp();
$layout = __DIR__ . '/fixtures/layout.tmpl'; $layout = __DIR__.'/fixtures/layout.tmpl';
$povray = $this->getParameter('povray_bin'); $povray = $this->getParameter('povray_bin');
$stl2pov = $this->getParameter('stl2pov_bin'); $stl2pov = $this->getParameter('stl2pov_bin');
$this->stlRenderer = new StlRendererService($layout,$povray,$stl2pov); $this->stlRenderer = new StlRendererService($layout, $povray, $stl2pov);
}
public function tearDown()
{
$this->filesystem->delete('973c00.png');
} }
public function testRendering() public function testRendering()
{ {
$this->stlRenderer->render(__DIR__ . '/fixtures/973c00.stl',$this->filesystem->getAdapter()->getPathPrefix()); $this->stlRenderer->render(__DIR__.'/fixtures/973c00.stl', $this->filesystem->getAdapter()->getPathPrefix());
$this->assertTrue($this->filesystem->has('973c00.png')); $this->assertTrue($this->filesystem->has('973c00.png'));
} }
}
/**
* @expectedException \LoaderBundle\Exception\FileNotFoundException
*/
public function testFileNotFound()
{
$this->stlRenderer->render('abc', $this->filesystem->getAdapter()->getPathPrefix());
}
/**
* @expectedException \Symfony\Component\Process\Exception\ProcessFailedException
*/
public function testPovRayMissing()
{
$layout = __DIR__.'/fixtures/layout.tmpl';
$stl2pov = $this->getParameter('stl2pov_bin');
$this->stlRenderer = new StlRendererService($layout, '', $stl2pov);
$this->stlRenderer->render(__DIR__.'/fixtures/973c00.stl', $this->filesystem->getAdapter()->getPathPrefix());
$this->assertTrue($this->filesystem->has('973c00.png'));
}
}

View File

@ -2,11 +2,8 @@
namespace Tests\LoaderBundle\Util\LDModelParser; namespace Tests\LoaderBundle\Util\LDModelParser;
use AppBundle\Exception\ErrorParsingLineException;
use AppBundle\Exception\ParseErrorException;
use LoaderBundle\Util\LDModelParser; use LoaderBundle\Util\LDModelParser;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\DateTime;
class LDModelParserTest extends TestCase class LDModelParserTest extends TestCase
{ {
@ -22,56 +19,63 @@ class LDModelParserTest extends TestCase
public function testValid() public function testValid()
{ {
$resource = file_get_contents(__DIR__ . '/fixtures/valid.dat'); $resource = file_get_contents(__DIR__.'/fixtures/valid.dat');
$array = [ $array = [
"id" => "1234", 'id' => '1234',
"name" => "Category Name", 'name' => 'Category Name',
"category" => "Category!", 'category' => 'Category!',
"keywords" => [ 'keywords' => [
'keyword1', 'keyword 2', 'keyword3', 'keyword4' 'keyword1', 'keyword 2', 'keyword3', 'keyword4',
], ],
"author" => "Author [nickname]", 'author' => 'Author [nickname]',
"modified" => new \DateTime('2017-04-01'), 'modified' => new \DateTime('2017-04-01'),
"type" => "Part", 'type' => 'Part',
'subparts' => [ 'subparts' => [
'submodel' => [ 'submodel' => [
1 => 2, 1 => 2,
16 => 1 16 => 1,
] ],
], ],
"parent" => null, 'parent' => null,
"license" => "Redistributable under CCAL version 2.0", 'license' => 'Redistributable under CCAL version 2.0',
]; ];
$this->assertEquals($array, $this->parser->parse($resource)); $this->assertEquals($array, $this->parser->parse($resource));
} }
/** /**
* @expectedException LoaderBundle\Exception\ErrorParsingLineException * @expectedException \LoaderBundle\Exception\ErrorParsingLineException
*/ */
public function testInvalid() public function testInvalid()
{ {
$resource = file_get_contents(__DIR__ . '/fixtures/invalid.dat'); $resource = file_get_contents(__DIR__.'/fixtures/invalid.dat');
$this->parser->parse($resource); $this->parser->parse($resource);
} }
public function testStickers() { public function testStickers()
$resource = file_get_contents(__DIR__ . '/fixtures/stickers.txt'); {
$resource = file_get_contents(__DIR__.'/fixtures/stickers.txt');
foreach (preg_split('/^---DAT/m', $resource) as $dat) { foreach (preg_split('/^---DAT/m', $resource) as $dat) {
$this->assertEquals('Sticker', $this->parser->parse($dat)['type']); $this->assertEquals('Sticker', $this->parser->parse($dat)['type']);
} }
} }
public function testColor()
{
$resource = file_get_contents(__DIR__.'/fixtures/color.dat');
$this->assertEquals('3705', $this->parser->parse($resource)['parent']);
}
public function testAlias() public function testAlias()
{ {
$resource = file_get_contents(__DIR__ . '/fixtures/alias.txt'); $resource = file_get_contents(__DIR__.'/fixtures/alias.txt');
foreach (preg_split('/^---DAT/m', $resource) as $dat) { foreach (preg_split('/^---DAT/m', $resource) as $dat) {
$this->assertEquals('parent', $this->parser->parse($dat)['parent']); $this->assertEquals('parent', $this->parser->parse($dat)['parent']);
} }
} }
} }

View File

@ -0,0 +1,12 @@
0 _Technic Axle 4 Black
0 Name: 370526.dat
0 Author: Michael Heidemann [mikeheide]
0 !LDRAW_ORG Part Physical_Colour UPDATE 2010-03
0 !LICENSE Redistributable under CCAL version 2.0 : see CAreadme.txt
0 BFC CERTIFY CCW
0 !HISTORY 2010-12-31 [PTadmin] Official Update 2010-03
1 0 0 0 0 1 0 0 0 1 0 0 0 1 3705.DAT
0

View File

@ -2,25 +2,23 @@
namespace Tests\LoaderBundle\Util\RelationMapper; namespace Tests\LoaderBundle\Util\RelationMapper;
use LoaderBundle\Util\RelationMapper;
use Doctrine\Common\Cache\ArrayCache; use Doctrine\Common\Cache\ArrayCache;
use LoaderBundle\Util\RelationMapper;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Yaml\Yaml;
class RelationMapperTest extends TestCase class RelationMapperTest extends TestCase
{ {
public function testLoad() public function testLoad()
{ {
$mapper = new RelationMapper(new ArrayCache()); $mapper = new RelationMapper(new ArrayCache());
$mapper->loadResource(__DIR__ . '/fixtures/resources.yml', 'resources'); $mapper->loadResource(__DIR__.'/fixtures/resources.yml', 'resources');
$this->assertEquals('bar', $mapper->find('foo','resources')); $this->assertEquals('bar', $mapper->find('foo', 'resources'));
$this->assertEquals('bar', $mapper->find('bar','resources')); $this->assertEquals('bar', $mapper->find('bar', 'resources'));
} }
/** /**
* @expectedException LoaderBundle\Exception\RelationMapper\ResourceNotFoundException * @expectedException \LoaderBundle\Exception\RelationMapper\ResourceNotFoundException
*/ */
public function testLoadNonExistingResource() public function testLoadNonExistingResource()
{ {
@ -30,12 +28,23 @@ class RelationMapperTest extends TestCase
} }
/** /**
* @expectedException LoaderBundle\Exception\RelationMapper\InvalidResourceException * @expectedException \LoaderBundle\Exception\RelationMapper\InvalidResourceException
*/ */
public function testLoadInvalidResource() public function testLoadInvalidResource()
{ {
$mapper = new RelationMapper(new ArrayCache()); $mapper = new RelationMapper(new ArrayCache());
$resource = __DIR__ . '/fixtures/invalid.yml'; $resource = __DIR__.'/fixtures/invalid.yml';
$mapper->loadResource($resource, 'resources'); $mapper->loadResource($resource, 'resources');
} }
}
/**
* @expectedException \LoaderBundle\Exception\RelationMapper\InvalidDomainException
*/
public function testLoadInvalidDomain()
{
$mapper = new RelationMapper(new ArrayCache());
$mapper->loadResource(__DIR__.'/fixtures/resources.yml', 'resources');
$mapper->find('foo', 'incorect');
}
}