Skip to content

Commit dca9d5b

Browse files
simbigSimon Bigelmayrmichaelachrisco
authored
feat: add possibility to disable the trait programmatically (#43)
* feat: add possibility to disable the trait programmatically * test: add test draft * Attempt at getting a workable test. * Test ugly mockable solution for happy path Co-authored-by: Simon Bigelmayr <[email protected]> Co-authored-by: Michael chrisco <[email protected]>
1 parent 1cd6556 commit dca9d5b

File tree

2 files changed

+104
-20
lines changed

2 files changed

+104
-20
lines changed

spec/ReadOnlyTraitSpec.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,30 @@ function(){
163163
});
164164
});
165165
});
166+
167+
class MockModel
168+
{
169+
public static function create(array $attributes = [])
170+
{
171+
return true;
172+
}
173+
}
174+
175+
class UserReadOnlyNotActive extends MockModel
176+
{
177+
use ReadOnlyTrait;
178+
179+
protected static function isActive(): bool
180+
{
181+
return false;
182+
}
183+
}
184+
185+
describe("UserReadOnlyNotActive", function () {
186+
describe("::create()", function () {
187+
it("expects `create()` to be toBeTruthy", function() {
188+
$user = new UserReadOnlyNotActive;
189+
expect($user->create([]))->toBeTruthy();
190+
});
191+
});
192+
});

src/ReadOnlyTrait.php

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22
namespace MichaelAChrisco\ReadOnly;
33

44
use Illuminate\Database\Eloquent\Builder;
5-
use MichaelAChrisco\ReadOnly\ReadOnlyException;
65

76
trait ReadOnlyTrait
87
{
8+
protected static function isActive(): bool {
9+
return true;
10+
}
11+
912
/**
1013
* Throws ReadOnlyException on create
1114
* @param array $attributes
1215
* @throws ReadOnlyException
1316
*/
1417
public static function create(array $attributes = [])
1518
{
16-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
19+
if (static::isActive()) {
20+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
21+
}
22+
return parent::create($attributes);
1723
}
1824

1925
/**
@@ -23,7 +29,10 @@ public static function create(array $attributes = [])
2329
*/
2430
public static function forceCreate(array $attributes)
2531
{
26-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
32+
if (static::isActive()) {
33+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
34+
}
35+
return parent::forceCreate($attributes);
2736
}
2837

2938
/**
@@ -33,7 +42,10 @@ public static function forceCreate(array $attributes)
3342
*/
3443
public function save(array $options = [])
3544
{
36-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
45+
if (static::isActive()) {
46+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
47+
}
48+
return parent::save($options);
3749
}
3850

3951
/**
@@ -44,7 +56,10 @@ public function save(array $options = [])
4456
*/
4557
public function update(array $attributes = [], array $options = [])
4658
{
47-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
59+
if (static::isActive()) {
60+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
61+
}
62+
return parent::update($attributes, $options);
4863
}
4964

5065
/**
@@ -55,7 +70,10 @@ public function update(array $attributes = [], array $options = [])
5570
*/
5671
public static function firstOrCreate(array $attributes, array $values = [])
5772
{
58-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
73+
if (static::isActive()) {
74+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
75+
}
76+
return parent::firstOrCreate( $attributes, $values);
5977
}
6078

6179
/**
@@ -66,7 +84,10 @@ public static function firstOrCreate(array $attributes, array $values = [])
6684
*/
6785
public static function firstOrNew(array $attributes, array $values = [])
6886
{
69-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
87+
if (static::isActive()) {
88+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
89+
}
90+
return parent::firstOrNew( $attributes, $values);
7091
}
7192

7293
/**
@@ -77,7 +98,10 @@ public static function firstOrNew(array $attributes, array $values = [])
7798
*/
7899
public static function updateOrCreate(array $attributes, array $values = [])
79100
{
80-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
101+
if (static::isActive()) {
102+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
103+
}
104+
return parent::updateOrCreate( $attributes, $values );
81105
}
82106

83107
/**
@@ -86,7 +110,10 @@ public static function updateOrCreate(array $attributes, array $values = [])
86110
*/
87111
public function delete()
88112
{
89-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
113+
if (static::isActive()) {
114+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
115+
}
116+
return parent::delete();
90117
}
91118

92119
/**
@@ -96,7 +123,10 @@ public function delete()
96123
*/
97124
public static function destroy($ids)
98125
{
99-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
126+
if (static::isActive()) {
127+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
128+
}
129+
return parent::destroy($ids);
100130
}
101131

102132
/**
@@ -105,7 +135,10 @@ public static function destroy($ids)
105135
*/
106136
public function restore()
107137
{
108-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
138+
if (static::isActive()) {
139+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
140+
}
141+
return parent::restore();
109142
}
110143

111144
/**
@@ -114,7 +147,10 @@ public function restore()
114147
*/
115148
public function forceDelete()
116149
{
117-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
150+
if (static::isActive()) {
151+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
152+
}
153+
return parent::forceDelete();
118154
}
119155

120156
/**
@@ -123,7 +159,10 @@ public function forceDelete()
123159
*/
124160
public function performDeleteOnModel()
125161
{
126-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
162+
if (static::isActive()) {
163+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
164+
}
165+
parent::performDeleteOnModel();
127166
}
128167

129168
/**
@@ -132,7 +171,10 @@ public function performDeleteOnModel()
132171
*/
133172
public function push()
134173
{
135-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
174+
if (static::isActive()) {
175+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
176+
}
177+
return parent::push();
136178
}
137179

138180
/**
@@ -142,7 +184,10 @@ public function push()
142184
*/
143185
public function finishSave(array $options)
144186
{
145-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
187+
if (static::isActive()) {
188+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
189+
}
190+
parent::finishSave($options);
146191
}
147192

148193
/**
@@ -151,9 +196,12 @@ public function finishSave(array $options)
151196
* @param array $options
152197
* @throws ReadOnlyException
153198
*/
154-
public function performUpdate(Builder $query, array $options = [])
199+
public function performUpdate(Builder $query, array $options = []): bool
155200
{
156-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
201+
if (static::isActive()) {
202+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
203+
}
204+
return parent::performUpdate($query, $options);
157205
}
158206

159207
/**
@@ -163,7 +211,10 @@ public function performUpdate(Builder $query, array $options = [])
163211
*/
164212
public function touch($attribute = null)
165213
{
166-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
214+
if (static::isActive()) {
215+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
216+
}
217+
return parent::touch($attribute);
167218
}
168219

169220
/**
@@ -172,7 +223,10 @@ public function touch($attribute = null)
172223
*/
173224
public function insert()
174225
{
175-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
226+
if (static::isActive()) {
227+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
228+
}
229+
return parent::insert();
176230
}
177231

178232
/**
@@ -181,6 +235,9 @@ public function insert()
181235
*/
182236
public function truncate()
183237
{
184-
throw new ReadOnlyException(__FUNCTION__, get_called_class());
238+
if (static::isActive()) {
239+
throw new ReadOnlyException(__FUNCTION__, get_called_class());
240+
}
241+
return parent::truncate();
185242
}
186243
}

0 commit comments

Comments
 (0)