Skip to content

Commit 5627a55

Browse files
jifermichaelachrisco
authored andcommitted
Small refactor (#18)
* Refactor for PSR-2 Formatting standard and PHP DockBlock standard. * Setting real php requirement (full param casting is available since php7) * php 7 for travis
1 parent 73a1308 commit 5627a55

File tree

5 files changed

+169
-185
lines changed

5 files changed

+169
-185
lines changed

.travis.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
language: php
22

33
php:
4-
- 5.5.9
5-
- 5.5
6-
- 5.6
7-
- 7.1
4+
- 7.0
85

96
env:
107
global:
118
- setup=basic
129

1310
matrix:
1411
include:
15-
- php: 5.5.9
12+
- php: 7.0
1613

1714
sudo: false
1815

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"psr-4": {"MichaelAChrisco\\ReadOnly\\": "src/"}
1212
},
1313
"require": {
14-
"php": ">=5.5.9"
14+
"php": ">=7.0.0"
1515
},
1616
"require-dev": {
1717
"kahlan/kahlan": "^2.4",

spec/ReadOnlyTraitSpec.php

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22
require_once('src/ReadOnlyTrait.php');
3-
use MichaelAChrisco\ReadOnly\ReadOnlyException,
4-
MichaelAChrisco\ReadOnly\ReadOnlyTrait,
5-
Illuminate\Database\Eloquent\Model;
6-
// use Illuminate\Database\Eloquent\Builder;
3+
4+
use MichaelAChrisco\ReadOnly\ReadOnlyException;
5+
use MichaelAChrisco\ReadOnly\ReadOnlyTrait;
76

87
class User extends Illuminate\Database\Eloquent\Model {
98
use ReadOnlyTrait;
@@ -16,7 +15,7 @@ class User extends Illuminate\Database\Eloquent\Model {
1615
function(){
1716
$user = new User;
1817
$user->create([]);
19-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
18+
})->toThrow(new ReadOnlyException('create', 'User'));
2019
});
2120
});
2221
describe("::forceCreate()", function(){
@@ -25,7 +24,7 @@ function(){
2524
$user = new User;
2625
$user->forceCreate([]);
2726
};
28-
expect($closure)->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
27+
expect($closure)->toThrow(new ReadOnlyException('forceCreate', 'User'));
2928
});
3029
});
3130
describe("::save()", function(){
@@ -34,7 +33,7 @@ function(){
3433
function(){
3534
$user = new User;
3635
$user->save([]);
37-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
36+
})->toThrow(new ReadOnlyException('save', 'User'));
3837
});
3938
});
4039
describe("::update()", function(){
@@ -43,7 +42,7 @@ function(){
4342
function(){
4443
$user = new User;
4544
$user->update([]);
46-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
45+
})->toThrow(new ReadOnlyException('update', 'User'));
4746
});
4847
});
4948
describe("::firstOrCreate()", function(){
@@ -52,7 +51,7 @@ function(){
5251
function(){
5352
$user = new User;
5453
$user->firstOrCreate([]);
55-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
54+
})->toThrow(new ReadOnlyException('firstOrCreate', 'User'));
5655
});
5756
});
5857
describe("::firstOrNew()", function(){
@@ -61,7 +60,7 @@ function(){
6160
function(){
6261
$user = new User;
6362
$user->firstOrNew([]);
64-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
63+
})->toThrow(new ReadOnlyException('firstOrNew', 'User'));
6564
});
6665
});
6766
describe("::delete()", function(){
@@ -70,7 +69,7 @@ function(){
7069
function(){
7170
$user = new User;
7271
$user->delete();
73-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
72+
})->toThrow(new ReadOnlyException('delete', 'User'));
7473
});
7574
});
7675
describe("::destroy()", function(){
@@ -79,7 +78,7 @@ function(){
7978
function(){
8079
$user = new User;
8180
$user->destroy(1);
82-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
81+
})->toThrow(new ReadOnlyException('destroy', 'User'));
8382
});
8483
});
8584
describe("::restore()", function(){
@@ -88,7 +87,7 @@ function(){
8887
function(){
8988
$user = new User;
9089
$user->restore();
91-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
90+
})->toThrow(new ReadOnlyException('restore', 'User'));
9291
});
9392
});
9493
describe("::forceDelete()", function(){
@@ -97,7 +96,7 @@ function(){
9796
function(){
9897
$user = new User;
9998
$user->forceDelete();
100-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
99+
})->toThrow(new ReadOnlyException('forceDelete', 'User'));
101100
});
102101
});
103102
describe("::performDeleteOnModel()", function(){
@@ -106,7 +105,7 @@ function(){
106105
function(){
107106
$user = new User;
108107
$user->performDeleteOnModel();
109-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
108+
})->toThrow(new ReadOnlyException('performDeleteOnModel', 'User'));
110109
});
111110
});
112111
describe("::push()", function(){
@@ -115,7 +114,7 @@ function(){
115114
function(){
116115
$user = new User;
117116
$user->push();
118-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
117+
})->toThrow(new ReadOnlyException('push', 'User'));
119118
});
120119
});
121120
describe("::finishSave()", function(){
@@ -124,7 +123,7 @@ function(){
124123
function(){
125124
$user = new User;
126125
$user->finishSave([]);
127-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
126+
})->toThrow(new ReadOnlyException('finishSave', 'User'));
128127
});
129128
});
130129
describe("::performUpdate()", function(){
@@ -142,7 +141,7 @@ function(){
142141
function(){
143142
$user = new User;
144143
$user->touch();
145-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
144+
})->toThrow(new ReadOnlyException('touch', 'User'));
146145
});
147146
});
148147
describe("::truncate()", function(){
@@ -151,7 +150,7 @@ function(){
151150
function(){
152151
$user = new User;
153152
$user->truncate();
154-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
153+
})->toThrow(new ReadOnlyException('truncate', 'User'));
155154
});
156155
});
157156
describe("::insert()", function(){
@@ -160,10 +159,7 @@ function(){
160159
function(){
161160
$user = new User;
162161
$user->insert();
163-
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User"));
162+
})->toThrow(new ReadOnlyException('insert', 'User'));
164163
});
165164
});
166165
});
167-
168-
169-
?>

src/ReadOnlyException.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,14 @@
33

44
class ReadOnlyException extends \RuntimeException
55
{
6-
}
6+
/**
7+
* @param string $functionName
8+
* @param string $modelClassName
9+
* {@inheritDoc}
10+
*/
11+
public function __construct(string $functionName, string $modelClassName, int $code = 0, \Throwable $previous = null)
12+
{
13+
$message = sprintf('Calling [%s] method on read-only model [%s] is not allowed.', $functionName, $modelClassName);
14+
parent::__construct($message, $code, $previous);
15+
}
16+
}

0 commit comments

Comments
 (0)