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:
parent
9089929e66
commit
f2899fcf17
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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,20 +23,12 @@ 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');
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/{id}/sets", name="part_sets")
|
||||
*/
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace FrontBundle\Controller\Brickset;
|
||||
namespace FrontBundle\Controller\Set;
|
||||
|
||||
use AppBundle\Api\Exception\ApiException;
|
||||
use AppBundle\Api\Manager\BricksetManager;
|
||||
@ -11,9 +11,9 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
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")
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace FrontBundle\Controller;
|
||||
namespace FrontBundle\Controller\Set;
|
||||
|
||||
use AppBundle\Api\Exception\ApiException;
|
||||
use AppBundle\Api\Manager\BricksetManager;
|
||||
@ -29,6 +29,8 @@ class SetController extends Controller
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @param FormFactoryInterface $formFactory
|
||||
* @param SearchService $searchService
|
||||
* @return Response
|
||||
*/
|
||||
public function indexAction(Request $request, FormFactoryInterface $formFactory, SearchService $searchService)
|
||||
@ -56,6 +58,8 @@ class SetController extends Controller
|
||||
*
|
||||
* @param Set $set
|
||||
*
|
||||
* @param SetService $setService
|
||||
* @param BricksetManager $bricksetManager
|
||||
* @return Response
|
||||
*/
|
||||
public function detailAction(Set $set, SetService $setService, BricksetManager $bricksetManager)
|
||||
@ -81,10 +85,14 @@ class SetController extends Controller
|
||||
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
$template = $this->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)
|
||||
{
|
@ -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',
|
||||
|
@ -37,15 +37,11 @@ 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']]);
|
||||
|
||||
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
|
||||
try {
|
||||
@ -61,6 +57,5 @@ class PartImageLoader extends BaseImageLoader
|
||||
}
|
||||
|
||||
throw new NotLoadableException(sprintf('Source image %s not found.', $path));
|
||||
// return $this->mediaFilesystem->read('noimage.png');
|
||||
}
|
||||
}
|
||||
|
@ -32,15 +32,11 @@ 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']]);
|
||||
|
||||
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
|
||||
try {
|
||||
@ -56,6 +52,5 @@ class SetImageLoader extends BaseImageLoader
|
||||
}
|
||||
|
||||
throw new NotLoadableException(sprintf('Source image %s not found.', $path));
|
||||
// return $this->mediaFilesystem->read('noimage.png');
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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'],
|
||||
];
|
||||
}
|
||||
}
|
@ -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');
|
||||
}
|
||||
}
|
1
tests/FrontBundle/Controller/fixtures/samplefile.txt
Normal file
1
tests/FrontBundle/Controller/fixtures/samplefile.txt
Normal file
@ -0,0 +1 @@
|
||||
file contents
|
67
tests/FrontBundle/Imagine/PartImageLoaderTest.php
Normal file
67
tests/FrontBundle/Imagine/PartImageLoaderTest.php
Normal 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');
|
||||
}
|
||||
}
|
65
tests/FrontBundle/Imagine/SetImageLoaderTest.php
Normal file
65
tests/FrontBundle/Imagine/SetImageLoaderTest.php
Normal 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');
|
||||
}
|
||||
}
|
BIN
tests/FrontBundle/Imagine/fixtures/1.png
Normal file
BIN
tests/FrontBundle/Imagine/fixtures/1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 32 KiB |
Loading…
x
Reference in New Issue
Block a user