Skip to content

Commit

Permalink
Merge pull request #5 from cybercog/feature/is-not-owned-by
Browse files Browse the repository at this point in the history
Add isNotOwnedBy method
  • Loading branch information
Pe Ell authored Dec 21, 2016
2 parents 3421c08 + 65bb536 commit a7999cb
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to `laravel-ownership` will be documented in this file.

## 2.1.0 - 2016-12-21

### Added

- `isNotOwnedBy($owner)` to check if model not owned by concrete owner.

## 2.0.0 - 2016-12-17

### Added
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ $article->hasOwner();
$article->isOwnedBy($owner);
```

#### Check not owned by owner

```php
$article->isNotOwnedBy($owner);
```

#### Manually define default owner on model creation

```php
Expand Down
8 changes: 8 additions & 0 deletions src/Contracts/HasOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public function hasOwner();
*/
public function isOwnedBy(CanBeOwnerContract $owner);

/**
* Checks if model not owned by given owner.
*
* @param \Cog\Ownership\Contracts\CanBeOwner $owner
* @return bool
*/
public function isNotOwnedBy(CanBeOwnerContract $owner);

/**
* Scope a query to only include models by owner.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Traits/HasMorphOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,17 @@ public function isOwnedBy(CanBeOwnerContract $owner)
return $owner->getKey() === $this->getOwner()->getKey() && $owner->getMorphClass() === $this->getOwner()->getMorphClass();
}

/**
* Checks if model not owned by given owner.
*
* @param \Cog\Ownership\Contracts\CanBeOwner $owner
* @return bool
*/
public function isNotOwnedBy(CanBeOwnerContract $owner)
{
return !$this->isOwnedBy($owner);
}

/**
* Scope a query to only include models by owner.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Traits/HasOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ public function isOwnedBy(CanBeOwnerContract $owner)
return $owner->getKey() === $this->getOwner()->getKey();
}

/**
* Checks if model not owned by given owner.
*
* @param \Cog\Ownership\Contracts\CanBeOwner $owner
* @return bool
*/
public function isNotOwnedBy(CanBeOwnerContract $owner)
{
return !$this->isOwnedBy($owner);
}

/**
* Scope a query to only include models by owner.
*
Expand Down
27 changes: 25 additions & 2 deletions tests/unit/Traits/HasCustomizedOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function it_can_check_if_dont_have_owner()
}

/** @test */
public function it_can_check_if_owned_by()
public function it_can_return_true_from_is_owned_by()
{
$group = factory(Group::class)->create();
$entity = factory(EntityWithCustomizedOwner::class)->create([
Expand All @@ -107,7 +107,7 @@ public function it_can_check_if_owned_by()
}

/** @test */
public function it_can_check_if_not_group_id()
public function it_can_return_false_from_is_owned_by()
{
$group = factory(Group::class)->create();
$entity = factory(EntityWithCustomizedOwner::class)->create([
Expand All @@ -118,6 +118,29 @@ public function it_can_check_if_not_group_id()
$this->assertFalse($entity->isOwnedBy($notOwnerUser));
}

/** @test */
public function it_can_return_true_from_is_not_owned_by()
{
$group = factory(Group::class)->create();
$entity = factory(EntityWithCustomizedOwner::class)->create([
'group_id' => $group->getKey(),
]);
$notOwner = factory(Group::class)->create();

$this->assertTrue($entity->isNotOwnedBy($notOwner));
}

/** @test */
public function it_can_return_false_from_is_not_owned_by()
{
$group = factory(Group::class)->create();
$entity = factory(EntityWithCustomizedOwner::class)->create([
'group_id' => $group->getKey(),
]);

$this->assertFalse($entity->isNotOwnedBy($group));
}

/** @test */
public function it_can_scope_models_by_owner()
{
Expand Down
29 changes: 27 additions & 2 deletions tests/unit/Traits/HasMorphOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function it_can_check_if_dont_have_owner()
}

/** @test */
public function it_can_check_if_owned_by()
public function it_can_return_true_from_is_owned_by()
{
$character = factory(Character::class)->create();
$entity = factory(EntityWithMorphOwner::class)->create([
Expand All @@ -124,7 +124,7 @@ public function it_can_check_if_owned_by()
}

/** @test */
public function it_can_check_if_not_owned_by()
public function it_can_return_false_from_is_owned_by()
{
$character = factory(Character::class)->create();
$entity = factory(EntityWithMorphOwner::class)->create([
Expand All @@ -136,6 +136,31 @@ public function it_can_check_if_not_owned_by()
$this->assertFalse($entity->isOwnedBy($notOwnerUser));
}

/** @test */
public function it_can_return_true_from_is_not_owned_by()
{
$character = factory(Character::class)->create();
$entity = factory(EntityWithMorphOwner::class)->create([
'owned_by_id' => $character->getKey(),
'owned_by_type' => $character->getMorphClass(),
]);
$notOwnerUser = factory(Character::class)->create();

$this->assertTrue($entity->isNotOwnedBy($notOwnerUser));
}

/** @test */
public function it_can_return_false_from_is_not_owned_by()
{
$character = factory(Character::class)->create();
$entity = factory(EntityWithMorphOwner::class)->create([
'owned_by_id' => $character->getKey(),
'owned_by_type' => $character->getMorphClass(),
]);

$this->assertFalse($entity->isNotOwnedBy($character));
}

/** @test */
public function it_can_scope_models_by_owner()
{
Expand Down
27 changes: 25 additions & 2 deletions tests/unit/Traits/HasOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function it_can_check_if_dont_have_owner()
}

/** @test */
public function it_can_check_if_owned_by()
public function it_can_return_true_from_is_owned_by()
{
$user = factory(User::class)->create();
$entity = factory(EntityWithOwner::class)->create([
Expand All @@ -107,7 +107,7 @@ public function it_can_check_if_owned_by()
}

/** @test */
public function it_can_check_if_not_owned_by()
public function it_can_return_false_from_is_owned_by()
{
$user = factory(User::class)->create();
$entity = factory(EntityWithOwner::class)->create([
Expand All @@ -118,6 +118,29 @@ public function it_can_check_if_not_owned_by()
$this->assertFalse($entity->isOwnedBy($notOwnerUser));
}

/** @test */
public function it_can_return_true_from_is_not_owned_by()
{
$user = factory(User::class)->create();
$entity = factory(EntityWithOwner::class)->create([
'owned_by' => $user->getKey(),
]);
$notOwnerUser = factory(User::class)->create();

$this->assertTrue($entity->isNotOwnedBy($notOwnerUser));
}

/** @test */
public function it_can_return_false_from_is_not_owned_by()
{
$user = factory(User::class)->create();
$entity = factory(EntityWithOwner::class)->create([
'owned_by' => $user->getKey(),
]);

$this->assertFalse($entity->isNotOwnedBy($user));
}

/** @test */
public function it_can_scope_models_by_owner()
{
Expand Down

0 comments on commit a7999cb

Please sign in to comment.