mirror of
https://github.com/michaelachrisco/ReadOnlyTraitLaravel.git
synced 2024-12-26 07:37:20 -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
|
||||
- 5.6
|
||||
- 7.0
|
||||
- 7.1
|
||||
- hhvm
|
||||
|
||||
env:
|
||||
|
|
|
@ -15,19 +15,20 @@ composer require michaelachrisco/readonly
|
|||
```php
|
||||
<?php
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use MichaelAChrisco\ReadOnly\ReadOnlyTrait;
|
||||
class User extends Model {
|
||||
use \MichaelAChrisco\ReadOnly\ReadOnlyTrait;
|
||||
use ReadOnlyTrait;
|
||||
}
|
||||
|
||||
$legacyUser = new User;
|
||||
$legacyUser->set_user_name('bob');
|
||||
|
||||
$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
|
||||
* forceCreate
|
||||
|
|
|
@ -1,119 +1,149 @@
|
|||
<?php
|
||||
require_once('src/ReadOnlyTrait.php');
|
||||
// use MichaelAChrisco\ReadOnlyTrait;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use MichaelAChrisco\ReadOnly\ReadOnlyException,
|
||||
MichaelAChrisco\ReadOnly\ReadOnlyTrait,
|
||||
Illuminate\Database\Eloquent\Model;
|
||||
// use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class User extends Illuminate\Database\Eloquent\Model {
|
||||
use MichaelAChrisco\ReadOnly\ReadOnlyTrait;
|
||||
use ReadOnlyTrait;
|
||||
}
|
||||
|
||||
describe("User", function() {
|
||||
describe("::create()", function(){
|
||||
it("is expected to return false", function() {
|
||||
$user = new User;
|
||||
expect($user->create([]))->toBe(false);
|
||||
unset($user);
|
||||
it("is expected to throw ReadOnlyException", function() {
|
||||
expect(
|
||||
function(){
|
||||
$user = new User;
|
||||
$user->create([]);
|
||||
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||
});
|
||||
});
|
||||
describe("::forceCreate()", function(){
|
||||
it("is expected to return false", function() {
|
||||
it("is expected to throw ReadOnlyException", function() {
|
||||
$closure = function(){
|
||||
$user = new User;
|
||||
expect($user->forceCreate([]))->toBe(false);
|
||||
unset($user);
|
||||
$user->forceCreate([]);
|
||||
};
|
||||
expect($closure)->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||
});
|
||||
});
|
||||
describe("::save()", function(){
|
||||
|
||||
it("is expected to return false", function() {
|
||||
$user = new User;
|
||||
expect($user->save([]))->toBe(false);
|
||||
unset($user);
|
||||
it("is expected to throw ReadOnlyException", function() {
|
||||
expect(
|
||||
function(){
|
||||
$user = new User;
|
||||
$user->save([]);
|
||||
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||
});
|
||||
});
|
||||
describe("::update()", function(){
|
||||
it("is expected to return false", function() {
|
||||
$user = new User;
|
||||
expect($user->update([]))->toBe(false);
|
||||
unset($user);
|
||||
it("is expected to throw ReadOnlyException", function() {
|
||||
expect(
|
||||
function(){
|
||||
$user = new User;
|
||||
$user->update([]);
|
||||
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||
});
|
||||
});
|
||||
describe("::firstOrCreate()", function(){
|
||||
it("is expected to return false", function() {
|
||||
$user = new User;
|
||||
expect($user->firstOrCreate([]))->toBe(false);
|
||||
unset($user);
|
||||
it("is expected to throw ReadOnlyException", function() {
|
||||
expect(
|
||||
function(){
|
||||
$user = new User;
|
||||
$user->firstOrCreate([]);
|
||||
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||
});
|
||||
});
|
||||
describe("::firstOrNew()", function(){
|
||||
it("is expected to return false", function() {
|
||||
$user = new User;
|
||||
expect($user->firstOrNew([]))->toBe(false);
|
||||
unset($user);
|
||||
it("is expected to throw ReadOnlyException", function() {
|
||||
expect(
|
||||
function(){
|
||||
$user = new User;
|
||||
$user->firstOrNew([]);
|
||||
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||
});
|
||||
});
|
||||
describe("::delete()", function(){
|
||||
it("is expected to return false", function() {
|
||||
$user = new User;
|
||||
expect($user->delete())->toBe(false);
|
||||
unset($user);
|
||||
it("is expected to throw ReadOnlyException", function() {
|
||||
expect(
|
||||
function(){
|
||||
$user = new User;
|
||||
$user->delete();
|
||||
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||
});
|
||||
});
|
||||
describe("::destroy()", function(){
|
||||
it("is expected to return false", function() {
|
||||
$user = new User;
|
||||
expect($user->destroy(1))->toBe(false);
|
||||
unset($user);
|
||||
it("is expected to throw ReadOnlyException", function() {
|
||||
expect(
|
||||
function(){
|
||||
$user = new User;
|
||||
$user->destroy(1);
|
||||
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||
});
|
||||
});
|
||||
describe("::restore()", function(){
|
||||
it("is expected to return false", function() {
|
||||
$user = new User;
|
||||
expect($user->restore())->toBe(false);
|
||||
unset($user);
|
||||
it("is expected to throw ReadOnlyException", function() {
|
||||
expect(
|
||||
function(){
|
||||
$user = new User;
|
||||
$user->restore();
|
||||
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||
});
|
||||
});
|
||||
describe("::forceDelete()", function(){
|
||||
it("is expected to return false", function() {
|
||||
$user = new User;
|
||||
expect($user->forceDelete())->toBe(false);
|
||||
unset($user);
|
||||
it("is expected to throw ReadOnlyException", function() {
|
||||
expect(
|
||||
function(){
|
||||
$user = new User;
|
||||
$user->forceDelete();
|
||||
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||
});
|
||||
});
|
||||
});
|
||||
describe("::performDeleteOnModel()", function(){
|
||||
it("is expected to return false", function() {
|
||||
$user = new User;
|
||||
expect($user->performDeleteOnModel())->toBe(false);
|
||||
unset($user);
|
||||
});
|
||||
it("is expected to throw ReadOnlyException", function() {
|
||||
expect(
|
||||
function(){
|
||||
$user = new User;
|
||||
$user->performDeleteOnModel();
|
||||
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||
});
|
||||
});
|
||||
describe("::push()", function(){
|
||||
it("is expected to return false", function() {
|
||||
$user = new User;
|
||||
expect($user->push())->toBe(false);
|
||||
unset($user);
|
||||
});
|
||||
it("is expected to throw ReadOnlyException", function() {
|
||||
expect(
|
||||
function(){
|
||||
$user = new User;
|
||||
$user->push();
|
||||
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||
});
|
||||
});
|
||||
describe("::finishSave()", function(){
|
||||
it("is expected to return false", function() {
|
||||
$user = new User;
|
||||
expect($user->finishSave([]))->toBe(false);
|
||||
unset($user);
|
||||
});
|
||||
it("is expected to throw ReadOnlyException", function() {
|
||||
expect(
|
||||
function(){
|
||||
$user = new User;
|
||||
$user->finishSave([]);
|
||||
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
|
||||
});
|
||||
});
|
||||
describe("::performUpdate()", function(){
|
||||
it("is expected to return false", function() {
|
||||
it("is expected to throw ReadOnlyException", function() {
|
||||
$user = new User;
|
||||
//TODO: Mock up
|
||||
// $query = new Illuminate\Database\Eloquent\Builder('');
|
||||
// expect($user->performUpdate($query))->toBe(false);
|
||||
// $user = new User;
|
||||
// $user->performUpdate(new Builder, []);
|
||||
unset($user);
|
||||
});
|
||||
});
|
||||
describe("::touch()", function(){
|
||||
it("is expected to return false", function() {
|
||||
$user = new User;
|
||||
expect($user->touch())->toBe(false);
|
||||
unset($user);
|
||||
});
|
||||
it("is expected to throw Error", function() {
|
||||
expect(
|
||||
function(){
|
||||
$user = new User;
|
||||
$user->touch();
|
||||
})->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
|
||||
namespace MichaelAChrisco\ReadOnly;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use MichaelAChrisco\ReadOnly\ReadOnlyException;
|
||||
|
||||
trait ReadOnlyTrait {
|
||||
/**
|
||||
* returns false on create
|
||||
* throws ReadOnlyException on create
|
||||
* @method create
|
||||
* @param array $attributes
|
||||
* @return false
|
||||
*
|
||||
*/
|
||||
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
|
||||
* @param array $attributes
|
||||
* @return false
|
||||
*
|
||||
*/
|
||||
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
|
||||
* @param array $options
|
||||
* @return false
|
||||
*
|
||||
*/
|
||||
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
|
||||
* @param [type] $attributes
|
||||
* @param [type] $options
|
||||
* @return false
|
||||
* @param $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
|
||||
* @param array $arr
|
||||
* @return false
|
||||
*
|
||||
*/
|
||||
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
|
||||
* @param array $arr
|
||||
* @return false
|
||||
*
|
||||
*/
|
||||
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
|
||||
* @return false
|
||||
*
|
||||
*/
|
||||
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
|
||||
* @param mixed $ids
|
||||
* @return false
|
||||
*
|
||||
*/
|
||||
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
|
||||
* @return false
|
||||
*
|
||||
*/
|
||||
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
|
||||
* @return false
|
||||
*
|
||||
*/
|
||||
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
|
||||
* @return false
|
||||
*
|
||||
*/
|
||||
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
|
||||
* @return false
|
||||
*
|
||||
*/
|
||||
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
|
||||
* @return false
|
||||
*
|
||||
*/
|
||||
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
|
||||
* @return false
|
||||
*
|
||||
*/
|
||||
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
|
||||
* @return false
|
||||
*
|
||||
*/
|
||||
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