From 709e069f026ca082a71f37580d89f85efdbd5262 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 23 Jun 2017 22:21:40 +0200 Subject: [PATCH] Add AppBundle tests --- phpunit.xml.dist | 2 + .../Api/Client/Brickset/BricksetClient.php | 2 + src/AppBundle/Entity/Rebrickable/Set.php | 6 +- src/AppBundle/Entity/Rebrickable/Theme.php | 10 ++ src/AppBundle/Repository/ColorRepository.php | 19 --- .../Repository/LDraw/ModelRepository.php | 67 ++------- .../Repository/LDraw/SubpartRepository.php | 7 +- .../Rebrickable/Inventory_PartRepository.php | 21 --- .../Repository/Rebrickable/PartRepository.php | 32 +---- .../Repository/Rebrickable/SetRepository.php | 10 -- .../Rebrickable/ThemeRepository.php | 18 --- src/AppBundle/Service/ModelService.php | 2 +- tests/AppBundle/BaseTest.php | 22 +-- tests/AppBundle/Fixtures/LoadBaseData.php | 89 ++++++++++-- tests/AppBundle/Fixtures/LoadUnmappedData.php | 135 ++++++++++++++++++ .../Repository/LDraw/AliasRepositoryTest.php | 39 +++++ .../Repository/LDraw/AuthorRepositoryTest.php | 35 +++++ .../LDraw/CategoryRepositoryTest.php | 35 +++++ .../LDraw/KeywordRepositoryTest.php | 35 +++++ .../Repository/LDraw/ModelRepositoryTest.php | 66 +++++++++ .../LDraw/SubpartRepositoryTest.php | 45 ++++++ tests/AppBundle/Service/ZipServiceTest.php | 13 +- .../Transformer/FormatTransformerTest.php | 11 +- 23 files changed, 522 insertions(+), 199 deletions(-) create mode 100644 tests/AppBundle/Fixtures/LoadUnmappedData.php create mode 100644 tests/AppBundle/Repository/LDraw/AliasRepositoryTest.php create mode 100644 tests/AppBundle/Repository/LDraw/AuthorRepositoryTest.php create mode 100644 tests/AppBundle/Repository/LDraw/CategoryRepositoryTest.php create mode 100644 tests/AppBundle/Repository/LDraw/KeywordRepositoryTest.php create mode 100644 tests/AppBundle/Repository/LDraw/ModelRepositoryTest.php create mode 100644 tests/AppBundle/Repository/LDraw/SubpartRepositoryTest.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 65e9082..227f3e4 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -23,6 +23,8 @@ src src/*Bundle/Resources + src/*Bundle/Api/Client + src/*Bundle/DataFixtures src/*/*Bundle/Resources src/*/Bundle/*Bundle/Resources diff --git a/src/AppBundle/Api/Client/Brickset/BricksetClient.php b/src/AppBundle/Api/Client/Brickset/BricksetClient.php index 192ce49..ce53a99 100644 --- a/src/AppBundle/Api/Client/Brickset/BricksetClient.php +++ b/src/AppBundle/Api/Client/Brickset/BricksetClient.php @@ -111,6 +111,8 @@ class BricksetClient return null; } catch (\SoapFault $e) { throw new CallFailedException(ApiException::BRICKSET); + } catch (AuthenticationFailedException $e) { + throw $e; } catch (\Exception $e) { throw new ApiException(ApiException::BRICKSET); } diff --git a/src/AppBundle/Entity/Rebrickable/Set.php b/src/AppBundle/Entity/Rebrickable/Set.php index 27de06d..0a18f97 100644 --- a/src/AppBundle/Entity/Rebrickable/Set.php +++ b/src/AppBundle/Entity/Rebrickable/Set.php @@ -150,11 +150,11 @@ class Set } /** - * @param Collection $inventorySets + * @param Inventory_Set $inventorySet */ - public function setInventorySets($inventorySets) + public function addInventorySet($inventorySet) { - $this->inventorySets = $inventorySets; + $this->inventorySets->add($inventorySet); } /** diff --git a/src/AppBundle/Entity/Rebrickable/Theme.php b/src/AppBundle/Entity/Rebrickable/Theme.php index 6f0626f..2399bfe 100644 --- a/src/AppBundle/Entity/Rebrickable/Theme.php +++ b/src/AppBundle/Entity/Rebrickable/Theme.php @@ -92,4 +92,14 @@ class Theme return implode(' > ', array_reverse($name)); } + + public function getGroup() + { + $theme = $this; + while (($theme->getParent()) !== null) { + $theme = $theme->getParent(); + } + + return $theme; + } } diff --git a/src/AppBundle/Repository/ColorRepository.php b/src/AppBundle/Repository/ColorRepository.php index 49cb141..4200578 100644 --- a/src/AppBundle/Repository/ColorRepository.php +++ b/src/AppBundle/Repository/ColorRepository.php @@ -2,25 +2,6 @@ namespace AppBundle\Repository; -use AppBundle\Entity\Rebrickable\Inventory; -use AppBundle\Entity\Rebrickable\Inventory_Part; -use AppBundle\Entity\Rebrickable\Set; -use Doctrine\ORM\Query\Expr\Join; - class ColorRepository extends BaseRepository { - public function findAllBySet(Set $set) - { - $inventory = $this->getEntityManager()->getRepository(Inventory::class)->findNewestInventoryBySetNumber($set->getId()); - - $queryBuilder = $this->createQueryBuilder('color'); - - $queryBuilder - ->join(Inventory_Part::class, 'inventory_part', Join::WITH, 'inventory_part.color = color.id') - ->where('inventory_part.inventory = :inventory') - ->setParameter('inventory', $inventory) - ->distinct(true); - - return $queryBuilder->getQuery()->getResult(); - } } diff --git a/src/AppBundle/Repository/LDraw/ModelRepository.php b/src/AppBundle/Repository/LDraw/ModelRepository.php index 8f700db..d4604e7 100644 --- a/src/AppBundle/Repository/LDraw/ModelRepository.php +++ b/src/AppBundle/Repository/LDraw/ModelRepository.php @@ -3,36 +3,20 @@ namespace AppBundle\Repository\LDraw; use AppBundle\Entity\LDraw\Alias; -use AppBundle\Entity\LDraw\Category; use AppBundle\Entity\LDraw\Model; use AppBundle\Entity\LDraw\Subpart; -use AppBundle\Entity\Rebrickable\Inventory; -use AppBundle\Entity\Rebrickable\Inventory_Part; -use AppBundle\Entity\Rebrickable\Part; -use AppBundle\Entity\Rebrickable\Set; use AppBundle\Repository\BaseRepository; use Doctrine\ORM\Query\Expr\Join; class ModelRepository extends BaseRepository { - public function getFilteredQueryBuilder() - { - $queryBuilder = $this->createQueryBuilder('model') - ->where('model.name NOT LIKE :obsolete') - ->setParameter('obsolete', '~%'); - - return $queryBuilder; - } - - public function findAllByCategory($category) - { - $queryBuilder = $this->createQueryBuilder('model') - ->join(Category::class, 'type', Join::LEFT_JOIN, 'model.category = :category') - ->setParameter('category', $category); - - return $queryBuilder->getQuery(); - } - + /** + * Find model by id or alias id. + * + * @param $number + * + * @return mixed + */ public function findOneByNumber($number) { $model = $this->createQueryBuilder('model') @@ -56,40 +40,7 @@ class ModelRepository extends BaseRepository return $this->findOneBy(['name' => $name]); } - public function findAllRegularBySetNumber($number) - { - $inventory = $this->getEntityManager()->getRepository(Inventory::class)->findNewestInventoryBySetNumber($number); - - $queryBuilder = $this->createQueryBuilder('model'); - - $queryBuilder - ->join(Part::class, 'part', JOIN::WITH, 'part.model = model') - ->join(Inventory_Part::class, 'inventory_part', JOIN::WITH, 'part.id = inventory_part.part') - ->join(Inventory::class, 'inventory', JOIN::WITH, 'inventory_part.inventory = :inventory') - ->setParameter('inventory', $inventory) - ->addSelect('inventory_part') - ->distinct(true); - - return $queryBuilder->getQuery()->getScalarResult(); - } - - public function findAllBySetNumber($number) - { - $queryBuilder = $this->createQueryBuilder('model'); - - $queryBuilder - ->join(Part::class, 'part', JOIN::WITH, 'part.model = model') - ->join(Inventory_Part::class, 'inventory_part', JOIN::WITH, 'part.id = inventory_part.part') - ->join(Inventory::class, 'inventory', JOIN::WITH, 'inventory_part.inventory = inventory.id') - ->join(Set::class, 's', Join::WITH, 'inventory.set = s.id') - ->where('s.id LIKE :number') - ->setParameter('number', $number) - ->distinct(true); - - return $queryBuilder->getQuery()->getResult(); - } - - public function findAllRelatedModels(Model $model) + public function findAllSiblings(Model $model) { $queryBuilder = $this->createQueryBuilder('model'); @@ -123,7 +74,7 @@ class ModelRepository extends BaseRepository */ public function getOrCreate($id) { - if (($model = $this->findOneBy(['id' => $id])) == null) { + if (($model = $this->findOneByNumber($id)) == null) { $model = new Model(); $model->setId($id); } diff --git a/src/AppBundle/Repository/LDraw/SubpartRepository.php b/src/AppBundle/Repository/LDraw/SubpartRepository.php index c369d20..99a6036 100644 --- a/src/AppBundle/Repository/LDraw/SubpartRepository.php +++ b/src/AppBundle/Repository/LDraw/SubpartRepository.php @@ -8,11 +8,6 @@ use AppBundle\Repository\BaseRepository; class SubpartRepository extends BaseRepository { - public function findOneByKeys($parent, $child, $color) - { - return $this->find(['parent' => $parent, 'subpart' => $child, 'color' => $color]); - } - /** * Create new Subpart relation entity or retrieve one by foreign keys. * @@ -25,7 +20,7 @@ class SubpartRepository extends BaseRepository */ public function getOrCreate($parent, $child, $count, $colorId) { - if (($subpart = $this->findOneByKeys($parent, $child, $colorId))) { + if (($subpart = $this->find(['parent' => $parent, 'subpart' => $child, 'color' => $colorId]))) { $subpart->setCount($count); } else { $subpart = new Subpart(); diff --git a/src/AppBundle/Repository/Rebrickable/Inventory_PartRepository.php b/src/AppBundle/Repository/Rebrickable/Inventory_PartRepository.php index 240fbc5..cac0aae 100644 --- a/src/AppBundle/Repository/Rebrickable/Inventory_PartRepository.php +++ b/src/AppBundle/Repository/Rebrickable/Inventory_PartRepository.php @@ -48,27 +48,6 @@ class Inventory_PartRepository extends BaseRepository return $queryBuilder->getQuery()->getResult(); } - /** - * Find all Inventory_Parts with $color in set. - * - * @param string $number - * @param int $color - * - * @return array - */ - public function findAllBySetNumberAndColor($number, $color) - { - $inventory = $this->getEntityManager()->getRepository(Inventory::class)->findNewestInventoryBySetNumber($number); - - $queryBuilder = $this->createQueryBuilder('inventory_part') - ->where('inventory_part.inventory = :inventory') - ->setParameter('inventory', $inventory) - ->andWhere('inventory_part.color = :color') - ->setParameter('color', $color); - - return $queryBuilder->getQuery()->getResult(); - } - /** * Get total part count of Set. * diff --git a/src/AppBundle/Repository/Rebrickable/PartRepository.php b/src/AppBundle/Repository/Rebrickable/PartRepository.php index 42cf4bb..25832fa 100644 --- a/src/AppBundle/Repository/Rebrickable/PartRepository.php +++ b/src/AppBundle/Repository/Rebrickable/PartRepository.php @@ -4,46 +4,16 @@ namespace AppBundle\Repository\Rebrickable; use AppBundle\Entity\LDraw\Model; use AppBundle\Entity\Rebrickable\Category; -use AppBundle\Entity\Rebrickable\Inventory; -use AppBundle\Entity\Rebrickable\Inventory_Part; use AppBundle\Repository\BaseRepository; use Doctrine\ORM\Query\Expr\Join; class PartRepository extends BaseRepository { - public function findAllByModel(Model $model) - { - $queryBuilder = $this->createQueryBuilder('part'); - - $queryBuilder - ->where('part.model = :model') - ->setParameter('model', $model); - - return $queryBuilder->getQuery()->getResult(); - } - public function findAllNotPaired() { $queryBuilder = $this->createQueryBuilder('part') ->leftJoin(Category::class, 'category', JOIN::WITH, 'part.category = category.id') - ->where('category.name NOT LIKE :categoryName') - ->andWhere('part.model IS NULL') - ->setParameter('categoryName', 'Non-LEGO') - ->distinct(true); - - return $queryBuilder->getQuery()->getResult(); - } - - public function findAllBySetNumber($number) - { - $queryBuilder = $this->createQueryBuilder('part'); - - $inventory = $this->getEntityManager()->getRepository(Inventory::class)->findNewestInventoryBySetNumber($number); - - $queryBuilder - ->join(Inventory_Part::class, 'inventory_part', JOIN::WITH, 'part.number = inventory_part.part') - ->where('inventory_part.inventory = :inventory') - ->setParameter('inventory', $inventory) + ->where('part.model IS NULL') ->distinct(true); return $queryBuilder->getQuery()->getResult(); diff --git a/src/AppBundle/Repository/Rebrickable/SetRepository.php b/src/AppBundle/Repository/Rebrickable/SetRepository.php index d6dace9..760ca09 100644 --- a/src/AppBundle/Repository/Rebrickable/SetRepository.php +++ b/src/AppBundle/Repository/Rebrickable/SetRepository.php @@ -13,16 +13,6 @@ use Doctrine\ORM\Query\Expr\Join; class SetRepository extends BaseRepository { - public function findAllByTheme(Theme $theme) - { - $queryBuilder = $this->createQueryBuilder('s') - ->join(Theme::class, 'theme', Join::WITH, 's.theme = theme') - ->where('theme.id = :id') - ->setParameter('id', $theme->getId()); - - return $queryBuilder->getQuery()->getResult(); - } - public function findAllByPart(Part $part) { $queryBuilder = $this->createQueryBuilder('s') diff --git a/src/AppBundle/Repository/Rebrickable/ThemeRepository.php b/src/AppBundle/Repository/Rebrickable/ThemeRepository.php index 24d8436..11b0d88 100644 --- a/src/AppBundle/Repository/Rebrickable/ThemeRepository.php +++ b/src/AppBundle/Repository/Rebrickable/ThemeRepository.php @@ -2,26 +2,8 @@ namespace AppBundle\Repository\Rebrickable; -use AppBundle\Entity\Rebrickable\Theme; use AppBundle\Repository\BaseRepository; class ThemeRepository extends BaseRepository { - public function findAllSubthemes(Theme $theme) - { - $queryBuilder = $this->createQueryBuilder('theme') - ->where('theme.parent = :id') - ->setParameter('id', $theme->getId()); - - return $queryBuilder->getQuery()->getResult(); - } - - public function findAllMain() - { - $queryBuilder = $this->createQueryBuilder('theme') - ->where('theme.parent IS NULL') - ->orderBy('theme.name', 'ASC'); - - return $queryBuilder->getQuery()->getResult(); - } } diff --git a/src/AppBundle/Service/ModelService.php b/src/AppBundle/Service/ModelService.php index 511496e..75c7bfa 100644 --- a/src/AppBundle/Service/ModelService.php +++ b/src/AppBundle/Service/ModelService.php @@ -60,7 +60,7 @@ class ModelService */ public function getSiblings(Model $model) { - return $this->modelRepository->findAllRelatedModels($model); + return $this->modelRepository->findAllSiblings($model); } /** diff --git a/tests/AppBundle/BaseTest.php b/tests/AppBundle/BaseTest.php index 0304a9a..86ed24f 100644 --- a/tests/AppBundle/BaseTest.php +++ b/tests/AppBundle/BaseTest.php @@ -2,14 +2,9 @@ namespace Tests\AppBundle; -use AppBundle\DataFixtures\ORM\LoadColors; -use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\Tools\SchemaTool; use League\Flysystem\FilesystemInterface; use Liip\FunctionalTestBundle\Test\WebTestCase; -use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; -use Symfony\Component\HttpKernel\KernelInterface; abstract class BaseTest extends WebTestCase { @@ -25,7 +20,7 @@ abstract class BaseTest extends WebTestCase $this->em = $this->get('doctrine.orm.entity_manager'); } - public function setUpDb() + public function setUpDb(array $fixtures) { // Make sure we are in the test environment if ('test' !== $this->get('kernel')->getEnvironment()) { @@ -33,9 +28,7 @@ abstract class BaseTest extends WebTestCase } // If you are using the Doctrine Fixtures Bundle you could load these here - $this->loadFixtures([ - LoadColors::class - ]); + $this->loadFixtures($fixtures); } protected function get($service) @@ -47,4 +40,15 @@ abstract class BaseTest extends WebTestCase { return $this->getContainer()->getParameter($parameter); } + + protected function tearDown() + { + parent::tearDown(); + + $this->filesystem->deleteDir('models'); + $this->filesystem->deleteDir('images'); + + $this->em->close(); + $this->em = null; // avoid memory leaks + } } diff --git a/tests/AppBundle/Fixtures/LoadBaseData.php b/tests/AppBundle/Fixtures/LoadBaseData.php index a9c373b..4bef18e 100644 --- a/tests/AppBundle/Fixtures/LoadBaseData.php +++ b/tests/AppBundle/Fixtures/LoadBaseData.php @@ -3,12 +3,15 @@ namespace Tests\AppBundle\Fixtures; use AppBundle\Entity\Color; +use AppBundle\Entity\LDraw\Alias; use AppBundle\Entity\LDraw\Author; use AppBundle\Entity\LDraw\Model; +use AppBundle\Entity\LDraw\Subpart; use AppBundle\Entity\Rebrickable\Inventory; use AppBundle\Entity\Rebrickable\Inventory_Part; use AppBundle\Entity\Rebrickable\Part; use AppBundle\Entity\Rebrickable\Set; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\DataFixtures\FixtureInterface; use Doctrine\Common\Persistence\ObjectManager; use Symfony\Component\DependencyInjection\ContainerAwareInterface; @@ -28,6 +31,27 @@ class LoadBaseData implements FixtureInterface, ContainerAwareInterface public function load(ObjectManager $manager) { + $color = new Color(); + $color->setId(1); + $color->setName('Black'); + $color->setRgb('000000'); + $color->setTransparent(false); + $manager->persist($color); + + $color2 = new Color(); + $color2->setId(2); + $color2->setName('Blue'); + $color2->setRgb('EEEEEE'); + $color2->setTransparent(false); + $manager->persist($color2); + + $color2 = new Color(); + $color2->setId(-1); + $color2->setName('Unknown'); + $color2->setRgb('EEEEEE'); + $color2->setTransparent(false); + $manager->persist($color2); + // Add sample author $author = new Author(); $author->setName('Author'); @@ -42,6 +66,52 @@ class LoadBaseData implements FixtureInterface, ContainerAwareInterface $model->setPath('models/1.stl'); $manager->persist($model); + // Add sample model + $child = new Model(); + $child->setId(2); + $child->setAuthor($author); + $child->setModified(new \DateTime()); + $child->setName('Name2'); + $child->setPath('models/1.stl'); + $manager->persist($child); + + // Add sample model + $child2 = new Model(); + $child2->setId(3); + $child2->setAuthor($author); + $child2->setModified(new \DateTime()); + $child2->setName('Name2'); + $child2->setPath('models/1.stl'); + $manager->persist($child); + + $subpart = new Subpart(); + $subpart->setParent($model); + $subpart->setSubpart($child); + $subpart->setCount(2); + $subpart->setColor($color); + $model->addSubpart($subpart); + + $subpart2 = new Subpart(); + $subpart2->setParent($model); + $subpart2->setSubpart($child2); + $subpart2->setCount(2); + $subpart2->setColor($color); + + $model->addSubpart($subpart2); + $manager->persist($model); + + // Add sample model + $alias = new Alias(); + $alias->setId('2d'); + $alias->setModel($model); + $manager->persist($alias); + + // Add sample model + $alias = new Alias(); + $alias->setId('25'); + $alias->setModel($model); + $manager->persist($alias); + // Add sample part $part = new Part(); $part->setId(1); @@ -49,24 +119,25 @@ class LoadBaseData implements FixtureInterface, ContainerAwareInterface $part->setModel($model); $manager->persist($part); + // Add sample part + $part = new Part(); + $part->setId(2); + $part->setName('Name2'); + $manager->persist($part); + $set = new Set(); - $set->setName('Set'); + $set->setName('Set name'); $set->setId('8049-1'); $set->setPartCount(1); $set->setYear(2011); $manager->persist($set); - $color = new Color(); - $color->setId(1); - $color->setName('Black'); - $color->setRgb('000000'); - $color->setTransparent(false); - $manager->persist($color); - $inventory = new Inventory(); $inventory->setSet($set); $inventory->setVersion(1); + $set->addInventory($inventory); $manager->persist($inventory); + $manager->persist($set); $inventoryPart = new Inventory_Part(); $inventoryPart->setColor($color); @@ -78,4 +149,4 @@ class LoadBaseData implements FixtureInterface, ContainerAwareInterface $manager->flush(); } -} \ No newline at end of file +} diff --git a/tests/AppBundle/Fixtures/LoadUnmappedData.php b/tests/AppBundle/Fixtures/LoadUnmappedData.php new file mode 100644 index 0000000..8c6d9ef --- /dev/null +++ b/tests/AppBundle/Fixtures/LoadUnmappedData.php @@ -0,0 +1,135 @@ +container = $container; +// } + + public function load(ObjectManager $manager) + { + // Add sample author + $author = new Author(); + $author->setName('Author'); + $manager->persist($author); + + // Add sample model + $model = new Model(); + $model->setId('930'); + $model->setAuthor($author); + $model->setModified(new \DateTime()); + $model->setName('Name'); + $model->setPath('models/1.stl'); + $manager->persist($model); + + // Add sample model + $model = new Model(); + $model->setId('973c00'); + $model->setAuthor($author); + $model->setModified(new \DateTime()); + $model->setName('ModelName'); + $model->setPath('models/1.stl'); + $manager->persist($model); + + // Add sample model + $model = new Model(); + $model->setId('970c00'); + $model->setAuthor($author); + $model->setModified(new \DateTime()); + $model->setName('Name 3'); + $model->setPath('models/1.stl'); + $manager->persist($model); + + // Add sample part + $part = new Part(); + $part->setId('930'); + $part->setName('Name'); + $manager->persist($part); + + // Add sample part + $part = new Part(); + $part->setId('930p01'); + $part->setName('Part2'); + $manager->persist($part); + + // Add sample part + $part = new Part(); + $part->setId('930pr002'); + $part->setName('Part2'); + $manager->persist($part); + + // Add sample part + $part = new Part(); + $part->setId('930pb001a'); + $part->setName('Part2'); + $part->setModel($model); + $manager->persist($part); + + // Add sample part + $part = new Part(); + $part->setId('973c05'); + $part->setName('Part2'); + $manager->persist($part); + + // Add sample part + $part = new Part(); + $part->setId('1235'); + $part->setName('ModelName'); + $manager->persist($part); + + // Add sample part + $part = new Part(); + $part->setId('970c52'); + $part->setName('part'); + $manager->persist($part); + + $set = new Set(); + $set->setName('Set'); + $set->setId('8049-1'); + $set->setPartCount(1); + $set->setYear(2011); + $manager->persist($set); + + $color = new Color(); + $color->setId(1); + $color->setName('Black'); + $color->setRgb('000000'); + $color->setTransparent(false); + $manager->persist($color); + + $inventory = new Inventory(); + $inventory->setSet($set); + $inventory->setVersion(1); + $manager->persist($inventory); + + $inventoryPart = new Inventory_Part(); + $inventoryPart->setColor($color); + $inventoryPart->setQuantity(5); + $inventoryPart->setPart($part); + $inventoryPart->setInventory($inventory); + $inventoryPart->setSpare(false); + $manager->persist($inventoryPart); + + $manager->flush(); + } +} diff --git a/tests/AppBundle/Repository/LDraw/AliasRepositoryTest.php b/tests/AppBundle/Repository/LDraw/AliasRepositoryTest.php new file mode 100644 index 0000000..1e16527 --- /dev/null +++ b/tests/AppBundle/Repository/LDraw/AliasRepositoryTest.php @@ -0,0 +1,39 @@ +setUpDb([LoadBaseData::class]); + + $this->repository = $this->em->getRepository(Alias::class); + } + + public function testGetOrCreate() + { + $this->assertCount(2, $this->repository->findAll()); + + /** @var Model $model */ + $model = $this->em->getRepository(Model::class)->find(1); + + $alias = $this->repository->getOrCreate(25, $model); + $this->repository->save($alias); + $this->assertCount(2, $this->repository->findAll()); + + $alias = $this->repository->getOrCreate(33, $model); + $this->repository->save($alias); + $this->assertCount(3, $this->repository->findAll()); + } +} diff --git a/tests/AppBundle/Repository/LDraw/AuthorRepositoryTest.php b/tests/AppBundle/Repository/LDraw/AuthorRepositoryTest.php new file mode 100644 index 0000000..769cdbd --- /dev/null +++ b/tests/AppBundle/Repository/LDraw/AuthorRepositoryTest.php @@ -0,0 +1,35 @@ +setUpDb([LoadBaseData::class]); + + $this->repository = $this->em->getRepository(Author::class); + } + + public function testGetOrCreate() + { + $this->assertCount(1, $this->repository->findAll()); + + $author = $this->repository->getOrCreate('Author'); + $this->repository->save($author); + $this->assertCount(1, $this->repository->findAll()); + + $author = $this->repository->getOrCreate('Author2'); + $this->repository->save($author); + $this->assertCount(2, $this->repository->findAll()); + } +} diff --git a/tests/AppBundle/Repository/LDraw/CategoryRepositoryTest.php b/tests/AppBundle/Repository/LDraw/CategoryRepositoryTest.php new file mode 100644 index 0000000..b82eb76 --- /dev/null +++ b/tests/AppBundle/Repository/LDraw/CategoryRepositoryTest.php @@ -0,0 +1,35 @@ +setUpDb([LoadBaseData::class]); + + $this->repository = $this->em->getRepository(Category::class); + } + + public function testGetOrCreate() + { + $this->assertCount(0, $this->repository->findAll()); + + $category = $this->repository->getOrCreate('Category'); + $this->repository->save($category); + $this->assertCount(1, $this->repository->findAll()); + + $category = $this->repository->getOrCreate('Category'); + $this->repository->save($category); + $this->assertCount(1, $this->repository->findAll()); + } +} diff --git a/tests/AppBundle/Repository/LDraw/KeywordRepositoryTest.php b/tests/AppBundle/Repository/LDraw/KeywordRepositoryTest.php new file mode 100644 index 0000000..c4fb291 --- /dev/null +++ b/tests/AppBundle/Repository/LDraw/KeywordRepositoryTest.php @@ -0,0 +1,35 @@ +setUpDb([LoadBaseData::class]); + + $this->repository = $this->em->getRepository(Keyword::class); + } + + public function testGetOrCreate() + { + $this->assertCount(0, $this->repository->findAll()); + + $keyword = $this->repository->getOrCreate('Keyword'); + $this->repository->save($keyword); + $this->assertCount(1, $this->repository->findAll()); + + $keyword = $this->repository->getOrCreate('Keyword'); + $this->repository->save($keyword); + $this->assertCount(1, $this->repository->findAll()); + } +} diff --git a/tests/AppBundle/Repository/LDraw/ModelRepositoryTest.php b/tests/AppBundle/Repository/LDraw/ModelRepositoryTest.php new file mode 100644 index 0000000..9625155 --- /dev/null +++ b/tests/AppBundle/Repository/LDraw/ModelRepositoryTest.php @@ -0,0 +1,66 @@ +setUpDb([LoadBaseData::class]); + + $this->repository = $this->em->getRepository(Model::class); + } + + public function testFindOneByNumber() + { + $model = $this->repository->findOneByNumber('25'); + $this->assertEquals(1, $model->getId()); + + $model = $this->repository->findOneByNumber(1); + $this->assertEquals(1, $model->getId()); + } + + public function testFindOneByName() + { + $model = $this->repository->findOneByName('Name'); + $this->assertEquals(1, $model->getId()); + + $model = $this->repository->findOneByName('Not'); + $this->assertNull($model); + } + + public function testFindAllSiblings() + { + $model = $this->repository->findOneByNumber(2); + $siblings = $this->repository->findAllSiblings($model); + + $this->assertCount(1, $siblings); + } + + public function testCount() + { + $this->assertEquals(3, $this->repository->count()); + } + + public function testGetOrCreate() + { + $this->assertCount(3, $this->repository->findAll()); + + $model = $this->repository->getOrCreate('25'); + $this->repository->save($model); + $this->assertCount(3, $this->repository->findAll()); + + $model = $this->repository->getOrCreate(33); + $this->repository->save($model); + $this->assertCount(4, $this->repository->findAll()); + } +} diff --git a/tests/AppBundle/Repository/LDraw/SubpartRepositoryTest.php b/tests/AppBundle/Repository/LDraw/SubpartRepositoryTest.php new file mode 100644 index 0000000..584b914 --- /dev/null +++ b/tests/AppBundle/Repository/LDraw/SubpartRepositoryTest.php @@ -0,0 +1,45 @@ +setUpDb([LoadBaseData::class]); + + $this->repository = $this->em->getRepository(Subpart::class); + } + + public function testGetOrCreate() + { + $this->assertCount(2, $this->repository->findAll()); + + /** @var Model $model */ + $model = $this->em->getRepository(Model::class)->find(1); + /** @var Model $child */ + $child = $this->em->getRepository(Model::class)->find(2); + + $subpart = $this->repository->getOrCreate($model, $child, 2, 1); + $this->repository->save($subpart); + $this->assertCount(2, $this->repository->findAll()); + + $subpart = $this->repository->getOrCreate($model, $child, 2, 2); + $this->repository->save($subpart); + $this->assertCount(3, $this->repository->findAll()); + + $subpart = $this->repository->getOrCreate($model, $child, 2, 3); + $this->repository->save($subpart); + $this->assertCount(4, $this->repository->findAll()); + } +} diff --git a/tests/AppBundle/Service/ZipServiceTest.php b/tests/AppBundle/Service/ZipServiceTest.php index 68e651e..e4a1565 100644 --- a/tests/AppBundle/Service/ZipServiceTest.php +++ b/tests/AppBundle/Service/ZipServiceTest.php @@ -24,20 +24,15 @@ class ZipServiceTest extends BaseTest parent::setUp(); $this->loadFixtures([ - LoadBaseData::class + LoadBaseData::class, ]); $this->modelService = new ModelService($this->em); $this->setService = new SetService($this->em); - $this->filesystem->write('models/1.stl',file_get_contents(__DIR__ . '/../Fixtures/models/1.stl')); + $this->filesystem->write('models/1.stl', file_get_contents(__DIR__.'/../Fixtures/models/1.stl')); - $this->zipService = new ZipService($this->filesystem,$this->modelService,$this->setService); - } - - public function tearDown() - { - $this->filesystem->delete('models/1.stl'); + $this->zipService = new ZipService($this->filesystem, $this->modelService, $this->setService); } public function testModelZip() @@ -57,4 +52,4 @@ class ZipServiceTest extends BaseTest $this->assertFileExists($path); } -} \ No newline at end of file +} diff --git a/tests/AppBundle/Transformer/FormatTransformerTest.php b/tests/AppBundle/Transformer/FormatTransformerTest.php index 3548355..7b6707f 100644 --- a/tests/AppBundle/Transformer/FormatTransformerTest.php +++ b/tests/AppBundle/Transformer/FormatTransformerTest.php @@ -17,12 +17,13 @@ class FormatTransformerTest extends TestCase $this->transformer = new FormatTransformer(); } - public function testBytesToSize() { - $this->assertEquals('1.5 MB', $this->transformer->bytesToSize(512*1024+1024*1024, 2)); + public function testBytesToSize() + { + $this->assertEquals('1.5 MB', $this->transformer->bytesToSize(512 * 1024 + 1024 * 1024, 2)); $this->assertEquals('512 B', $this->transformer->bytesToSize(512, 2)); $this->assertEquals('1 KB', $this->transformer->bytesToSize(1024, 2)); - $this->assertEquals('1 MB', $this->transformer->bytesToSize(1024*1024, 2)); - $this->assertEquals('1 GB', $this->transformer->bytesToSize(1024*1024*1024, 2)); - $this->assertEquals('1 TB', $this->transformer->bytesToSize(1024*1024*1024*1024, 2)); + $this->assertEquals('1 MB', $this->transformer->bytesToSize(1024 * 1024, 2)); + $this->assertEquals('1 GB', $this->transformer->bytesToSize(1024 * 1024 * 1024, 2)); + $this->assertEquals('1 TB', $this->transformer->bytesToSize(1024 * 1024 * 1024 * 1024, 2)); } }