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 <simon.bigelmayr@mll.com>
Co-authored-by: Michael chrisco <michaelachrisco@gmail.com>
This commit is contained in:
Simon Bigelmayr 2022-09-07 18:14:13 +02:00 committed by GitHub
parent 1cd655670f
commit dca9d5bef5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 104 additions and 20 deletions

View file

@ -163,3 +163,30 @@ describe("User", function() {
}); });
}); });
}); });
class MockModel
{
public static function create(array $attributes = [])
{
return true;
}
}
class UserReadOnlyNotActive extends MockModel
{
use ReadOnlyTrait;
protected static function isActive(): bool
{
return false;
}
}
describe("UserReadOnlyNotActive", function () {
describe("::create()", function () {
it("expects `create()` to be toBeTruthy", function() {
$user = new UserReadOnlyNotActive;
expect($user->create([]))->toBeTruthy();
});
});
});

View file

@ -2,10 +2,13 @@
namespace MichaelAChrisco\ReadOnly; namespace MichaelAChrisco\ReadOnly;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use MichaelAChrisco\ReadOnly\ReadOnlyException;
trait ReadOnlyTrait trait ReadOnlyTrait
{ {
protected static function isActive(): bool {
return true;
}
/** /**
* Throws ReadOnlyException on create * Throws ReadOnlyException on create
* @param array $attributes * @param array $attributes
@ -13,8 +16,11 @@ trait ReadOnlyTrait
*/ */
public static function create(array $attributes = []) public static function create(array $attributes = [])
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::create($attributes);
}
/** /**
* Throws ReadOnlyException on forceCreate * Throws ReadOnlyException on forceCreate
@ -23,8 +29,11 @@ trait ReadOnlyTrait
*/ */
public static function forceCreate(array $attributes) public static function forceCreate(array $attributes)
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::forceCreate($attributes);
}
/** /**
* Throws ReadOnlyException on save * Throws ReadOnlyException on save
@ -33,8 +42,11 @@ trait ReadOnlyTrait
*/ */
public function save(array $options = []) public function save(array $options = [])
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::save($options);
}
/** /**
* Throws ReadOnlyException on update * Throws ReadOnlyException on update
@ -44,8 +56,11 @@ trait ReadOnlyTrait
*/ */
public function update(array $attributes = [], array $options = []) public function update(array $attributes = [], array $options = [])
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::update($attributes, $options);
}
/** /**
* Throws ReadOnlyException on firstOrCreate * Throws ReadOnlyException on firstOrCreate
@ -55,8 +70,11 @@ trait ReadOnlyTrait
*/ */
public static function firstOrCreate(array $attributes, array $values = []) public static function firstOrCreate(array $attributes, array $values = [])
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::firstOrCreate( $attributes, $values);
}
/** /**
* Throws ReadOnlyException on firstOrNew * Throws ReadOnlyException on firstOrNew
@ -66,8 +84,11 @@ trait ReadOnlyTrait
*/ */
public static function firstOrNew(array $attributes, array $values = []) public static function firstOrNew(array $attributes, array $values = [])
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::firstOrNew( $attributes, $values);
}
/** /**
* Throws ReadOnlyException on updateOrCreate * Throws ReadOnlyException on updateOrCreate
@ -77,8 +98,11 @@ trait ReadOnlyTrait
*/ */
public static function updateOrCreate(array $attributes, array $values = []) public static function updateOrCreate(array $attributes, array $values = [])
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::updateOrCreate( $attributes, $values );
}
/** /**
* Throws ReadOnlyException on delete * Throws ReadOnlyException on delete
@ -86,8 +110,11 @@ trait ReadOnlyTrait
*/ */
public function delete() public function delete()
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::delete();
}
/** /**
* Throws ReadOnlyException on destroy * Throws ReadOnlyException on destroy
@ -96,8 +123,11 @@ trait ReadOnlyTrait
*/ */
public static function destroy($ids) public static function destroy($ids)
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::destroy($ids);
}
/** /**
* Throws ReadOnlyException on restore * Throws ReadOnlyException on restore
@ -105,8 +135,11 @@ trait ReadOnlyTrait
*/ */
public function restore() public function restore()
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::restore();
}
/** /**
* Throws ReadOnlyException on forceDelete * Throws ReadOnlyException on forceDelete
@ -114,8 +147,11 @@ trait ReadOnlyTrait
*/ */
public function forceDelete() public function forceDelete()
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::forceDelete();
}
/** /**
* Throws ReadOnlyException on performDeleteOnModel * Throws ReadOnlyException on performDeleteOnModel
@ -123,8 +159,11 @@ trait ReadOnlyTrait
*/ */
public function performDeleteOnModel() public function performDeleteOnModel()
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
parent::performDeleteOnModel();
}
/** /**
* Throws ReadOnlyException on push * Throws ReadOnlyException on push
@ -132,8 +171,11 @@ trait ReadOnlyTrait
*/ */
public function push() public function push()
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::push();
}
/** /**
* Throws ReadOnlyException on finishSave * Throws ReadOnlyException on finishSave
@ -142,8 +184,11 @@ trait ReadOnlyTrait
*/ */
public function finishSave(array $options) public function finishSave(array $options)
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
parent::finishSave($options);
}
/** /**
* Throws ReadOnlyException on performUpdate * Throws ReadOnlyException on performUpdate
@ -151,10 +196,13 @@ trait ReadOnlyTrait
* @param array $options * @param array $options
* @throws ReadOnlyException * @throws ReadOnlyException
*/ */
public function performUpdate(Builder $query, array $options = []) public function performUpdate(Builder $query, array $options = []): bool
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::performUpdate($query, $options);
}
/** /**
* Throws ReadOnlyException on touch * Throws ReadOnlyException on touch
@ -163,8 +211,11 @@ trait ReadOnlyTrait
*/ */
public function touch($attribute = null) public function touch($attribute = null)
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::touch($attribute);
}
/** /**
* Throws ReadOnlyException on insert * Throws ReadOnlyException on insert
@ -172,8 +223,11 @@ trait ReadOnlyTrait
*/ */
public function insert() public function insert()
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::insert();
}
/** /**
* Throws ReadOnlyException on truncate * Throws ReadOnlyException on truncate
@ -181,6 +235,9 @@ trait ReadOnlyTrait
*/ */
public function truncate() public function truncate()
{ {
if (static::isActive()) {
throw new ReadOnlyException(__FUNCTION__, get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
return parent::truncate();
}
} }