1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-17 21:00:09 -07:00

Add FrontBundle tests

This commit is contained in:
Unknown 2017-06-23 22:21:25 +02:00
parent 9089929e66
commit f2899fcf17
14 changed files with 211 additions and 156 deletions

View File

@ -16,7 +16,6 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Symfony\Component\HttpFoundation\ResponseHeaderBag;
/** /**
@ -90,19 +89,9 @@ class ModelController extends Controller
$request->query->getInt('limit', 16)/*limit per page*/ $request->query->getInt('limit', 16)/*limit per page*/
); );
$template = $this->render('model/tabs/sets.html.twig', [ return $this->render('model/tabs/sets.html.twig', [
'sets' => $sets, 'sets' => $sets,
]); ]);
if ($request->isXmlHttpRequest()) {
$json = json_encode($template->getContent());
$response = new Response($json, 200);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
return $template;
} }
/** /**

View File

@ -8,7 +8,6 @@ use Knp\Component\Pager\Paginator;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/** /**
* Part controller. * Part controller.
@ -24,18 +23,10 @@ class PartController extends Controller
*/ */
public function detailAction(Part $part, SetService $setService) public function detailAction(Part $part, SetService $setService)
{ {
if ($part) { return $this->render('part/detail.html.twig', [
if ($model = $part->getModel()) { 'part' => $part,
$this->redirectToRoute('model_detail', ['id' => $model->getId()]); 'setCount' => count($setService->getAllByPart($part)),
} ]);
return $this->render('part/detail.html.twig', [
'part' => $part,
'setCount' => count($setService->getAllByPart($part)),
]);
}
return $this->render('error/error.html.twig');
} }
/** /**
@ -51,18 +42,8 @@ class PartController extends Controller
$request->query->getInt('limit', 16)/*limit per page*/ $request->query->getInt('limit', 16)/*limit per page*/
); );
$template = $this->render('model/tabs/sets.html.twig', [ return $this->render('model/tabs/sets.html.twig', [
'sets' => $sets, 'sets' => $sets,
]); ]);
if ($request->isXmlHttpRequest()) {
$json = json_encode($template->getContent());
$response = new Response($json, 200);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
return $template;
} }
} }

View File

