Skip to content

Commit

Permalink
test(update-person): check if update-method ignores invalid modifiabl…
Browse files Browse the repository at this point in the history
…eAttributes
  • Loading branch information
DumbergerL committed Aug 18, 2022
1 parent 7cf1288 commit 73db4f4
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions tests/unit/Requests/PersonRequestBuilderUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
namespace Tests\Unit\Requests;


use CTApi\CTConfig;
use CTApi\CTLog;
use CTApi\Exceptions\CTModelException;
use CTApi\Models\Person;
use CTApi\Models\Traits\ExtractData;
use CTApi\Models\Traits\FillWithData;
use CTApi\Requests\PersonRequest;
use CTApi\Utils\CTUtil;

Expand Down Expand Up @@ -98,4 +98,46 @@ public function testUpdateNotExistingModifiedProperty()
PersonRequest::update($person, ["notExistingProperty", "notExistingPropertyTheSecond"]);
$this->assertRequestCallNotExists("PATCH", "/api/persons/21");
}

/**
* Test what happens to Update-Method when the Model has ModifiableAttributes defined, that does not exist as
* Attributes. For this Test-Case the CarModelMock inherit all methods from the PersonModel-Class. The
* getModifiableAttributes return the key "numberOfWings", that does not exist as Attribut. The Test now checks
* that this Fake-Attribute is not send via to the REST-API.
*/
public function testModelHasInvalidModifiableAttributes()
{
$car = CarModelMock::createModelFromData([
"id" => 82,
"color" => "red",
"numberOfDoors" => 4
]);

PersonRequest::update($car);

$request = $this->assertRequestCallExists("PATCH", "/api/persons/82");
$jsonData = CTUtil::arrayPathGet($request, "options.json");

$this->assertEquals(sizeof($jsonData), 1, "Expected only one JSON-Attribut (Color)");
$this->assertArrayHasKey("color", $jsonData);
$this->assertEquals($jsonData["color"], "red");
}
}


class CarModelMock extends Person
{
use FillWithData, ExtractData;

protected ?string $color = null;
protected ?string $brand = null;
protected ?int $numberOfDoors = null;

public function getModifiableAttributes(): array
{
return [
"color",
"numberOfWings" // illegal modifiable attribute => should be ignored
];
}
}

0 comments on commit 73db4f4

Please sign in to comment.