From 3e39985b4018985e6370c4d36a8afd3d4d3667a3 Mon Sep 17 00:00:00 2001 From: Michael Chrisco Date: Mon, 27 Mar 2017 13:14:50 -0700 Subject: [PATCH] Throw ReadOnlyException on writable eloquent model methods. (#14) Travis CI Testing on PHP 7.1 --- .travis.yml | 2 +- README.md | 7 +- spec/ReadOnlyTraitSpec.php | 164 ++++++++++++++++++++++--------------- src/ReadOnlyException.php | 7 ++ src/ReadOnlyTrait.php | 108 +++++++++++++----------- 5 files changed, 171 insertions(+), 117 deletions(-) create mode 100644 src/ReadOnlyException.php diff --git a/.travis.yml b/.travis.yml index e320189..f05ac61 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ php: - 5.5.9 - 5.5 - 5.6 - - 7.0 + - 7.1 - hhvm env: diff --git a/README.md b/README.md index f1bbca7..0deb594 100644 --- a/README.md +++ b/README.md @@ -15,19 +15,20 @@ composer require michaelachrisco/readonly ```php 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 diff --git a/spec/ReadOnlyTraitSpec.php b/spec/ReadOnlyTraitSpec.php index 887b0d8..3076f92 100644 --- a/spec/ReadOnlyTraitSpec.php +++ b/spec/ReadOnlyTraitSpec.php @@ -1,119 +1,149 @@ 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")); + }); }); }); diff --git a/src/ReadOnlyException.php b/src/ReadOnlyException.php new file mode 100644 index 0000000..38f165b --- /dev/null +++ b/src/ReadOnlyException.php @@ -0,0 +1,7 @@ +