@ -1,6 +1,6 @@
<?php <?php
namespace FrontBundle\Controller\Brickset; namespace FrontBundle\Controller\Set;
use AppBundle\Api\Exception\ApiException; use AppBundle\Api\Exception\ApiException;
use AppBundle\Api\Manager\BricksetManager; use AppBundle\Api\Manager\BricksetManager;
@ -11,9 +11,9 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
/** /**
* @Route("/brickset/sets") * @Route("sets/brickset")
*/ */
class SetController extends Controller class BricksetController extends Controller
{ {
/** /**
* @Route("/{id}/instructions", name="brickset_instructions") * @Route("/{id}/instructions", name="brickset_instructions")

View File

@ -1,6 +1,6 @@
<?php <?php
namespace FrontBundle\Controller; namespace FrontBundle\Controller\Set;
use AppBundle\Api\Exception\ApiException; use AppBundle\Api\Exception\ApiException;
use AppBundle\Api\Manager\BricksetManager; use AppBundle\Api\Manager\BricksetManager;
@ -29,6 +29,8 @@ class SetController extends Controller
* *
* @param Request $request * @param Request $request
* *
* @param FormFactoryInterface $formFactory
* @param SearchService $searchService
* @return Response * @return Response
*/ */
public function indexAction(Request $request, FormFactoryInterface $formFactory, SearchService $searchService) public function indexAction(Request $request, FormFactoryInterface $formFactory, SearchService $searchService)
@ -56,6 +58,8 @@ class SetController extends Controller
* *
* @param Set $set * @param Set $set
* *
* @param SetService $setService
* @param BricksetManager $bricksetManager
* @return Response * @return Response
*/ */
public function detailAction(Set $set, SetService $setService, BricksetManager $bricksetManager) public function detailAction(Set $set, SetService $setService, BricksetManager $bricksetManager)
@ -81,10 +85,14 @@ class SetController extends Controller
/** /**
* @Route("/{id}/inventory", name="set_inventory") * @Route("/{id}/inventory", name="set_inventory")
* @param Request $request
* @param Set $set
* @param SetService $setService
* @return Response
*/ */
public function inventoryAction(Request $request, Set $set, SetService $setService) public function inventoryAction(Request $request, Set $set, SetService $setService)
{ {
$template = $this->render('set/tabs/inventory.html.twig', [ return $this->render('set/tabs/inventory.html.twig', [
'inventorySets' => $setService->getAllSubSets($set), 'inventorySets' => $setService->getAllSubSets($set),
'set' => $set, 'set' => $set,
'missing' => $setService->getParts($set, false, false), 'missing' => $setService->getParts($set, false, false),
@ -92,84 +100,44 @@ class SetController extends Controller
'missingCount' => $setService->getPartCount($set, false, false), 'missingCount' => $setService->getPartCount($set, false, false),
'partCount' => $setService->getPartCount($set, false), 'partCount' => $setService->getPartCount($set, false),
]); ]);
if ($request->isXmlHttpRequest()) {
$json = json_encode($template->getContent());
$response = new Response($json, 200);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
return $template;
} }
/** /**
* @Route("/{id}/models", name="set_models") * @Route("/{id}/models", name="set_models")
* @param Set $set
* @param SetService $setService
* @return Response
*/ */
public function modelsAction(Request $request, Set $set, SetService $setService) public function modelsAction(Set $set, SetService $setService)
{ {
$models = null; return $this->render('set/tabs/models.html.twig', [
$missing = null;
try {
$models = $setService->getModels($set, false);
$missing = $setService->getParts($set, false, false);
} catch (\Exception $e) {
$this->addFlash('error', $e->getMessage());
}
$template = $this->render('set/tabs/models.html.twig', [
'set' => $set, 'set' => $set,
'missing' => $missing, 'missing' => $setService->getParts($set, false, false),
'models' => $models, 'models' => $setService->getModels($set, false),
]); ]);
if ($request->isXmlHttpRequest()) {
$json = json_encode($template->getContent());
$response = new Response($json, 200);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
return $template;
} }
/** /**
* @Route("/{id}/colors", name="set_colors") * @Route("/{id}/colors", name="set_colors")
* @param Set $set
* @param SetService $setService
* @return Response
*/ */
public function colorsAction(Request $request, Set $set, SetService $setService) public function colorsAction(Set $set, SetService $setService)
{ {
$colors = null; return $this->render('set/tabs/colors.html.twig', [
$missing = null;
try {
$colors = $setService->getModelsGroupedByColor($set, false);
$missing = $setService->getParts($set, false, false);
} catch (\Exception $e) {
$this->addFlash('error', $e->getMessage());
}
$template = $this->render('set/tabs/colors.html.twig', [
'set' => $set, 'set' => $set,
'colors' => $colors, 'colors' => $setService->getModelsGroupedByColor($set, false),
'missing' => $missing, 'missing' => $setService->getParts($set, false, false),
]); ]);
if ($request->isXmlHttpRequest()) {
$json = json_encode($template->getContent());
$response = new Response($json, 200);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
return $template;
} }
/** /**
* @Route("/{id}/zip", name="set_zip") * @Route("/{id}/zip", name="set_zip")
* @param Request $request
* @param Set $set
* @param ZipService $zipService
* @return BinaryFileResponse
*/ */
public function zipAction(Request $request, Set $set, ZipService $zipService) public function zipAction(Request $request, Set $set, ZipService $zipService)
{ {

View File

@ -68,10 +68,7 @@ class SetSearchType extends AbstractType
'choice_label' => 'fullName', 'choice_label' => 'fullName',
'choice_translation_domain' => false, 'choice_translation_domain' => false,
'group_by' => function ($theme, $key, $index) { 'group_by' => function ($theme, $key, $index) {
/** @var Theme $theme */ return $theme->getGroup()->getName();
$parent = $theme->getParent();
return $parent ? $parent->getParent() ? $parent->getParent()->getName() : $parent->getName() : $theme->getName();
}, },
'choice_value' => 'id', 'choice_value' => 'id',
'placeholder' => 'set.form.theme.all', 'placeholder' => 'set.form.theme.all',

View File

@ -37,14 +37,10 @@ class PartImageLoader extends BaseImageLoader
} }
// try to load image from rebrickable website // try to load image from rebrickable website
try { if ($this->remoteFileExists($this->rebrickableContext.$path)) {
if ($this->remoteFileExists($this->rebrickableContext.$path)) { $context = stream_context_create(['http' => ['header' => 'Connection: close\r\n']]);
$context = stream_context_create(['http' => ['header' => 'Connection: close\r\n']]);
return file_get_contents($this->rebrickableContext.strtolower($path), false, $context); return file_get_contents($this->rebrickableContext.strtolower($path), false, $context);
}
} catch (\Exception $e) {
throw new NotLoadableException(sprintf('Source image %s could not be loaded.', $path), $e->getCode(), $e);
} }
// Load part entity form rebrickable api and get image path from response // Load part entity form rebrickable api and get image path from response
@ -61,6 +57,5 @@ class PartImageLoader extends BaseImageLoader
} }
throw new NotLoadableException(sprintf('Source image %s not found.', $path)); throw new NotLoadableException(sprintf('Source image %s not found.', $path));
// return $this->mediaFilesystem->read('noimage.png');
} }
} }

