Skip to content

Commit

Permalink
refactore relation limits to stdClas
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Baettig committed Dec 20, 2019
1 parent 5b16b90 commit fca058d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ composer.lock
/.idea
/tests/database.sqlite-journal
/tests/database.sqlite
/.phpunit.result.cache
7 changes: 4 additions & 3 deletions src/Models/HasLimitedRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@


use OnlineVerkaufen\Subscriptions\Exception\FeatureException;
use stdClass;

trait HasLimitedRelations
{
/**
* @param string $relation
* @return array
* @return stdClass
* @throws FeatureException
*/
public function getUsageFor(string $relation): array
public function getUsageFor(string $relation): stdClass
{
if ($this->$relation) {
$usedItems = count($this->$relation);
$availableItems = $this->getFeatureAvailabilityForRelation($relation);
$remainingItems = $availableItems - $usedItems;

return [
return (object)[
'used' => $usedItems,
'available' => $availableItems,
'remaining' => $remainingItems
Expand Down
24 changes: 11 additions & 13 deletions tests/unit/HasRestrictedUsagesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ public function can_get_the_usage_stats_for_a_limited_model(): void
])
]);
factory(Post::class)->create(['user_id' => $this->user->id]);
$this->assertEquals([
'used' => 1,
'available' => 1,
'remaining' => 0
], $this->user->getUsageFor('posts'));
$usage = $this->user->getUsageFor('posts');
$this->assertEquals(1, $usage->used);
$this->assertEquals(1, $usage->available);
$this->assertEquals(0, $usage->remaining);
}

/** @test
Expand All @@ -79,11 +78,10 @@ public function can_get_the_usage_stats_for_an_unlimited_model(): void
]),
]);
factory(Image::class)->create(['imageable_id' => $this->user->id, 'imageable_type' => User::class]);
$this->assertEquals([
'used' => 1,
'available' => 9999,
'remaining' => 9998
], $this->user->getUsageFor('images'));
$usage = $this->user->getUsageFor('images');
$this->assertEquals(1, $usage->used);
$this->assertEquals(9999, $usage->available);
$this->assertEquals(9998, $usage->remaining);
}

/**
Expand Down Expand Up @@ -128,20 +126,20 @@ public function can_get_usage_stats_for_all_limited_relations(): void
factory(Image::class)->create(['imageable_id' => $firstPost->id, 'imageable_type' => Post::class]);
factory(Image::class)->create(['imageable_id' => $firstPost->id, 'imageable_type' => Post::class]);
$this->assertEquals([
'images' => [
'images' => (object)[
'used' => 2,
'available' => 2,
'remaining' => 0
],
'posts' => [
'posts' => (object)[
'used' => 2,
'available' => 1,
'remaining' => -1
]
], $this->user->getUsages());

$this->assertEquals([
'images' => [
'images' => (object)[
'used' => 2,
'available' => 9999,
'remaining' => 9997
Expand Down

0 comments on commit fca058d

Please sign in to comment.