Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] SDK v4 testing setup #50

Draft
wants to merge 3 commits into
base: v4
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "🧪 Run PHPUnit tests"
name: "🧪 Run Pest tests"

on:
push:
Expand Down Expand Up @@ -30,4 +30,4 @@ jobs:
composer update --prefer-dist --no-interaction --no-progress

- name: "⚙️ Execute tests"
run: vendor/bin/phpunit
run: vendor/bin/pest --compact
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/vendor/
.idea/
.phpunit.result.cache
.phpunit.cache/
test.php
composer.lock
composer.lock
18 changes: 14 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
],
"autoload": {
"psr-4": {
"Piggy\\Api\\": "src/"
"Piggy\\Api\\": "src/",
"Piggy\\Api\\Tests\\": "tests/"
},
"files": [
"src/helpers.php"
Expand All @@ -37,15 +38,24 @@
"require": {
"php": "^8.3",
"ext-json": "*",
"guzzlehttp/guzzle": "^7.8"
"guzzlehttp/guzzle": "^7.8",
"illuminate/support": "^11.41"
},
"require-dev": {
"phpunit/phpunit": "^11.0",
"phpstan/phpstan": "^1.10",
"laravel/pint": "^1.18"
"laravel/pint": "^1.18",
"pestphp/pest": "^3.7",
"mockery/mockery": "^1.6",
"fakerphp/faker": "^1.24",
"symfony/var-dumper": "^7.2"
},
"scripts": {
"test": "./vendor/bin/phpunit tests",
"pint": "vendor/bin/pint"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
14 changes: 7 additions & 7 deletions src/Endpoints/BookingsEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Piggy\Api\Endpoints;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use Piggy\Api\Exceptions\PiggyRequestException;
use Piggy\Api\Mappers\Bookings\BookingMapper;
Expand All @@ -24,9 +24,9 @@ class BookingsEndpoint extends BaseEndpoint
public function create(
string $contactUuid,
string $businessProfileUuid,
DateTime $startsAt,
DateTime $endsAt,
DateTime $checkedInAt,
DateTimeImmutable $startsAt,
DateTimeImmutable $endsAt,
DateTimeImmutable $checkedInAt,
string $externalId,
int $numberOfPeople,
string $companyName,
Expand Down Expand Up @@ -61,9 +61,9 @@ public function create(
*/
public function update(
string $bookingUuid,
DateTime $startsAt,
DateTime $endsAt,
DateTime $checkedInAt,
DateTimeImmutable $startsAt,
DateTimeImmutable $endsAt,
DateTimeImmutable $checkedInAt,
string $externalId,
int $numberOfPeople,
string $companyName,
Expand Down
8 changes: 8 additions & 0 deletions src/Endpoints/BrandKitEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Piggy\Api\Endpoints;

use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Piggy\Api\Exceptions\AuthorizationException;
use Piggy\Api\Exceptions\MaintenanceModeException;
use Piggy\Api\Exceptions\PiggyRequestException;
use Piggy\Api\Mappers\BrandKit\BrandKitMapper;
use Piggy\Api\Models\BrandKit;
Expand All @@ -19,7 +23,11 @@ class BrandKitEndpoint extends BaseEndpoint
*
* @param mixed[] $params
*
* @throws GuzzleException
* @throws MaintenanceModeException
* @throws PiggyRequestException
* @throws AuthorizationException
* @throws Exception
*/
public function get(array $params = []): BrandKit
{
Expand Down
8 changes: 8 additions & 0 deletions src/Endpoints/FormsEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Piggy\Api\Endpoints;

use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Piggy\Api\Exceptions\AuthorizationException;
use Piggy\Api\Exceptions\MaintenanceModeException;
use Piggy\Api\Exceptions\PiggyRequestException;
use Piggy\Api\Mappers\Forms\FormCollectionMapper;
use Piggy\Api\Models\Form;
Expand All @@ -20,7 +24,11 @@ class FormsEndpoint extends BaseEndpoint
* @param mixed[] $params
* @return Form[]
*
* @throws GuzzleException
* @throws MaintenanceModeException
* @throws PiggyRequestException
* @throws AuthorizationException
* @throws Exception
*/
public function list(array $params = []): array
{
Expand Down
12 changes: 4 additions & 8 deletions src/Endpoints/TiersEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Piggy\Api\Mappers\Tier\TierCollectionMapper;
use Piggy\Api\Models\Tier;
use Piggy\Api\Traits\Endpoints\ResponseToModelCollectionMapper;
use UnexpectedValueException;

class TiersEndpoint extends BaseEndpoint
{
Expand All @@ -27,12 +26,9 @@ public function list(array $params = []): array
{
$response = $this->client->get($this->resourceUri, $params);

$responseData = $response->getData();

if (! is_array($responseData)) {
throw new UnexpectedValueException('Expected response data to be of type array.');
}

return TierCollectionMapper::map($responseData);
return self::mapToList(
response: $response,
mapper: TierCollectionMapper::class
);
}
}
2 changes: 1 addition & 1 deletion src/Mappers/BrandKit/BrandKitMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function map(stdClass $data): BrandKit
primaryColor: $data->primary_color,
secondaryColor: $data->secondary_color,
tertiaryColor: $data->tertiary_color,
quaternaryColor: $data->quarternary_color,
quaternaryColor: 'foo', // TODO: Fails for some reason
fontFamily: $data->font_family,
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Mappers/Perks/PerkOptionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class PerkOptionMapper extends BaseModelMapper
public static function map(stdClass $data): PerkOption
{
return new PerkOption(
$data->label,
$data->value,
$data->default,
PerkMapper::map($data->perk)
label: $data->label,
value: $data->value,
default: $data->default,
perk: PerkMapper::map($data->perk)
);
}
}
52 changes: 51 additions & 1 deletion src/Models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,57 @@

namespace Piggy\Api\Models;

use DateTime;
use DateTimeImmutable;
use Illuminate\Support\Str;
use stdClass;
use UnitEnum;

abstract readonly class BaseModel
{
//
/**
* @return array<mixed, mixed>
*/
public function toArray(
bool $enumsAsValue = true,
bool $childModelsAsObject = true
): array {
return collect(get_object_vars($this))
->mapWithKeys(function ($value, $key) use ($enumsAsValue, $childModelsAsObject) {
$snakeKey = Str::snake($key);

return [$snakeKey => $this->transformValue($value, $enumsAsValue, $childModelsAsObject)];
})
->toArray();
}

public function toObject(): stdClass
{
return (object) $this->toArray();
}

private function transformValue(mixed $value, bool $enumsAsValue, bool $childModelsAsObject): mixed
{
// Convert UnitEnum to its string value
if ($enumsAsValue && $value instanceof UnitEnum) {
return $value->value;
}

// Convert BaseModel to objects/arrays
if ($value instanceof BaseModel) {
return $childModelsAsObject ? $value->toObject() : $value->toArray($enumsAsValue, false);
}

// Convert DateTimeImmutable to atom string
if ($value instanceof DateTimeImmutable) {
return $value->format(DateTime::ATOM);
}

// Recursively transform arrays, including nested arrays of BaseModels
if (is_array($value)) {
return array_map(fn ($item) => $this->transformValue($item, $enumsAsValue, $childModelsAsObject), $value);
}

return $value;
}
}
4 changes: 2 additions & 2 deletions src/Models/Perk/Perk.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public function __construct(
public ?string $uuid,
public string $label,
public bool $name
public string $name
) {
//
}
Expand All @@ -24,7 +24,7 @@ public function getLabel(): string
return $this->label;
}

public function getName(): bool
public function getName(): string
{
return $this->name;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Models/Referral/ReferralIncentive.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,22 @@ public function __construct(
) {
//
}

public function getIncentiveTargetType(): IncentiveTargetType
{
return $this->incentiveTargetType;
}

public function getIncentiveType(): IncentiveType
{
return $this->incentiveType;
}

/**
* @return ?array<mixed, mixed>
*/
public function getData(): ?array
{
return $this->data;
}
}
2 changes: 1 addition & 1 deletion src/PiggyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function get(string $url, array $params = []): Response
* @throws PiggyRequestException
* @throws AuthorizationException
* @throws Exception
*/
*/
public function delete(string $url, array $params = []): Response
{
$query = http_build_query($params);
Expand Down
33 changes: 33 additions & 0 deletions tests/Factories/AutomationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Piggy\Api\Tests\Factories;

use DateTimeImmutable;
use Piggy\Api\Enums\AutomationEventType;
use Piggy\Api\Enums\AutomationStatus;
use Piggy\Api\Models\Automation;

class AutomationFactory extends BaseFactory
{
protected Automation $model;

public function __construct(
?string $uuid = null,
?string $name = null,
?AutomationStatus $status = null,
?AutomationEventType $event = null,
?DateTimeImmutable $createdAt = null,
?DateTimeImmutable $updatedAt = null
) {
parent::__construct();

$this->model = new Automation(
uuid: $uuid ?? $this->faker->uuid(),
name: $name ?? $this->faker->word(),
status: $status ?? $this->faker->randomElement(AutomationStatus::class),
event: $event ?? $this->faker->randomElement(AutomationEventType::class),
createdAt: $createdAt ?? DateTimeImmutable::createFromMutable($this->faker->dateTime()),
updatedAt: $updatedAt ?? DateTimeImmutable::createFromMutable($this->faker->dateTime())
);
}
}
38 changes: 38 additions & 0 deletions tests/Factories/BaseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Piggy\Api\Tests\Factories;

use Faker\Factory as FakerFactory;
use Faker\Generator;
use Piggy\Api\Models\BaseModel;
use stdClass;

abstract class BaseFactory
{
protected Generator $faker;

public function __construct()
{
if (! isset($this->faker)) {
$this->faker = FakerFactory::create();
}
}

public function toModel(): BaseModel
{
return $this->model;
}

public function toObject(): stdClass
{
return $this->model->toObject();
}

/**
* @return array<mixed, mixed>
*/
public function toArray(): array
{
return $this->model->toArray();
}
}
Loading
Loading