View File

@ -32,14 +32,10 @@ class SetImageLoader extends BaseImageLoader
public function find($path) public function find($path)
{ {
// try to load image from rebrickable website // try to load image from rebrickable website
try { if ($this->remoteFileExists($this->rebrickableContext.strtolower($path))) {
if ($this->remoteFileExists($this->rebrickableContext.strtolower($path))) { $context = stream_context_create(['http' => ['header' => 'Connection: close\r\n']]);
$context = stream_context_create(['http' => ['header' => 'Connection: close\r\n']]);
return file_get_contents($this->rebrickableContext.strtolower($path), false, $context); return file_get_contents($this->rebrickableContext.strtolower($path), false, $context);
}
} catch (\Exception $e) {
throw new NotLoadableException(sprintf('Source image %s could not be loaded.', $path), $e->getCode(), $e);
} }
// Load part entity form brickset api and get image path from response // Load part entity form brickset api and get image path from response
@ -56,6 +52,5 @@ class SetImageLoader extends BaseImageLoader
} }
throw new NotLoadableException(sprintf('Source image %s not found.', $path)); throw new NotLoadableException(sprintf('Source image %s not found.', $path));
// return $this->mediaFilesystem->read('noimage.png');
} }
} }

View File

@ -35,7 +35,6 @@ class AppExtension extends \Twig_Extension
return [ return [
new \Twig_SimpleFunction('remoteSize', [$this, 'remoteSize']), new \Twig_SimpleFunction('remoteSize', [$this, 'remoteSize']),
new \Twig_SimpleFunction('remoteFilename', [$this, 'remoteFilename']), new \Twig_SimpleFunction('remoteFilename', [$this, 'remoteFilename']),
new \Twig_SimpleFunction('remoteFileExists', [$this, 'remoteFileExists']),
new \Twig_SimpleFunction('fileTimestamp', [$this, 'fileTimestamp']), new \Twig_SimpleFunction('fileTimestamp', [$this, 'fileTimestamp']),
]; ];
} }
@ -45,22 +44,6 @@ class AppExtension extends \Twig_Extension
return $this->formatTransformer->bytesToSize($bytes, $precision); return $this->formatTransformer->bytesToSize($bytes, $precision);
} }
/**
* @param string $url
*
* @return bool
*/
public function remoteFileExists($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $status === 200 ? true : false;
}
public function remoteSize($url) public function remoteSize($url)
{ {
$ch = curl_init($url); $ch = curl_init($url);

View File

@ -9,28 +9,38 @@ class ApplicationAvailabilityTest extends BaseControllerTest
/** /**
* @dataProvider urlProvider * @dataProvider urlProvider
*/ */
public function testPageIsSuccessful($url) { public function testPageIsSuccessful($url)
{
$client = static::createClient(); $client = static::createClient();
$client->request('GET', $url); $client->request('GET', $url);
$this->assertTrue( $client->getResponse()->isSuccessful()); $this->assertTrue($client->getResponse()->isSuccessful());
} }
public function urlProvider() public function urlProvider()
{ {
return array( return [
array('/'), ['/'],
array('/colors/'), ['/colors/'],
array('/bricks/'), ['/bricks/'],
array('/bricks/1'), ['/bricks/1'],
array('/bricks/1/sets'), ['/bricks/1/zip'],
array('/sets/'), ['/bricks/1/sets'],
array('/sets/8049-1'), ['/sets/'],
array('/sets/8049-1/inventory'), ['/sets/?query=name&partCount[from]=620'],
array('/sets/8049-1/models'), ['/sets/8049-1'],
array('/sets/8049-1/colors'), ['/sets/8049-1/zip'],
array('/parts/1') ['/sets/8049-1/inventory'],
); ['/sets/8049-1/models'],
['/sets/8049-1/colors'],
['/parts/1'],
['/search/autocomplete?query=name'],
['/search/?query=name'],
['/sets/brickset/8540/reviews'],
['/sets/brickset/8540/instructions'],
['/sets/brickset/8540/description'],
['/sets/brickset/8540/images'],
];
} }
} }

