Skip to content

Commit a7999cb

Browse files
author
Pe Ell
authored
Merge pull request #5 from cybercog/feature/is-not-owned-by
Add isNotOwnedBy method
2 parents 3421c08 + 65bb536 commit a7999cb

File tree

8 files changed

+119
-6
lines changed

8 files changed

+119
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

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

5+
## 2.1.0 - 2016-12-21
6+
7+
### Added
8+
9+
- `isNotOwnedBy($owner)` to check if model not owned by concrete owner.
10+
511
## 2.0.0 - 2016-12-17
612

713
### Added

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ $article->hasOwner();
165165
$article->isOwnedBy($owner);
166166
```
167167

168+
#### Check not owned by owner
169+
170+
```php
171+
$article->isNotOwnedBy($owner);
172+
```
173+
168174
#### Manually define default owner on model creation
169175

170176
```php

src/Contracts/HasOwner.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ public function hasOwner();
101101
*/
102102
public function isOwnedBy(CanBeOwnerContract $owner);
103103

104+
/**
105+
* Checks if model not owned by given owner.
106+
*
107+
* @param \Cog\Ownership\Contracts\CanBeOwner $owner
108+
* @return bool
109+
*/
110+
public function isNotOwnedBy(CanBeOwnerContract $owner);
111+
104112
/**
105113
* Scope a query to only include models by owner.
106114
*

src/Traits/HasMorphOwner.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,17 @@ public function isOwnedBy(CanBeOwnerContract $owner)
168168
return $owner->getKey() === $this->getOwner()->getKey() && $owner->getMorphClass() === $this->getOwner()->getMorphClass();
169169
}
170170

171+
/**
172+
* Checks if model not owned by given owner.
173+
*
174+
* @param \Cog\Ownership\Contracts\CanBeOwner $owner
175+
* @return bool
176+
*/
177+
public function isNotOwnedBy(CanBeOwnerContract $owner)
178+
{
179+
return !$this->isOwnedBy($owner);
180+
}
181+
171182
/**
172183
* Scope a query to only include models by owner.
173184
*

src/Traits/HasOwner.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,17 @@ public function isOwnedBy(CanBeOwnerContract $owner)
173173
return $owner->getKey() === $this->getOwner()->getKey();
174174
}
175175

176+
/**
177+
* Checks if model not owned by given owner.
178+
*
179+
* @param \Cog\Ownership\Contracts\CanBeOwner $owner
180+
* @return bool
181+
*/
182+
public function isNotOwnedBy(CanBeOwnerContract $owner)
183+
{
184+
return !$this->isOwnedBy($owner);
185+
}
186+
176187
/**
177188
* Scope a query to only include models by owner.
178189
*

tests/unit/Traits/HasCustomizedOwnerTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function it_can_check_if_dont_have_owner()
9696
}
9797

9898
/** @test */
99-
public function it_can_check_if_owned_by()
99+
public function it_can_return_true_from_is_owned_by()
100100
{
101101
$group = factory(Group::class)->create();
102102
$entity = factory(EntityWithCustomizedOwner::class)->create([
@@ -107,7 +107,7 @@ public function it_can_check_if_owned_by()
107107
}
108108

109109
/** @test */
110-
public function it_can_check_if_not_group_id()
110+
public function it_can_return_false_from_is_owned_by()
111111
{
112112
$group = factory(Group::class)->create();
113113
$entity = factory(EntityWithCustomizedOwner::class)->create([
@@ -118,6 +118,29 @@ public function it_can_check_if_not_group_id()
118118
$this->assertFalse($entity->isOwnedBy($notOwnerUser));
119119
}
120120

121+
/** @test */
122+
public function it_can_return_true_from_is_not_owned_by()
123+
{
124+
$group = factory(Group::class)->create();
125+
$entity = factory(EntityWithCustomizedOwner::class)->create([
126+
'group_id' => $group->getKey(),
127+
]);
128+
$notOwner = factory(Group::class)->create();
129+
130+
$this->assertTrue($entity->isNotOwnedBy($notOwner));
131+
}
132+
133+
/** @test */
134+
public function it_can_return_false_from_is_not_owned_by()
135+
{
136+
$group = factory(Group::class)->create();
137+
$entity = factory(EntityWithCustomizedOwner::class)->create([
138+
'group_id' => $group->getKey(),
139+
]);
140+
141+
$this->assertFalse($entity->isNotOwnedBy($group));
142+
}
143+
121144
/** @test */
122145
public function it_can_scope_models_by_owner()
123146
{

tests/unit/Traits/HasMorphOwnerTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function it_can_check_if_dont_have_owner()
112112
}
113113

114114
/** @test */
115-
public function it_can_check_if_owned_by()
115+
public function it_can_return_true_from_is_owned_by()
116116
{
117117
$character = factory(Character::class)->create();
118118
$entity = factory(EntityWithMorphOwner::class)->create([
@@ -124,7 +124,7 @@ public function it_can_check_if_owned_by()
124124
}
125125

126126
/** @test */
127-
public function it_can_check_if_not_owned_by()
127+
public function it_can_return_false_from_is_owned_by()
128128
{
129129
$character = factory(Character::class)->create();
130130
$entity = factory(EntityWithMorphOwner::class)->create([
@@ -136,6 +136,31 @@ public function it_can_check_if_not_owned_by()
136136
$this->assertFalse($entity->isOwnedBy($notOwnerUser));
137137
}
138138

139+
/** @test */
140+
public function it_can_return_true_from_is_not_owned_by()
141+
{
142+
$character = factory(Character::class)->create();
143+
$entity = factory(EntityWithMorphOwner::class)->create([
144+
'owned_by_id' => $character->getKey(),
145+
'owned_by_type' => $character->getMorphClass(),
146+
]);
147+
$notOwnerUser = factory(Character::class)->create();
148+
149+
$this->assertTrue($entity->isNotOwnedBy($notOwnerUser));
150+
}
151+
152+
/** @test */
153+
public function it_can_return_false_from_is_not_owned_by()
154+
{
155+
$character = factory(Character::class)->create();
156+
$entity = factory(EntityWithMorphOwner::class)->create([
157+
'owned_by_id' => $character->getKey(),
158+
'owned_by_type' => $character->getMorphClass(),
159+
]);
160+
161+
$this->assertFalse($entity->isNotOwnedBy($character));
162+
}
163+
139164
/** @test */
140165
public function it_can_scope_models_by_owner()
141166
{

tests/unit/Traits/HasOwnerTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function it_can_check_if_dont_have_owner()
9696
}
9797

9898
/** @test */
99-
public function it_can_check_if_owned_by()
99+
public function it_can_return_true_from_is_owned_by()
100100
{
101101
$user = factory(User::class)->create();
102102
$entity = factory(EntityWithOwner::class)->create([
@@ -107,7 +107,7 @@ public function it_can_check_if_owned_by()
107107
}
108108

109109
/** @test */
110-
public function it_can_check_if_not_owned_by()
110+
public function it_can_return_false_from_is_owned_by()
111111
{
112112
$user = factory(User::class)->create();
113113
$entity = factory(EntityWithOwner::class)->create([
@@ -118,6 +118,29 @@ public function it_can_check_if_not_owned_by()
118118
$this->assertFalse($entity->isOwnedBy($notOwnerUser));
119119
}
120120

121+
/** @test */
122+
public function it_can_return_true_from_is_not_owned_by()
123+
{
124+
$user = factory(User::class)->create();
125+
$entity = factory(EntityWithOwner::class)->create([
126+
'owned_by' => $user->getKey(),
127+
]);
128+
$notOwnerUser = factory(User::class)->create();
129+
130+
$this->assertTrue($entity->isNotOwnedBy($notOwnerUser));
131+
}
132+
133+
/** @test */
134+
public function it_can_return_false_from_is_not_owned_by()
135+
{
136+
$user = factory(User::class)->create();
137+
$entity = factory(EntityWithOwner::class)->create([
138+
'owned_by' => $user->getKey(),
139+
]);
140+
141+
$this->assertFalse($entity->isNotOwnedBy($user));
142+
}
143+
121144
/** @test */
122145
public function it_can_scope_models_by_owner()
123146
{

0 commit comments

Comments
 (0)