mirror of
https://github.com/michaelachrisco/ReadOnlyTraitLaravel.git
synced 2024-12-26 23:57:21 -08:00
Throw ReadOnlyException on writable eloquent model methods. (#14)
Travis CI Testing on PHP 7.1
This commit is contained in:
parent
e0dffadac0
commit
3e39985b40
5 changed files with 171 additions and 117 deletions
|
@ -4,7 +4,7 @@ php:
|
||||||
- 5.5.9
|
- 5.5.9
|
||||||
- 5.5
|
- 5.5
|
||||||
- 5.6
|
- 5.6
|
||||||
- 7.0
|
- 7.1
|
||||||
- hhvm
|
- hhvm
|
||||||
|
|
||||||
env:
|
env:
|
||||||
|
|
|
@ -15,19 +15,20 @@ composer require michaelachrisco/readonly
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use MichaelAChrisco\ReadOnly\ReadOnlyTrait;
|
||||||
class User extends Model {
|
class User extends Model {
|
||||||
use \MichaelAChrisco\ReadOnly\ReadOnlyTrait;
|
use ReadOnlyTrait;
|
||||||
}
|
}
|
||||||
|
|
||||||
$legacyUser = new User;
|
$legacyUser = new User;
|
||||||
$legacyUser->set_user_name('bob');
|
$legacyUser->set_user_name('bob');
|
||||||
|
|
||||||
$result = $legacyUser->save();
|
$result = $legacyUser->save();
|
||||||
//User is not saved and $result is false.
|
//User is not saved and ReadOnlyException is thrown.
|
||||||
?>
|
?>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Methods that will return false:
|
## Methods that will throw ReadOnlyExceptions:
|
||||||
|
|
||||||
* create
|
* create
|
||||||
* forceCreate
|
* forceCreate
|
||||||
|
|
|
@ -1,118 +1,148 @@
|
||||||
<?php
|
<?php
|
||||||
require_once('src/ReadOnlyTrait.php');
|
require_once('src/ReadOnlyTrait.php');
|
||||||
// use MichaelAChrisco\ReadOnlyTrait;
|
use MichaelAChrisco\ReadOnly\ReadOnlyException,
|
||||||
use Illuminate\Database\Eloquent\Model;
|
MichaelAChrisco\ReadOnly\ReadOnlyTrait,
|
||||||
|
Illuminate\Database\Eloquent\Model;
|
||||||
|
// use Illuminate\Database\Eloquent\Builder;
|
||||||
|
|
||||||
class User extends Illuminate\Database\Eloquent\Model {
|
class User extends Illuminate\Database\Eloquent\Model {
|
||||||
use MichaelAChrisco\ReadOnly\ReadOnlyTrait;
|
use ReadOnlyTrait;
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("User", function() {
|
describe("User", function() {
|
||||||
describe("::create()", function(){
|
describe("::create()", function(){
|
||||||
it("is expected to return false", function() {
|
it("is expected to throw ReadOnlyException", function() {
|
||||||
|
expect(
|
||||||
|
function(){
|
||||||
$user = new User;
|
$user = new User;
|
||||||
expect($user->create([]))->toBe(false);
|
$user->create([]);
|
||||||
unset($user);
|
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("::forceCreate()", function(){
|
describe("::forceCreate()", function(){
|
||||||
it("is expected to return false", function() {
|
it("is expected to throw ReadOnlyException", function() {
|
||||||
|
$closure = function(){
|
||||||
$user = new User;
|
$user = new User;
|
||||||
expect($user->forceCreate([]))->toBe(false);
|
$user->forceCreate([]);
|
||||||
unset($user);
|
};
|
||||||
|
expect($closure)->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("::save()", function(){
|
describe("::save()", function(){
|
||||||
|
it("is expected to throw ReadOnlyException", function() {
|
||||||
it("is expected to return false", function() {
|
expect(
|
||||||
|
function(){
|
||||||
$user = new User;
|
$user = new User;
|
||||||
expect($user->save([]))->toBe(false);
|
$user->save([]);
|
||||||
unset($user);
|
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("::update()", function(){
|
describe("::update()", function(){
|
||||||
it("is expected to return false", function() {
|
it("is expected to throw ReadOnlyException", function() {
|
||||||
|
expect(
|
||||||
|
function(){
|
||||||
$user = new User;
|
$user = new User;
|
||||||
expect($user->update([]))->toBe(false);
|
$user->update([]);
|
||||||
unset($user);
|
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("::firstOrCreate()", function(){
|
describe("::firstOrCreate()", function(){
|
||||||
it("is expected to return false", function() {
|
it("is expected to throw ReadOnlyException", function() {
|
||||||
|
expect(
|
||||||
|
function(){
|
||||||
$user = new User;
|
$user = new User;
|
||||||
expect($user->firstOrCreate([]))->toBe(false);
|
$user->firstOrCreate([]);
|
||||||
unset($user);
|
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("::firstOrNew()", function(){
|
describe("::firstOrNew()", function(){
|
||||||
it("is expected to return false", function() {
|
it("is expected to throw ReadOnlyException", function() {
|
||||||
|
expect(
|
||||||
|
function(){
|
||||||
$user = new User;
|
$user = new User;
|
||||||
expect($user->firstOrNew([]))->toBe(false);
|
$user->firstOrNew([]);
|
||||||
unset($user);
|
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("::delete()", function(){
|
describe("::delete()", function(){
|
||||||
it("is expected to return false", function() {
|
it("is expected to throw ReadOnlyException", function() {
|
||||||
|
expect(
|
||||||
|
function(){
|
||||||
$user = new User;
|
$user = new User;
|
||||||
expect($user->delete())->toBe(false);
|
$user->delete();
|
||||||
unset($user);
|
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("::destroy()", function(){
|
describe("::destroy()", function(){
|
||||||
it("is expected to return false", function() {
|
it("is expected to throw ReadOnlyException", function() {
|
||||||
|
expect(
|
||||||
|
function(){
|
||||||
$user = new User;
|
$user = new User;
|
||||||
expect($user->destroy(1))->toBe(false);
|
$user->destroy(1);
|
||||||
unset($user);
|
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("::restore()", function(){
|
describe("::restore()", function(){
|
||||||
it("is expected to return false", function() {
|
it("is expected to throw ReadOnlyException", function() {
|
||||||
|
expect(
|
||||||
|
function(){
|
||||||
$user = new User;
|
$user = new User;
|
||||||
expect($user->restore())->toBe(false);
|
$user->restore();
|
||||||
unset($user);
|
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("::forceDelete()", function(){
|
describe("::forceDelete()", function(){
|
||||||
it("is expected to return false", function() {
|
it("is expected to throw ReadOnlyException", function() {
|
||||||
|
expect(
|
||||||
|
function(){
|
||||||
$user = new User;
|
$user = new User;
|
||||||
expect($user->forceDelete())->toBe(false);
|
$user->forceDelete();
|
||||||
unset($user);
|
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("::performDeleteOnModel()", function(){
|
describe("::performDeleteOnModel()", function(){
|
||||||
it("is expected to return false", function() {
|
it("is expected to throw ReadOnlyException", function() {
|
||||||
|
expect(
|
||||||
|
function(){
|
||||||
$user = new User;
|
$user = new User;
|
||||||
expect($user->performDeleteOnModel())->toBe(false);
|
$user->performDeleteOnModel();
|
||||||
unset($user);
|
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("::push()", function(){
|
describe("::push()", function(){
|
||||||
it("is expected to return false", function() {
|
it("is expected to throw ReadOnlyException", function() {
|
||||||
|
expect(
|
||||||
|
function(){
|
||||||
$user = new User;
|
$user = new User;
|
||||||
expect($user->push())->toBe(false);
|
$user->push();
|
||||||
unset($user);
|
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("::finishSave()", function(){
|
describe("::finishSave()", function(){
|
||||||
it("is expected to return false", function() {
|
it("is expected to throw ReadOnlyException", function() {
|
||||||
|
expect(
|
||||||
|
function(){
|
||||||
$user = new User;
|
$user = new User;
|
||||||
expect($user->finishSave([]))->toBe(false);
|
$user->finishSave([]);
|
||||||
unset($user);
|
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("::performUpdate()", function(){
|
describe("::performUpdate()", function(){
|
||||||
it("is expected to return false", function() {
|
it("is expected to throw ReadOnlyException", function() {
|
||||||
$user = new User;
|
$user = new User;
|
||||||
//TODO: Mock up
|
//TODO: Mock up
|
||||||
// $query = new Illuminate\Database\Eloquent\Builder('');
|
// $user = new User;
|
||||||
// expect($user->performUpdate($query))->toBe(false);
|
// $user->performUpdate(new Builder, []);
|
||||||
unset($user);
|
unset($user);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("::touch()", function(){
|
describe("::touch()", function(){
|
||||||
it("is expected to return false", function() {
|
it("is expected to throw Error", function() {
|
||||||
|
expect(
|
||||||
|
function(){
|
||||||
$user = new User;
|
$user = new User;
|
||||||
expect($user->touch())->toBe(false);
|
$user->touch();
|
||||||
unset($user);
|
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
7
src/ReadOnlyException.php
Normal file
7
src/ReadOnlyException.php
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<?php
|
||||||
|
namespace MichaelAChrisco\ReadOnly;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
class ReadOnlyException extends RuntimeException{
|
||||||
|
|
||||||
|
}
|
|
@ -1,148 +1,164 @@
|
||||||
<?php
|
<?php
|
||||||
namespace MichaelAChrisco\ReadOnly;
|
namespace MichaelAChrisco\ReadOnly;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use MichaelAChrisco\ReadOnly\ReadOnlyException;
|
||||||
|
|
||||||
trait ReadOnlyTrait {
|
trait ReadOnlyTrait {
|
||||||
/**
|
/**
|
||||||
* returns false on create
|
* throws ReadOnlyException on create
|
||||||
* @method create
|
* @method create
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
static function create(array $attributes = []){
|
static function create(array $attributes = []){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns false on forceCreate
|
* throws ReadOnlyException on forceCreate
|
||||||
* @method forceCreate
|
* @method forceCreate
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
static function forceCreate(array $attributes){
|
static function forceCreate(array $attributes){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns false on save
|
* throws ReadOnlyException on save
|
||||||
* @method save
|
* @method save
|
||||||
* @param array $options
|
* @param array $options
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
public function save(array $options = []){
|
public function save(array $options = []){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns false on update
|
* throws ReadOnlyException on update
|
||||||
* @method update
|
* @method update
|
||||||
* @param [type] $attributes
|
* @param [type] $attributes
|
||||||
* @param [type] $options
|
* @param $options
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
public function update(array $attributes = [], array $options = []){
|
public function update(array $attributes = [], array $options = []){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns false on firstOrCreate
|
* throws ReadOnlyException on firstOrCreate
|
||||||
* @method firstOrCreate
|
* @method firstOrCreate
|
||||||
* @param array $arr
|
* @param array $arr
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
static function firstOrCreate(array $arr){
|
static function firstOrCreate(array $arr){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns false on firstOrNew
|
* throws ReadOnlyException on firstOrNew
|
||||||
* @method firstOrNew
|
* @method firstOrNew
|
||||||
* @param array $arr
|
* @param array $arr
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
static function firstOrNew(array $arr){
|
static function firstOrNew(array $arr){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns false on delete
|
* throws ReadOnlyException on delete
|
||||||
* @method delete
|
* @method delete
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
public function delete(){
|
public function delete(){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns false on destroy
|
* throws ReadOnlyException on destroy
|
||||||
* @method destroy
|
* @method destroy
|
||||||
* @param mixed $ids
|
* @param mixed $ids
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
static function destroy($ids){
|
static function destroy($ids){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns false on restore
|
* throws ReadOnlyException on restore
|
||||||
* @method restore
|
* @method restore
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
public function restore(){
|
public function restore(){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns false on forceDelete
|
* throws ReadOnlyException on forceDelete
|
||||||
* @method forceDelete
|
* @method forceDelete
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
public function forceDelete(){
|
public function forceDelete(){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns false on performDeleteOnModel
|
* throws ReadOnlyException on performDeleteOnModel
|
||||||
* @method performDeleteOnModel
|
* @method performDeleteOnModel
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
public function performDeleteOnModel(){
|
public function performDeleteOnModel(){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns false on push
|
* throws ReadOnlyException on push
|
||||||
* @method push
|
* @method push
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
public function push(){
|
public function push(){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns false on finishSave
|
* throws ReadOnlyException on finishSave
|
||||||
* @method finishSave
|
* @method finishSave
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
public function finishSave(array $options){
|
public function finishSave(array $options){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns false on performUpdate
|
* throws ReadOnlyException on performUpdate
|
||||||
* @method performUpdate
|
* @method performUpdate
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
public function performUpdate(Builder $query, array $options = []){
|
public function performUpdate(Builder $query, array $options = []){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns false on touch
|
* throws ReadOnlyException on touch
|
||||||
* @method touch
|
* @method touch
|
||||||
* @return false
|
*
|
||||||
*/
|
*/
|
||||||
public function touch(){
|
public function touch(){
|
||||||
return false;
|
$class = get_called_class();
|
||||||
|
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue