ldc-flucollector/tests/Client/ClientIncoTest.php
2023-10-04 09:45:04 +02:00

330 lines
13 KiB
PHP

<?php
namespace Tests\Client;
use App\Models\XmlInco;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use UWIL\Flucollector\Enums\PortionType;
use UWIL\Flucollector\Exceptions\AccessError;
use UWIL\Flucollector\Exceptions\ValidationException;
use UWIL\Flucollector\FlucollectorApi;
use UWIL\Flucollector\Lib\Allegations;
use UWIL\Flucollector\Lib\NutritionalValues;
use UWIL\Flucollector\Lib\PortionAmount;
use UWIL\Flucollector\Models\Inco;
use UWIL\Flucollector\Models\IncoResult;
class ClientIncoTest extends ClientTester
{
/**
* Test get customer prop.
*
* @return void
*/
public function testGetter()
{
$this->expectNotToPerformAssertions();
$inco = new Inco();
$inco->name;
}
/**
* Test set customer prop
*
* @return void
*/
public function testSetter()
{
$this->expectNotToPerformAssertions();
$inco = new Inco();
$inco->name = 'toto';
$inco->name;
}
/**
* Test client wrong type.
*
* @return void
*/
public function testSetterFailWrongType()
{
$this->expectException(ValidationException::class);
$inco = new Inco();
$inco->name = 2;
}
/**
* Test country validation.
*
* @return void
*/
public function testSetterFailWrongTypePortionType()
{
$this->expectException(ValidationException::class);
$inco = new Inco();
$inco->portion_type = 'toto';
}
/**
* Test set all props ok.
*
* @return void
*/
public function testPropsAllSetOk()
{
$this->expectNotToPerformAssertions();
$inco = new Inco();
XmlInco::factory(255)->make()->each(function (XmlInco $xml_inco) use ($inco) {
$inco->id = 3;
$inco->name = $xml_inco->name;
$inco->description = $xml_inco->description;
$inco->tips = $xml_inco->tips;
$inco->preparation_advices = $xml_inco->preparation_advices;
$inco->sell_arguments = $xml_inco->sell_arguments;
$inco->allegations = new Allegations(...$xml_inco->allegations->toArray());
$inco->ingredient_list = $xml_inco->ingredient_list;
$inco->allergen_list = $xml_inco->allergen_list;
$inco->nutritional_values = new NutritionalValues($xml_inco->nutritional_values->toArray());
$inco->portion_amount = \is_null($xml_inco->portion_amount) ?
$xml_inco->portion_amount : PortionAmount::fromJson($xml_inco->portion_amount->toArray());
$inco->portion_type = \is_null($xml_inco->portion_type) ?
$xml_inco->portion_type : PortionType::make($xml_inco->portion_type->value());
$inco->is_seasonal = $xml_inco->is_seasonal;
$inco->updated_at = Carbon::parse(now()->toIso8601String());
});
}
/**
* Test customer is json serializable.
*
* @return void
*/
public function testJsonSerialize()
{
$inco = new Inco();
/** @var \App\Models\XmlInco */
$xml_inco = XmlInco::factory(1)->make()->first();
$propTypes = [
'id' => 3,
'name' => $xml_inco->name,
'description' => $xml_inco->description,
'tips' => $xml_inco->tips,
'preparation_advices' => $xml_inco->preparation_advices,
'sell_arguments' => $xml_inco->sell_arguments,
'allegations' => new Allegations(...$xml_inco->allegations->toArray()),
'ingredient_list' => $xml_inco->ingredient_list,
'allergen_list' => $xml_inco->allergen_list,
'nutritional_values' => new NutritionalValues($xml_inco->nutritional_values->toArray()),
'portion_amount' => \is_null($xml_inco->portion_amount) ?
$xml_inco->portion_amount : PortionAmount::fromJson($xml_inco->portion_amount->toArray()),
'portion_type' => \is_null($xml_inco->portion_type) ?
$xml_inco->portion_type : PortionType::make($xml_inco->portion_type->value()),
'is_seasonal' => $xml_inco->is_seasonal,
'updated_at' => Carbon::parse(now()->toIso8601String())
];
foreach ($propTypes as $prop => $type) {
$inco->{$prop} = $type;
}
$jsonDecoded = \json_decode(\json_encode($inco), true);
$this->assertIsArray($jsonDecoded);
$propTypes['allegations'] = $inco->allegations->jsonSerialize();
$propTypes['nutritional_values'] = $inco->nutritional_values->jsonSerialize(true);
$propTypes['portion_amount'] = \is_null($inco->portion_amount) ?
$inco->portion_amount : $inco->portion_amount->jsonSerialize();
$propTypes['portion_type'] = \is_null($inco->portion_type) ?
$inco->portion_type : $inco->portion_type->jsonSerialize();
$propTypes['updated_at'] = $inco->updated_at->toIso8601String();
foreach ($propTypes as $key => $value) {
$this->assertArrayHasKey($key, $jsonDecoded);
$this->assertEquals($value, $jsonDecoded[$key]);
}
}
/**
* Test set all props fail.
*
* @return void
*/
public function testPropsAllSetNotOk()
{
$this->expectException(ValidationException::class);
$inco = new Inco();
/** @var \App\Models\XmlInco */
$xml_inco = XmlInco::factory(1)->make()->first();
$propTypes = [
'id' => 3,
'name' => $xml_inco->name,
'description' => $xml_inco->description,
'tips' => $xml_inco->tips,
'preparation_advices' => $xml_inco->preparation_advices,
'sell_arguments' => $xml_inco->sell_arguments,
'allegations' => new Allegations(...$xml_inco->allegations->toArray()),
'ingredient_list' => $xml_inco->ingredient_list,
'allergen_list' => 6,
'nutritional_values' => new NutritionalValues($xml_inco->nutritional_values->toArray()),
'portion_amount' => \is_null($xml_inco->portion_amount) ?
$xml_inco->portion_amount : PortionAmount::fromJson($xml_inco->portion_amount->toArray()),
'portion_type' => \is_null($xml_inco->portion_type) ?
$xml_inco->portion_type : PortionType::make($xml_inco->portion_type->value()),
'is_seasonal' => $xml_inco->is_seasonal,
'updated_at' => Carbon::parse(now()->toIso8601String())
];
foreach ($propTypes as $prop => $type) {
$inco->{$prop} = $type;
}
}
/**
* Test set unkown prop fail
*
* @return void
*/
public function testPropsAssignUnknown()
{
$this->expectException(AccessError::class);
$inco = new Inco();
/** @var \App\Models\XmlInco */
$xml_inco = XmlInco::factory(1)->make()->first();
$propTypes = [
'id' => 3,
'name' => $xml_inco->name,
'description' => $xml_inco->description,
'tips' => $xml_inco->tips,
'preparation_advices' => $xml_inco->preparation_advices,
'sell_arguments' => $xml_inco->sell_arguments,
'allegations' => new Allegations(...$xml_inco->allegations->toArray()),
'ingredient_list' => $xml_inco->ingredient_list,
'allergen_list' => $xml_inco->allergen_list,
'nutritional_values' => new NutritionalValues($xml_inco->nutritional_values->toArray()),
'portion_amount' => \is_null($xml_inco->portion_amount) ?
$xml_inco->portion_amount : PortionAmount::fromJson($xml_inco->portion_amount->toArray()),
'portion_type' => \is_null($xml_inco->portion_type) ?
$xml_inco->portion_type : PortionType::make($xml_inco->portion_type->value()),
'is_seasonal' => $xml_inco->is_seasonal,
'updated_at' => Carbon::parse(now()->toIso8601String())
];
foreach ($propTypes as $prop => $type) {
$inco->{$prop} = $type;
}
$this->assertNull($inco->unknownProp);
$inco->unknownProp = 'value';
}
/**
* Test set prop fail wrong type
*
* @return void
*/
public function testPropsAssignWrongType()
{
$this->expectException(ValidationException::class);
/** @var \App\Models\XmlInco */
$xml_inco = XmlInco::factory(1)->make()->first();
$inco = new Inco([
'id' => 3,
'name' => $xml_inco->name,
'description' => $xml_inco->description,
'tips' => $xml_inco->tips,
'preparation_advices' => $xml_inco->preparation_advices,
'sell_arguments' => $xml_inco->sell_arguments,
'allegations' => new Allegations(...$xml_inco->allegations->toArray()),
'ingredient_list' => $xml_inco->ingredient_list,
'allergen_list' => $xml_inco->allergen_list,
'nutritional_values' => new NutritionalValues($xml_inco->nutritional_values->toArray()),
'portion_amount' => \is_null($xml_inco->portion_amount) ?
$xml_inco->portion_amount : PortionAmount::fromJson($xml_inco->portion_amount->toArray()),
'portion_type' => \is_null($xml_inco->portion_type) ?
$xml_inco->portion_type : PortionType::make($xml_inco->portion_type->value()),
'is_seasonal' => $xml_inco->is_seasonal,
'updated_at' => Carbon::parse(now()->toIso8601String())
]);
$inco->nutritional_values = 'value';
}
/**
* Test get inco from id.
*
* @return void
*/
public function testIncoGet()
{
$api = new FlucollectorApi();
$inco = $api->get(static::$xml_inco->id);
$expectedInco = new Inco([
'id' => static::$xml_inco->id,
'name' => static::$xml_inco->name,
'description' => static::$xml_inco->description,
'tips' => static::$xml_inco->tips,
'preparation_advices' => static::$xml_inco->preparation_advices,
'sell_arguments' => static::$xml_inco->sell_arguments,
'allegations' => new Allegations(...static::$xml_inco->allegations->toArray()),
'ingredient_list' => static::$xml_inco->ingredient_list,
'allergen_list' => static::$xml_inco->allergen_list,
'nutritional_values' => new NutritionalValues(static::$xml_inco->nutritional_values->toArray()),
'portion_amount' => \is_null(static::$xml_inco->portion_amount) ?
static::$xml_inco->portion_amount :
PortionAmount::fromJson(static::$xml_inco->portion_amount->toArray()),
'portion_type' => \is_null(static::$xml_inco->portion_type) ?
static::$xml_inco->portion_type : PortionType::make(static::$xml_inco->portion_type->value()),
'is_seasonal' => static::$xml_inco->is_seasonal,
'updated_at' => Carbon::parse(static::$xml_inco->updated_at->toIso8601String())
]);
$this->assertEquals(Inco::class, get_class($inco));
$this->assertEquals($inco->id, $expectedInco->id);
$this->assertEquals($inco->name, $expectedInco->name);
$this->assertEquals($inco->description, $expectedInco->description);
$this->assertEquals($inco->tips, $expectedInco->tips);
$this->assertEquals($inco->preparation_advices, $expectedInco->preparation_advices);
$this->assertEquals($inco->sell_arguments, $expectedInco->sell_arguments);
$this->assertEquals($inco->allegations, $expectedInco->allegations);
$this->assertEquals($inco->ingredient_list, $expectedInco->ingredient_list);
$this->assertEquals($inco->allergen_list, $expectedInco->allergen_list);
$this->assertEquals($inco->nutritional_values, $expectedInco->nutritional_values);
$this->assertEquals($inco->portion_amount, $expectedInco->portion_amount);
$this->assertEquals($inco->portion_type, $expectedInco->portion_type);
$this->assertEquals($inco->is_seasonal, $expectedInco->is_seasonal);
$this->assertEquals($inco->updated_at, $expectedInco->updated_at);
}
/**
* Test get find inco
*
* @return void
*/
public function testIncoFind()
{
$api = new FlucollectorApi();
$results = $api->find(\strval(static::$xml_inco->id));
$expectedResult = new IncoResult([
'id' => static::$xml_inco->id,
'name' => static::$xml_inco->name,
]);
$this->assertCount(1, $results, 'Assumed there is only one results since we wanted to search using id');
$incoResult = Arr::first($results);
$this->assertEquals(IncoResult::class, get_class($expectedResult));
$this->assertEquals($incoResult->id, $expectedResult->id);
$this->assertEquals($incoResult->name, $expectedResult->name);
}
}