Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions tests/Api/MauticApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

Expand Down Expand Up @@ -233,7 +239,7 @@ protected function standardTestGetList()
protected function standardTestCreateGetAndDelete(?array $payload = null)
{
if (empty($payload)) {
$payload = $this->testPayload;
$payload = $this->getTestPayload();
}

// Create item
Expand All @@ -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(),
];
}

Expand Down Expand Up @@ -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);
Expand All @@ -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']);
Expand Down
28 changes: 26 additions & 2 deletions tests/Api/StagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class StagesTest extends MauticApiTestCase
{
private static $nextWeight;

public function setUp(): void
{
$this->api = $this->getContext('stages');
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down
Loading