Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner committed Aug 21, 2024
1 parent fc64333 commit 733748b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
28 changes: 27 additions & 1 deletion src/Rules/MaxTokenCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Enjin\Platform\Rules\Traits\HasDataAwareRule;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Arr;

class MaxTokenCount implements DataAwareRule, ValidationRule
{
Expand Down Expand Up @@ -41,6 +42,12 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
&& ($collection = Collection::withCount('tokens')->firstWhere(['collection_chain_id' => $this->collectionId]))
&& ! is_null($this->limit = $collection->max_token_count)
) {
if ($this->limit == 0) {
$fail('enjin-platform-beam::validation.max_token_count')->translate(['limit' => $this->limit]);

return;
}

$existingCount = BeamClaim::where('type', BeamType::MINT_ON_DEMAND->name)
->whereHas(
'beam',
Expand All @@ -53,7 +60,26 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
->groupBy('token_chain_id')
->count();

[$integers, $ranges] = collect($this->data['tokens'])
$tokens = collect($this->data['tokens'])
->filter(fn ($data) => !empty(Arr::get($data, 'tokenIds')))
->pluck('tokenIds')
->flatten();


collect($this->data['tokens'])
->filter(fn ($data) => !empty(Arr::get($data, 'tokenIdDataUpload')))
->map(function ($data) use ($tokens) {
$handle = fopen($data['tokenIdDataUpload']->getPathname(), 'r');
while (($line = fgets($handle)) !== false) {
if (! $this->tokenIdExists($tokens->all(), $tokenId = trim($line))) {
$tokens->push($tokenId);
}
}
fclose($handle);
});

[$integers, $ranges] = collect($tokens)
->filter(fn ($val) => !empty(Arr::get($val, 'tokenIds')))
->pluck('tokenIds')
->flatten()
->partition(fn ($val) => $this->integerRange($val) === false);
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/GraphQL/Mutations/CreateBeamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,18 +343,18 @@ public function test_it_will_fail_with_invalid_claim_quantity(): void
);
$this->assertArraySubset(['tokens.0.claimQuantity' => ['The token count exceeded the maximum limit of 0 for this collection.']], $response['error']);

$this->collection->update(['max_token_count' => 2]);
$response = $this->graphql(
$this->method,
$data = array_merge(
$this->generateBeamData(BeamType::MINT_ON_DEMAND, 1),
['tokens' => [['tokenIds' => ['1'], 'type' => BeamType::MINT_ON_DEMAND->name]]]
)
),
true
);
$this->assertNotEmpty($response);

$response = $this->graphql($this->method, $data, true);
$this->assertArraySubset(['tokens.0.claimQuantity' => ['The token count exceeded the maximum limit of 2 for this collection.']], $response['error']);
$this->assertArraySubset(['tokens.0.claimQuantity' => ['The token count exceeded the maximum limit of 0 for this collection.']], $response['error']);
}

/**
Expand Down

0 comments on commit 733748b

Please sign in to comment.