diff --git a/src/Serializer/BigNumberSerializer.php b/src/Serializer/BigNumberNormalizer.php similarity index 96% rename from src/Serializer/BigNumberSerializer.php rename to src/Serializer/BigNumberNormalizer.php index afdcaee6..fc352cf1 100644 --- a/src/Serializer/BigNumberSerializer.php +++ b/src/Serializer/BigNumberNormalizer.php @@ -26,7 +26,7 @@ use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -class BigNumberSerializer implements NormalizerInterface, CacheableSupportsMethodInterface +class BigNumberNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface { public function supportsNormalization($data, string $format = null): bool diff --git a/src/Serializer/PartNormalizer.php b/src/Serializer/PartNormalizer.php index 54d307fe..0a513283 100644 --- a/src/Serializer/PartNormalizer.php +++ b/src/Serializer/PartNormalizer.php @@ -40,7 +40,8 @@ class PartNormalizer implements NormalizerInterface, DenormalizerInterface, Cach 'amount' => 'instock', 'mpn' => 'manufacturer_product_number', 'spn' => 'supplier_part_number', - 'supplier_product_number' => 'supplier_part_number' + 'supplier_product_number' => 'supplier_part_number', + 'storage_location' => 'storelocation', ]; private ObjectNormalizer $normalizer; diff --git a/tests/ApplicationAvailabilityFunctionalTest.php b/tests/ApplicationAvailabilityFunctionalTest.php index 1fa08660..f13f173c 100644 --- a/tests/ApplicationAvailabilityFunctionalTest.php +++ b/tests/ApplicationAvailabilityFunctionalTest.php @@ -94,6 +94,9 @@ class ApplicationAvailabilityFunctionalTest extends WebTestCase yield ['/part/new']; yield ['/part/new?category=1&footprint=1&manufacturer=1&storelocation=1&supplier=1']; + //Parts import + yield ['/parts/import']; + //Statistics yield ['/statistics']; diff --git a/tests/Serializer/BigNumberNormalizerTest.php b/tests/Serializer/BigNumberNormalizerTest.php new file mode 100644 index 00000000..1c05dc19 --- /dev/null +++ b/tests/Serializer/BigNumberNormalizerTest.php @@ -0,0 +1,59 @@ +. + */ + +namespace App\Tests\Serializer; + +use App\Serializer\BigNumberNormalizer; +use PHPUnit\Framework\TestCase; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Brick\Math\BigDecimal; +use Brick\Math\BigNumber; + +class BigNumberNormalizerTest extends WebTestCase +{ + /** @var BigNumberNormalizer */ + protected $service; + + protected function setUp(): void + { + parent::setUp(); + //Get an service instance. + self::bootKernel(); + $this->service = self::getContainer()->get(BigNumberNormalizer::class); + } + + public function testNormalize() + { + $bigDecimal = BigDecimal::of('1.23456789'); + $this->assertSame('1.23456789', $this->service->normalize($bigDecimal)); + } + + public function testSupportsNormalization() + { + //Normalizer must only support BigNumber objects (and child classes) + $this->assertFalse($this->service->supportsNormalization(new \stdClass())); + + $bigNumber = BigNumber::of(1); + $this->assertTrue($this->service->supportsNormalization($bigNumber)); + + $bigDecimal = BigDecimal::of(1); + $this->assertTrue($this->service->supportsNormalization($bigDecimal)); + } +} diff --git a/tests/Serializer/PartNormalizerTest.php b/tests/Serializer/PartNormalizerTest.php new file mode 100644 index 00000000..21f86990 --- /dev/null +++ b/tests/Serializer/PartNormalizerTest.php @@ -0,0 +1,132 @@ +. + */ + +namespace App\Tests\Serializer; + +use App\Entity\Parts\Part; +use App\Entity\Parts\PartLot; +use App\Entity\PriceInformations\Orderdetail; +use App\Entity\PriceInformations\Pricedetail; +use App\Serializer\PartNormalizer; +use PHPUnit\Framework\TestCase; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; + +class PartNormalizerTest extends WebTestCase +{ + /** @var PartNormalizer */ + protected $service; + + protected function setUp(): void + { + parent::setUp(); + //Get an service instance. + self::bootKernel(); + $this->service = self::getContainer()->get(PartNormalizer::class); + } + + public function testSupportsNormalization() + { + //Normalizer must only support Part objects (and child classes) + $this->assertFalse($this->service->supportsNormalization(new \stdClass())); + $this->assertTrue($this->service->supportsNormalization(new Part())); + } + + public function testNormalize() + { + $part = new Part(); + $part->setName('Test Part'); + $partLot1 = new PartLot(); + $partLot1->setAmount(1); + $partLot2 = new PartLot(); + $partLot2->setAmount(5); + $part->addPartLot($partLot1); + $part->addPartLot($partLot2); + + $data = $this->service->normalize($part, 'json', ['groups' => ['simple']]); + $this->assertSame('Test Part', $data['name']); + $this->assertSame(6.0, $data['total_instock']); + $this->assertSame('part', $data['type']); + + //Check that type field is not present in CSV export + $data = $this->service->normalize($part, 'csv', ['groups' => ['simple']]); + $this->assertSame('Test Part', $data['name']); + $this->assertArrayNotHasKey('type', $data); + } + + public function testSupportsDenormalization() + { + //Normalizer must only support Part type with array as input + $this->assertFalse($this->service->supportsDenormalization(new \stdClass(), Part::class)); + $this->assertFalse($this->service->supportsDenormalization('string', Part::class)); + $this->assertFalse($this->service->supportsDenormalization(['a' => 'b'], \stdClass::class)); + $this->assertTrue($this->service->supportsDenormalization(['a' => 'b'], Part::class)); + } + + public function testDenormalize() + { + $input = [ + 'name' => 'Test Part', + 'description' => 'Test Description', + 'notes' => 'Test Note', //Test key normalization + 'ipn' => 'Test IPN', + 'mpn' => 'Test MPN', + 'instock' => '5', + 'storage_location' => 'Test Storage Location', + 'supplier' => 'Test Supplier', + 'price' => '5.5', + 'supplier_part_number' => 'TEST123' + ]; + + $part = $this->service->denormalize($input, Part::class, 'json', ['groups' => ['import'], 'create_unknown_datastructures' => true]); + $this->assertInstanceOf(Part::class, $part); + $this->assertSame('Test Part', $part->getName()); + $this->assertSame('Test Description', $part->getDescription()); + $this->assertSame('Test Note', $part->getComment()); + $this->assertSame('Test IPN', $part->getIpn()); + $this->assertSame('Test MPN', $part->getManufacturerProductNumber()); + + //Check that a new PartLot was created + $this->assertCount(1, $part->getPartLots()); + /** @var PartLot $partLot */ + $partLot = $part->getPartLots()->first(); + $this->assertSame(5.0, $partLot->getAmount()); + $this->assertNotNull($partLot->getStorageLocation()); + $this->assertSame('Test Storage Location', $partLot->getStorageLocation()->getName()); + + //Check that a new orderdetail was created + $this->assertCount(1, $part->getOrderdetails()); + /** @var Orderdetail $orderDetail */ + $orderDetail = $part->getOrderdetails()->first(); + $this->assertNotNull($orderDetail->getSupplier()); + $this->assertSame('Test Supplier', $orderDetail->getSupplier()->getName()); + $this->assertSame('TEST123', $orderDetail->getSupplierPartNr()); + + //Check that a pricedetail was created + $this->assertCount(1, $orderDetail->getPricedetails()); + /** @var Pricedetail $priceDetail */ + $priceDetail = $orderDetail->getPricedetails()->first(); + $this->assertSame("5.50000", (string) $priceDetail->getPrice()); + //Must be in base currency + $this->assertNull($priceDetail->getCurrency()); + //Must be for 1 part and 1 minimum order quantity + $this->assertSame(1.0, $priceDetail->getPriceRelatedQuantity()); + $this->assertSame(1.0, $priceDetail->getMinDiscountQuantity()); + } +} diff --git a/tests/Serializer/StructuralElementFromNameDenormalizerTest.php b/tests/Serializer/StructuralElementFromNameDenormalizerTest.php new file mode 100644 index 00000000..94b343a2 --- /dev/null +++ b/tests/Serializer/StructuralElementFromNameDenormalizerTest.php @@ -0,0 +1,123 @@ +. + */ + +namespace App\Tests\Serializer; + +use App\Entity\Base\AbstractStructuralDBElement; +use App\Entity\Parts\Category; +use App\Serializer\StructuralElementFromNameDenormalizer; +use PHPUnit\Framework\TestCase; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; + +class StructuralElementFromNameDenormalizerTest extends WebTestCase +{ + + /** @var StructuralElementFromNameDenormalizer */ + protected $service; + + protected function setUp(): void + { + parent::setUp(); + //Get an service instance. + self::bootKernel(); + $this->service = self::getContainer()->get(StructuralElementFromNameDenormalizer::class); + } + + public function testSupportsDenormalization(): void + { + //Only the combination of string data and StructuralElement class as type is supported. + $this->assertFalse($this->service->supportsDenormalization('doesnt_matter', \stdClass::class)); + $this->assertFalse($this->service->supportsDenormalization(['a' => 'b'], Category::class)); + + $this->assertTrue($this->service->supportsDenormalization('doesnt_matter', Category::class)); + } + + public function testDenormalizeCreateNew(): void + { + $context = [ + 'groups' => ['simple'], + 'path_delimiter' => '->', + 'create_unknown_datastructures' => true, + ]; + + //Test for simple category + $category = $this->service->denormalize('New Category', Category::class, null, $context); + $this->assertInstanceOf(Category::class, $category); + $this->assertSame('New Category', $category->getName()); + + //Test for nested category + $category = $this->service->denormalize('New Category->Sub Category', Category::class, null, $context); + $this->assertInstanceOf(Category::class, $category); + $this->assertSame('Sub Category', $category->getName()); + $this->assertInstanceOf(Category::class, $category->getParent()); + $this->assertSame('New Category', $category->getParent()->getName()); + + //Test with existing category + $category = $this->service->denormalize('Node 1->Node 1.1', Category::class, null, $context); + $this->assertInstanceOf(Category::class, $category); + $this->assertSame('Node 1.1', $category->getName()); + $this->assertInstanceOf(Category::class, $category->getParent()); + $this->assertSame('Node 1', $category->getParent()->getName()); + //Both categories should be in DB (have an ID) + $this->assertNotNull($category->getID()); + $this->assertNotNull($category->getParent()->getID()); + + //Test with other path_delimiter + $context['path_delimiter'] = '/'; + $category = $this->service->denormalize('New Category/Sub Category', Category::class, null, $context); + $this->assertInstanceOf(Category::class, $category); + $this->assertSame('Sub Category', $category->getName()); + $this->assertInstanceOf(Category::class, $category->getParent()); + $this->assertSame('New Category', $category->getParent()->getName()); + + //Test with empty path + $category = $this->service->denormalize('', Category::class, null, $context); + $this->assertNull($category); + } + + public function testDenormalizeOnlyExisting(): void + { + $context = [ + 'groups' => ['simple'], + 'path_delimiter' => '->', + 'create_unknown_datastructures' => false, + ]; + + //Test with existing category + $category = $this->service->denormalize('Node 1->Node 1.1', Category::class, null, $context); + $this->assertInstanceOf(Category::class, $category); + $this->assertSame('Node 1.1', $category->getName()); + $this->assertInstanceOf(Category::class, $category->getParent()); + $this->assertSame('Node 1', $category->getParent()->getName()); + //Both categories should be in DB (have an ID) + $this->assertNotNull($category->getID()); + $this->assertNotNull($category->getParent()->getID()); + + //Test with non existing category + $category = $this->service->denormalize('New category', Category::class, null, $context); + $this->assertNull($category); + + //Test with empty path + $category = $this->service->denormalize('', Category::class, null, $context); + $this->assertNull($category); + } + + +} diff --git a/tests/Serializer/StructuralElementNormalizerTest.php b/tests/Serializer/StructuralElementNormalizerTest.php new file mode 100644 index 00000000..09cf7299 --- /dev/null +++ b/tests/Serializer/StructuralElementNormalizerTest.php @@ -0,0 +1,77 @@ +. + */ + +namespace App\Tests\Serializer; + +use App\Entity\Parts\Category; +use App\Entity\Parts\Footprint; +use App\Entity\Parts\Part; +use App\Serializer\BigNumberNormalizer; +use App\Serializer\StructuralElementNormalizer; +use PHPUnit\Framework\TestCase; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; + +class StructuralElementNormalizerTest extends WebTestCase +{ + + /** @var StructuralElementNormalizer */ + protected $service; + + protected function setUp(): void + { + parent::setUp(); + //Get an service instance. + self::bootKernel(); + $this->service = self::getContainer()->get(StructuralElementNormalizer::class); + } + + public function testNormalize() + { + $category1 = (new Category())->setName('Category 1'); + $category11 = (new Category())->setName('Category 1.1'); + $category11->setParent($category1); + + //Serialize category 1 + $data1 = $this->service->normalize($category1, 'json', ['groups' => ['simple']]); + $this->assertArrayHasKey('full_name', $data1); + $this->assertSame('Category 1', $data1['full_name']); + //Json export must contain type attribute + $this->assertArrayHasKey('type', $data1); + + //Serialize category 1.1 + $data11 = $this->service->normalize($category11, 'json', ['groups' => ['simple']]); + $this->assertArrayHasKey('full_name', $data11); + $this->assertSame('Category 1->Category 1.1', $data11['full_name']); + + //Test that type attribute is removed for CSV export + $data11 = $this->service->normalize($category11, 'csv', ['groups' => ['simple']]); + $this->assertArrayNotHasKey('type', $data11); + } + + public function testSupportsNormalization() + { + //Normalizer must only support StructuralElement objects (and child classes) + $this->assertFalse($this->service->supportsNormalization(new \stdClass())); + $this->assertFalse($this->service->supportsNormalization(new Part())); + $this->assertTrue($this->service->supportsNormalization(new Category())); + $this->assertTrue($this->service->supportsNormalization(new Footprint())); + + } +}