Skip to content

Instantly share code, notes, and snippets.

@Gummibeer
Created December 1, 2022 22:14
Show Gist options
  • Save Gummibeer/feaa6ed96f67d77fd2a7189bb89ca80b to your computer and use it in GitHub Desktop.
Save Gummibeer/feaa6ed96f67d77fd2a7189bb89ca80b to your computer and use it in GitHub Desktop.
<?php
namespace Tests\Asserts;
use App\Enums\DeliveryStatus;
use App\Enums\DistributionRoundStatus;
use App\Enums\DistributionStopType;
use App\Enums\DistributionTaskStatus;
use App\Enums\PackingStationType;
use App\Enums\ProductStockEventReason;
use App\Enums\ProductStockEventType;
use App\Enums\StocktakingStatus;
use App\Http\Resources\BufferResource;
use App\Http\Resources\DeliveryDemandItemResource;
use App\Http\Resources\DeliveryResource;
use App\Http\Resources\DepositProductResource;
use App\Http\Resources\DistributionRouteStopResource;
use App\Http\Resources\ManufacturerResource;
use App\Http\Resources\PackingBoxResource;
use App\Http\Resources\PackingPositionResource;
use App\Http\Resources\PackingShiftResource;
use App\Http\Resources\PackingStationResource;
use App\Http\Resources\PrinterResource;
use App\Http\Resources\ProductResource;
use App\Http\Resources\PurchaseOrderItemResource;
use App\Http\Resources\PurchaseOrderResource;
use App\Http\Resources\ReplacementDeliveryItemResource;
use App\Http\Resources\SimplePackingStationResource;
use App\Http\Resources\StatusDeliveryItemResource;
use App\Http\Resources\StatusDeliveryResource;
use App\Http\Resources\SupplierDeliveryItemResource;
use App\Http\Resources\SupplierPickupItemResource;
use App\Http\Resources\SupplierResource;
use App\Http\Resources\SupplierRouteResource;
use App\Http\Resources\UserResource;
use DateTimeInterface;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Testing\Fluent\AssertableJson;
use OutOfRangeException;
use PHPUnit\Framework\Assert as PHPUnit;
final class AssertableJsonResourceAssertions
{
public static function assertResource(AssertableJson $json, string $resource): void
{
$method = (string) Str::of($resource)
->classBasename()
->prepend('assert');
if (! method_exists(self::class, $method)) {
throw new OutOfRangeException($resource);
}
call_user_func(self::class.'::'.$method, $json);
}
public static function assertResourceCollection(AssertableJson $json, string $resource): void
{
$json->each(static function (AssertableJson $json) use ($resource): void {
self::assertResource($json, $resource);
});
}
protected static function assertStorageBatchResource(AssertableJson $json): bool
{
$json
->whereType('id', 'integer')
->whereSku('sku')
->whereDateTime('best_before_date', 'Y-m-d', false)
->whereDateTime('sellable_until', 'Y-m-d', false)
->whereType('stock', 'integer')
->where('stock', static function (int $stock): bool {
return $stock >= 0;
})
->whereType('quantity', ['integer', 'double'])
->where('quantity', static function (float $quantity): bool {
return $quantity >= 0;
})
->whereType('quantity_surplus', ['integer', 'double'])
->where('quantity_surplus', static function (float $surplus): bool {
return $surplus >= 0;
})
->whereType('packing_position_name', 'string')
->whereType('approved_by', ['integer', 'null'])
->whereDateTime('approved_at', DateTimeInterface::RFC3339, false)
->whereDateTime('stocktaking_requested_at', DateTimeInterface::RFC3339, false)
->whereType('stocktaking_status', 'string')
->whereIn('stocktaking_status', StocktakingStatus::toValues())
->whereResource('product', ProductResource::class, false)
->whereType('is_deletable', 'boolean');
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment