diff --git a/src/FrontBundle/Controller/ModelController.php b/src/FrontBundle/Controller/ModelController.php index 786ed42..f246a7e 100644 --- a/src/FrontBundle/Controller/ModelController.php +++ b/src/FrontBundle/Controller/ModelController.php @@ -16,7 +16,6 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\ResponseHeaderBag; /** @@ -90,19 +89,9 @@ class ModelController extends Controller $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, ]); - - if ($request->isXmlHttpRequest()) { - $json = json_encode($template->getContent()); - $response = new Response($json, 200); - $response->headers->set('Content-Type', 'application/json'); - - return $response; - } - - return $template; } /** diff --git a/src/FrontBundle/Controller/PartController.php b/src/FrontBundle/Controller/PartController.php index 1f2e8f5..f8db192 100644 --- a/src/FrontBundle/Controller/PartController.php +++ b/src/FrontBundle/Controller/PartController.php @@ -8,7 +8,6 @@ use Knp\Component\Pager\Paginator; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; /** * Part controller. @@ -24,18 +23,10 @@ class PartController extends Controller */ public function detailAction(Part $part, SetService $setService) { - if ($part) { - if ($model = $part->getModel()) { - $this->redirectToRoute('model_detail', ['id' => $model->getId()]); - } - - return $this->render('part/detail.html.twig', [ - 'part' => $part, - 'setCount' => count($setService->getAllByPart($part)), - ]); - } - - return $this->render('error/error.html.twig'); + return $this->render('part/detail.html.twig', [ + 'part' => $part, + 'setCount' => count($setService->getAllByPart($part)), + ]); } /** @@ -51,18 +42,8 @@ class PartController extends Controller $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, ]); - - if ($request->isXmlHttpRequest()) { - $json = json_encode($template->getContent()); - $response = new Response($json, 200); - $response->headers->set('Content-Type', 'application/json'); - - return $response; - } - - return $template; } } diff --git a/src/FrontBundle/Controller/Brickset/SetController.php b/src/FrontBundle/Controller/Set/BricksetController.php similarity index 97% rename from src/FrontBundle/Controller/Brickset/SetController.php rename to src/FrontBundle/Controller/Set/BricksetController.php index cac0d34..317e42d 100644 --- a/src/FrontBundle/Controller/Brickset/SetController.php +++ b/src/FrontBundle/Controller/Set/BricksetController.php @@ -1,6 +1,6 @@ render('set/tabs/inventory.html.twig', [ + return $this->render('set/tabs/inventory.html.twig', [ 'inventorySets' => $setService->getAllSubSets($set), 'set' => $set, 'missing' => $setService->getParts($set, false, false), @@ -92,84 +100,44 @@ class SetController extends Controller 'missingCount' => $setService->getPartCount($set, false, 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") + * @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; - $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', [ + return $this->render('set/tabs/models.html.twig', [ 'set' => $set, - 'missing' => $missing, - 'models' => $models, + 'missing' => $setService->getParts($set, false, false), + '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") + * @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; - $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', [ + return $this->render('set/tabs/colors.html.twig', [ 'set' => $set, - 'colors' => $colors, - 'missing' => $missing, + 'colors' => $setService->getModelsGroupedByColor($set, false), + '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") + * @param Request $request + * @param Set $set + * @param ZipService $zipService + * @return BinaryFileResponse */ public function zipAction(Request $request, Set $set, ZipService $zipService) { diff --git a/src/FrontBundle/Form/Search/SetSearchType.php b/src/FrontBundle/Form/Search/SetSearchType.php index d516a78..78a5bf6 100644 --- a/src/FrontBundle/Form/Search/SetSearchType.php +++ b/src/FrontBundle/Form/Search/SetSearchType.php @@ -68,10 +68,7 @@ class SetSearchType extends AbstractType 'choice_label' => 'fullName', 'choice_translation_domain' => false, 'group_by' => function ($theme, $key, $index) { - /** @var Theme $theme */ - $parent = $theme->getParent(); - - return $parent ? $parent->getParent() ? $parent->getParent()->getName() : $parent->getName() : $theme->getName(); + return $theme->getGroup()->getName(); }, 'choice_value' => 'id', 'placeholder' => 'set.form.theme.all', diff --git a/src/FrontBundle/Imagine/PartImageLoader.php b/src/FrontBundle/Imagine/PartImageLoader.php index 5f14478..4254456 100644 --- a/src/FrontBundle/Imagine/PartImageLoader.php +++ b/src/FrontBundle/Imagine/PartImageLoader.php @@ -37,14 +37,10 @@ class PartImageLoader extends BaseImageLoader } // try to load image from rebrickable website - try { - if ($this->remoteFileExists($this->rebrickableContext.$path)) { - $context = stream_context_create(['http' => ['header' => 'Connection: close\r\n']]); + if ($this->remoteFileExists($this->rebrickableContext.$path)) { + $context = stream_context_create(['http' => ['header' => 'Connection: close\r\n']]); - 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); + return file_get_contents($this->rebrickableContext.strtolower($path), false, $context); } // 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)); -// return $this->mediaFilesystem->read('noimage.png'); } } diff --git a/src/FrontBundle/Imagine/SetImageLoader.php b/src/FrontBundle/Imagine/SetImageLoader.php index bfe9057..7766a55 100644 --- a/src/FrontBundle/Imagine/SetImageLoader.php +++ b/src/FrontBundle/Imagine/SetImageLoader.php @@ -32,14 +32,10 @@ class SetImageLoader extends BaseImageLoader public function find($path) { // try to load image from rebrickable website - try { - if ($this->remoteFileExists($this->rebrickableContext.strtolower($path))) { - $context = stream_context_create(['http' => ['header' => 'Connection: close\r\n']]); + if ($this->remoteFileExists($this->rebrickableContext.strtolower($path))) { + $context = stream_context_create(['http' => ['header' => 'Connection: close\r\n']]); - 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); + return file_get_contents($this->rebrickableContext.strtolower($path), false, $context); } // 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)); -// return $this->mediaFilesystem->read('noimage.png'); } } diff --git a/src/FrontBundle/Twig/AppExtension.php b/src/FrontBundle/Twig/AppExtension.php index db67b43..d19f2b4 100644 --- a/src/FrontBundle/Twig/AppExtension.php +++ b/src/FrontBundle/Twig/AppExtension.php @@ -35,7 +35,6 @@ class AppExtension extends \Twig_Extension return [ new \Twig_SimpleFunction('remoteSize', [$this, 'remoteSize']), new \Twig_SimpleFunction('remoteFilename', [$this, 'remoteFilename']), - new \Twig_SimpleFunction('remoteFileExists', [$this, 'remoteFileExists']), new \Twig_SimpleFunction('fileTimestamp', [$this, 'fileTimestamp']), ]; } @@ -45,22 +44,6 @@ class AppExtension extends \Twig_Extension 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) { $ch = curl_init($url); diff --git a/tests/FrontBundle/Controller/ApplicationAvailabilityTest.php b/tests/FrontBundle/Controller/ApplicationAvailabilityTest.php index 3736085..13901d3 100644 --- a/tests/FrontBundle/Controller/ApplicationAvailabilityTest.php +++ b/tests/FrontBundle/Controller/ApplicationAvailabilityTest.php @@ -9,28 +9,38 @@ class ApplicationAvailabilityTest extends BaseControllerTest /** * @dataProvider urlProvider */ - public function testPageIsSuccessful($url) { + public function testPageIsSuccessful($url) + { $client = static::createClient(); $client->request('GET', $url); - $this->assertTrue( $client->getResponse()->isSuccessful()); + $this->assertTrue($client->getResponse()->isSuccessful()); } public function urlProvider() { - return array( - array('/'), - array('/colors/'), - array('/bricks/'), - array('/bricks/1'), - array('/bricks/1/sets'), - array('/sets/'), - array('/sets/8049-1'), - array('/sets/8049-1/inventory'), - array('/sets/8049-1/models'), - array('/sets/8049-1/colors'), - array('/parts/1') - ); + return [ + ['/'], + ['/colors/'], + ['/bricks/'], + ['/bricks/1'], + ['/bricks/1/zip'], + ['/bricks/1/sets'], + ['/sets/'], + ['/sets/?query=name&partCount[from]=620'], + ['/sets/8049-1'], + ['/sets/8049-1/zip'], + ['/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'], + ]; } -} \ No newline at end of file +} diff --git a/tests/FrontBundle/Controller/BaseControllerTest.php b/tests/FrontBundle/Controller/BaseControllerTest.php index 7f6a2e1..daebf49 100644 --- a/tests/FrontBundle/Controller/BaseControllerTest.php +++ b/tests/FrontBundle/Controller/BaseControllerTest.php @@ -2,18 +2,22 @@ namespace Tests\FrontBundle\Controller; -use Liip\FunctionalTestBundle\Test\WebTestCase; +use Tests\AppBundle\BaseTest; use Tests\AppBundle\Fixtures\LoadBaseData; -abstract class BaseControllerTest extends WebTestCase +abstract class BaseControllerTest extends BaseTest { public function setUp() { + parent::setUp(); + // If you are using the Doctrine Fixtures Bundle you could load these here $this->loadFixtures([ - LoadBaseData::class + LoadBaseData::class, ]); + $this->filesystem->write('models/1.stl', 'abcd'); + $this->runCommand('fos:elastica:populate'); } -} \ No newline at end of file +} diff --git a/tests/FrontBundle/Controller/fixtures/samplefile.txt b/tests/FrontBundle/Controller/fixtures/samplefile.txt new file mode 100644 index 0000000..754bb84 --- /dev/null +++ b/tests/FrontBundle/Controller/fixtures/samplefile.txt @@ -0,0 +1 @@ +file contents \ No newline at end of file diff --git a/tests/FrontBundle/Imagine/PartImageLoaderTest.php b/tests/FrontBundle/Imagine/PartImageLoaderTest.php new file mode 100644 index 0000000..0218980 --- /dev/null +++ b/tests/FrontBundle/Imagine/PartImageLoaderTest.php @@ -0,0 +1,67 @@ +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'); + } +} diff --git a/tests/FrontBundle/Imagine/SetImageLoaderTest.php b/tests/FrontBundle/Imagine/SetImageLoaderTest.php new file mode 100644 index 0000000..01ca0f4 --- /dev/null +++ b/tests/FrontBundle/Imagine/SetImageLoaderTest.php @@ -0,0 +1,65 @@ +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'); + } +} diff --git a/tests/FrontBundle/Imagine/fixtures/1.png b/tests/FrontBundle/Imagine/fixtures/1.png new file mode 100644 index 0000000..d065739 Binary files /dev/null and b/tests/FrontBundle/Imagine/fixtures/1.png differ