diff --git a/tests/Api/MauticApiTestCase.php b/tests/Api/MauticApiTestCase.php index c1a4fb1..c1362a2 100644 --- a/tests/Api/MauticApiTestCase.php +++ b/tests/Api/MauticApiTestCase.php @@ -173,13 +173,19 @@ protected function assertPayloadItem($itemProp, $callback, $itemVal, $item) } } + protected function getTestPayload(): array + { + return $this->testPayload; + } + protected function standardTestGetListOfSpecificIds($callback = null) { // Create some items first $itemIds = []; for ($i = 0; $i <= 2; ++$i) { - $response = $this->api->create($this->testPayload); - $this->assertErrors($response, 'Payload: '.json_encode($this->testPayload, JSON_PRETTY_PRINT)); + $payload = $this->getTestPayload(); + $response = $this->api->create($payload); + $this->assertErrors($response, 'Payload: '.json_encode($payload, JSON_PRETTY_PRINT)); $itemIds[] = $response[$this->api->itemName()]['id']; } @@ -233,7 +239,7 @@ protected function standardTestGetList() protected function standardTestCreateGetAndDelete(?array $payload = null) { if (empty($payload)) { - $payload = $this->testPayload; + $payload = $this->getTestPayload(); } // Create item @@ -259,9 +265,9 @@ protected function standardTestBatchEndpoints(?array $batch = null, $callback = if (null == $batch) { $batch = [ - $this->testPayload, - $this->testPayload, - $this->testPayload, + $this->getTestPayload(), + $this->getTestPayload(), + $this->getTestPayload(), ]; } @@ -304,13 +310,14 @@ protected function standardTestBatchEndpoints(?array $batch = null, $callback = public function standardTestEditPatch(array $editTo) { - $response = $this->api->edit(10000, $this->testPayload); + $createPayload = $this->getTestPayload(); + $response = $this->api->edit(10000, $createPayload); // there should be an error as the item shouldn't exist $this->assertTrue(isset($response['errors'][0]), $response['errors'][0]['message']); - $response = $this->api->create($this->testPayload); - $this->assertPayload($response); + $response = $this->api->create($createPayload); + $this->assertPayload($response, $createPayload); $response = $this->api->edit($response[$this->api->itemName()]['id'], $editTo); $this->assertPayload($response, $editTo); @@ -321,8 +328,9 @@ public function standardTestEditPatch(array $editTo) public function standardTestEditPut() { - $response = $this->api->edit(10000, $this->testPayload, true); - $this->assertPayload($response); + $payload = $this->getTestPayload(); + $response = $this->api->edit(10000, $payload, true); + $this->assertPayload($response, $payload); // now delete the entity $response = $this->api->delete($response[$this->api->itemName()]['id']); diff --git a/tests/Api/StagesTest.php b/tests/Api/StagesTest.php index 20befa1..556fde0 100644 --- a/tests/Api/StagesTest.php +++ b/tests/Api/StagesTest.php @@ -13,6 +13,8 @@ class StagesTest extends MauticApiTestCase { + private static $nextWeight; + public function setUp(): void { $this->api = $this->getContext('stages'); @@ -21,6 +23,27 @@ public function setUp(): void ]; } + protected function getTestPayload(): array + { + return [ + 'name' => sprintf('test %s', uniqid('', true)), + 'weight' => $this->getNextWeight(), + ]; + } + + private function getNextWeight(): int + { + if (null === self::$nextWeight) { + $response = $this->api->getList('', 0, 1, 'weight', 'DESC'); + $firstStage = $response[$this->api->listName()][0] ?? []; + $maxWeight = isset($firstStage['weight']) ? (int) $firstStage['weight'] : 0; + + self::$nextWeight = $maxWeight + 1; + } + + return self::$nextWeight++; + } + public function testGetList() { $this->standardTestGetList(); @@ -58,8 +81,9 @@ public function testAddAndRemove() $contact = $response['contact']; // Create stage - $response = $this->api->create($this->testPayload); - $this->assertPayload($response); + $payload = $this->getTestPayload(); + $response = $this->api->create($payload); + $this->assertPayload($response, $payload); $stage = $response[$this->api->itemName()]; // Add contact to the stage