Skip to content

Commit 3e39985

Browse files
Throw ReadOnlyException on writable eloquent model methods. (#14)
Travis CI Testing on PHP 7.1
1 parent e0dffad commit 3e39985

File tree

5 files changed

+171
-117
lines changed

5 files changed

+171
-117
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ php:
44
- 5.5.9
55
- 5.5
66
- 5.6
7-
- 7.0
7+
- 7.1
88
- hhvm
99

1010
env:

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,20 @@ composer require michaelachrisco/readonly
1515
```php
1616
<?php
1717
use Illuminate\Database\Eloquent\Model;
18+
use MichaelAChrisco\ReadOnly\ReadOnlyTrait;
1819
class User extends Model {
19-
use \MichaelAChrisco\ReadOnly\ReadOnlyTrait;
20+
use ReadOnlyTrait;
2021
}
2122

2223
$legacyUser = new User;
2324
$legacyUser->set_user_name('bob');
2425

2526
$result = $legacyUser->save();
26-
//User is not saved and $result is false.
27+
//User is not saved and ReadOnlyException is thrown.
2728
?>
2829
```
2930

30-
## Methods that will return false:
31+
## Methods that will throw ReadOnlyExceptions:
3132

3233
* create
3334
* forceCreate

spec/ReadOnlyTraitSpec.php

Lines changed: 97 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,149 @@
11
<?php
22
require_once('src/ReadOnlyTrait.php');
3-
// use MichaelAChrisco\ReadOnlyTrait;
4-
use Illuminate\Database\Eloquent\Model;
3+
use MichaelAChrisco\ReadOnly\ReadOnlyException,
4+
MichaelAChrisco\ReadOnly\ReadOnlyTrait,
5+
Illuminate\Database\Eloquent\Model;
6+
// use Illuminate\Database\Eloquent\Builder;
7+
58
class User extends Illuminate\Database\Eloquent\Model {
6-
use MichaelAChrisco\ReadOnly\ReadOnlyTrait;
9+
use ReadOnlyTrait;
710
}
811

912
describe("User", function() {
1013
describe("::create()", function(){
11-
it("is expected to return false", function() {
12-
$user = new User;
13-
expect($user->create([]))->toBe(false);
14-
unset($user);
14+
it("is expected to throw ReadOnlyException", function() {
15+
expect(
16+
function(){
17+
$user = new User;
18+
$user->create([]);
19+
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
1520
});
1621
});
1722
describe("::forceCreate()", function(){
18-
it("is expected to return false", function() {
23+
it("is expected to throw ReadOnlyException", function() {
24+
$closure = function(){
1925
$user = new User;
20-
expect($user->forceCreate([]))->toBe(false);
21-
unset($user);
26+
$user->forceCreate([]);
27+
};
28+
expect($closure)->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
2229
});
2330
});
2431
describe("::save()", function(){
25-
26-
it("is expected to return false", function() {
27-
$user = new User;
28-
expect($user->save([]))->toBe(false);
29-
unset($user);
32+
it("is expected to throw ReadOnlyException", function() {
33+
expect(
34+
function(){
35+
$user = new User;
36+
$user->save([]);
37+
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
3038
});
3139
});
3240
describe("::update()", function(){
33-
it("is expected to return false", function() {
34-
$user = new User;
35-
expect($user->update([]))->toBe(false);
36-
unset($user);
41+
it("is expected to throw ReadOnlyException", function() {
42+
expect(
43+
function(){
44+
$user = new User;
45+
$user->update([]);
46+
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
3747
});
3848
});
3949
describe("::firstOrCreate()", function(){
40-
it("is expected to return false", function() {
41-
$user = new User;
42-
expect($user->firstOrCreate([]))->toBe(false);
43-
unset($user);
50+
it("is expected to throw ReadOnlyException", function() {
51+
expect(
52+
function(){
53+
$user = new User;
54+
$user->firstOrCreate([]);
55+
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
4456
});
4557
});
4658
describe("::firstOrNew()", function(){
47-
it("is expected to return false", function() {
48-
$user = new User;
49-
expect($user->firstOrNew([]))->toBe(false);
50-
unset($user);
59+
it("is expected to throw ReadOnlyException", function() {
60+
expect(
61+
function(){
62+
$user = new User;
63+
$user->firstOrNew([]);
64+
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
5165
});
5266
});
5367
describe("::delete()", function(){
54-
it("is expected to return false", function() {
55-
$user = new User;
56-
expect($user->delete())->toBe(false);
57-
unset($user);
68+
it("is expected to throw ReadOnlyException", function() {
69+
expect(
70+
function(){
71+
$user = new User;
72+
$user->delete();
73+
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
5874
});
5975
});
6076
describe("::destroy()", function(){
61-
it("is expected to return false", function() {
62-
$user = new User;
63-
expect($user->destroy(1))->toBe(false);
64-
unset($user);
77+
it("is expected to throw ReadOnlyException", function() {
78+
expect(
79+
function(){
80+
$user = new User;
81+
$user->destroy(1);
82+
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
6583
});
6684
});
6785
describe("::restore()", function(){
68-
it("is expected to return false", function() {
69-
$user = new User;
70-
expect($user->restore())->toBe(false);
71-
unset($user);
86+
it("is expected to throw ReadOnlyException", function() {
87+
expect(
88+
function(){
89+
$user = new User;
90+
$user->restore();
91+
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
7292
});
7393
});
7494
describe("::forceDelete()", function(){
75-
it("is expected to return false", function() {
76-
$user = new User;
77-
expect($user->forceDelete())->toBe(false);
78-
unset($user);
95+
it("is expected to throw ReadOnlyException", function() {
96+
expect(
97+
function(){
98+
$user = new User;
99+
$user->forceDelete();
100+
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
101+
});
79102
});
80-
});
81103
describe("::performDeleteOnModel()", function(){
82-
it("is expected to return false", function() {
83-
$user = new User;
84-
expect($user->performDeleteOnModel())->toBe(false);
85-
unset($user);
86-
});
104+
it("is expected to throw ReadOnlyException", function() {
105+
expect(
106+
function(){
107+
$user = new User;
108+
$user->performDeleteOnModel();
109+
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
110+
});
87111
});
88112
describe("::push()", function(){
89-
it("is expected to return false", function() {
90-
$user = new User;
91-
expect($user->push())->toBe(false);
92-
unset($user);
93-
});
113+
it("is expected to throw ReadOnlyException", function() {
114+
expect(
115+
function(){
116+
$user = new User;
117+
$user->push();
118+
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
119+
});
94120
});
95121
describe("::finishSave()", function(){
96-
it("is expected to return false", function() {
97-
$user = new User;
98-
expect($user->finishSave([]))->toBe(false);
99-
unset($user);
100-
});
122+
it("is expected to throw ReadOnlyException", function() {
123+
expect(
124+
function(){
125+
$user = new User;
126+
$user->finishSave([]);
127+
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
128+
});
101129
});
102130
describe("::performUpdate()", function(){
103-
it("is expected to return false", function() {
131+
it("is expected to throw ReadOnlyException", function() {
104132
$user = new User;
105133
//TODO: Mock up
106-
// $query = new Illuminate\Database\Eloquent\Builder('');
107-
// expect($user->performUpdate($query))->toBe(false);
134+
// $user = new User;
135+
// $user->performUpdate(new Builder, []);
108136
unset($user);
109137
});
110138
});
111139
describe("::touch()", function(){
112-
it("is expected to return false", function() {
113-
$user = new User;
114-
expect($user->touch())->toBe(false);
115-
unset($user);
116-
});
140+
it("is expected to throw Error", function() {
141+
expect(
142+
function(){
143+
$user = new User;
144+
$user->touch();
145+
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
146+
});
117147
});
118148
});
119149

src/ReadOnlyException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace MichaelAChrisco\ReadOnly;
3+
use RuntimeException;
4+
5+
class ReadOnlyException extends RuntimeException{
6+
7+
}

0 commit comments

Comments
 (0)