Skip to content

Commit

Permalink
refactor to avoid endless casting to object
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasVHG committed Aug 6, 2024
1 parent 584c3e2 commit 7491086
Showing 1 changed file with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CultuurNet\UDB3\Http\ApiProblem\AssertApiProblemTrait;
use CultuurNet\UDB3\Http\ApiProblem\SchemaError;
use CultuurNet\UDB3\Http\Request\Psr7RequestBuilder;
use CultuurNet\UDB3\Json;
use PHPUnit\Framework\TestCase;

final class PriceInfoDuplicateNameValidatingRequestBodyParserTest extends TestCase
Expand Down Expand Up @@ -62,42 +63,42 @@ public function it_handles_priceInfo(): void
*/
public function it_throws_on_same_names(): void
{
$priceInfo = (object) [
'priceInfo' => (object) [
(object)[
$priceInfo = [
'priceInfo' => [
[
'category' => 'tariff',
'name' => (object) [
'name' => [
'nl' => 'Studenten',
],
'price' => 10,
'priceCurrency' => 'EUR',
],
(object)[
[
'category' => 'base',
'name' => (object) [
'name' => [
'nl' => 'Basistarief',
],
'price' => 50,
'priceCurrency' => 'EUR',
],
(object)[
[
'category' => 'tariff',
'name' => (object) [
'name' => [
'nl' => 'Studenten',
],
'price' => 10,
'priceCurrency' => 'EUR',
],
(object)[
[
'category' => 'tariff',
'name' => (object) [
'name' => [
'nl' => 'Leraren',
],
'price' => 20,
],
(object)[
[
'category' => 'tariff',
'name' => (object) [
'name' => [
'nl' => 'Studenten',
],
'price' => 10,
Expand All @@ -107,7 +108,7 @@ public function it_throws_on_same_names(): void

$request = $this->requestBuilder
->build('POST')
->withParsedBody($priceInfo);
->withParsedBody($this->arrayAsObject($priceInfo));

$this->assertCallableThrowsApiProblem(
ApiProblem::bodyInvalidData(
Expand All @@ -123,42 +124,42 @@ public function it_throws_on_same_names(): void
*/
public function it_throws_on_same_names_with_different_spacing(): void
{
$priceInfo = (object) [
'priceInfo' => (object) [
(object)[
$priceInfo = [
'priceInfo' => [
[
'category' => 'tariff',
'name' => (object) [
'name' => [
'nl' => 'Studenten',
],
'price' => 10,
'priceCurrency' => 'EUR',
],
(object)[
[
'category' => 'base',
'name' => (object) [
'name' => [
'nl' => 'Basistarief',
],
'price' => 50,
'priceCurrency' => 'EUR',
],
(object)[
[
'category' => 'tariff',
'name' => (object) [
'name' => [
'nl' => 'Studenten ',
],
'price' => 15,
'priceCurrency' => 'EUR',
],
(object)[
[
'category' => 'tariff',
'name' => (object) [
'name' => [
'nl' => 'Leraren',
],
'price' => 20,
],
(object)[
[
'category' => 'tariff',
'name' => (object) [
'name' => [
'nl' => 'Studenten ',
],
'price' => 30,
Expand All @@ -168,7 +169,7 @@ public function it_throws_on_same_names_with_different_spacing(): void

$request = $this->requestBuilder
->build('POST')
->withParsedBody($priceInfo);
->withParsedBody($this->arrayAsObject($priceInfo));

$this->assertCallableThrowsApiProblem(
ApiProblem::bodyInvalidData(
Expand All @@ -178,4 +179,9 @@ public function it_throws_on_same_names_with_different_spacing(): void
fn () => $this->priceInfoDuplicateNameValidatingRequestBodyParser->parse($request)
);
}

private function arrayAsObject(array $priceInfoArry): object
{
return Json::decode(Json::encode($priceInfoArry));
}
}

0 comments on commit 7491086

Please sign in to comment.