Added tests for serializer normalizers

This commit is contained in:
Jan Böhmer 2023-03-14 00:02:40 +01:00
parent 3bbff0aecf
commit 945fd988b3
7 changed files with 397 additions and 2 deletions

View file

@ -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

View file

@ -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;

View file

@ -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'];

View file

@ -0,0 +1,59 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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));
}
}

View file

@ -0,0 +1,132 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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());
}
}

View file

@ -0,0 +1,123 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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);
}
}

View file

@ -0,0 +1,77 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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()));
}
}