View File

@ -2,18 +2,22 @@
namespace Tests\FrontBundle\Controller; namespace Tests\FrontBundle\Controller;
use Liip\FunctionalTestBundle\Test\WebTestCase; use Tests\AppBundle\BaseTest;
use Tests\AppBundle\Fixtures\LoadBaseData; use Tests\AppBundle\Fixtures\LoadBaseData;
abstract class BaseControllerTest extends WebTestCase abstract class BaseControllerTest extends BaseTest
{ {
public function setUp() public function setUp()
{ {
parent::setUp();
// If you are using the Doctrine Fixtures Bundle you could load these here // If you are using the Doctrine Fixtures Bundle you could load these here
$this->loadFixtures([ $this->loadFixtures([
LoadBaseData::class LoadBaseData::class,
]); ]);
$this->filesystem->write('models/1.stl', 'abcd');
$this->runCommand('fos:elastica:populate'); $this->runCommand('fos:elastica:populate');
} }
} }

View File

@ -0,0 +1 @@
file contents

View File

@ -0,0 +1,67 @@
<?php
namespace Tests\FrontBundle\Imagine;
use AppBundle\Api\Client\Rebrickable\Entity\Part;
use AppBundle\Api\Manager\RebrickableManager;
use FrontBundle\Imagine\PartImageLoader;
use Tests\AppBundle\BaseTest;
class PartImageLoaderTest extends BaseTest
{
/** @var PartImageLoader */
private $partImageLoader;
public function setUp()
{
parent::setUp();
$rebrickableManager = $this->createMock(RebrickableManager::class);
$part = new Part();
$part->setImgUrl(__DIR__.'/fixtures/1.png');
$rebrickableManager->method('getPart')
->willReturn($part);
$this->partImageLoader = new PartImageLoader($rebrickableManager, $this->filesystem);
}
public function testLocal()
{
$this->filesystem->write('images/-1/1.png', file_get_contents(__DIR__.'/fixtures/1.png'));
$this->assertNotNull($this->partImageLoader->find('-1/1.png'));
}
public function testWebsite()
{
$this->assertNotNull($this->partImageLoader->find('-1/1.png'));
}
public function testAPI()
{
$this->assertNotNull($this->partImageLoader->find('-1/123213.png'));
}
/**
* @expectedException \Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException
*/
public function testNotFound()
{
$this->assertNotNull($this->partImageLoader->find('123213.png'));
}
/**
* @expectedException \Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException
*/
public function testException()
{
$rebrickableManager = $this->createMock(RebrickableManager::class);
$rebrickableManager->method('getPart')
->willThrowException(new \Exception());
$this->partImageLoader = new PartImageLoader($rebrickableManager, $this->filesystem);
$this->partImageLoader->find('-1/123213.png');
}
}

View File

@ -0,0 +1,65 @@
<?php
namespace Tests\FrontBundle\Imagine;
use AppBundle\Api\Client\Brickset\Entity\Set;
use AppBundle\Api\Manager\BricksetManager;
use FrontBundle\Imagine\SetImageLoader;
use Tests\AppBundle\BaseTest;
class SetImageLoaderTest extends BaseTest
{
/** @var SetImageLoader */
private $setImageLoader;
public function setUp()
{
parent::setUp();
$bricksetManager = $this->createMock(BricksetManager::class);
$set = new Set();
$set->setImage(__DIR__.'/fixtures/1.png');
$bricksetManager->method('getSetByNumber')
->willReturn($set);
$this->setImageLoader = new SetImageLoader($bricksetManager, $this->filesystem);
}
public function testWebsite()
{
$this->assertNotNull($this->setImageLoader->find('4488-1.jpg'));
}
/**
* @expectedException \Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException
*/
public function testNotFound()
{
$this->assertNotNull($this->setImageLoader->find('123213.png'));
}
public function testAPI()
{
$this->assertNotNull($this->setImageLoader->find('4488-1.jpg'));
}
/**
* @expectedException \Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException
*/
public function testException()
{
$bricksetManager = $this->createMock(BricksetManager::class);
$set = new Set();
$set->setImage(__DIR__.'/fixtures/1.png');
$bricksetManager->method('getSetByNumber')
->willReturn(null);
$this->setImageLoader = new SetImageLoader($bricksetManager, $this->filesystem);
$this->setImageLoader->find('-1/123213.png');
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB