Skip to content

Commit f0d65c5

Browse files
author
hareland
committed
Minor cleanup
- Update NS - Add arrow function to justify PHP8.1+ - Remove redundant test - Rename TestCase.php to LaravelTestCase.php
1 parent fa3e265 commit f0d65c5

9 files changed

+32
-45
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ composer require hareland/laravel-immutable-attributes
1616
## Define Immutable Attributes
1717

1818
Define the attributes to be immutable on your model:
19+
1920
```php
2021
<?php
2122

2223
namespace App\Models;
2324

2425
use Illuminate\Database\Eloquent\Model;
25-
use Hareland\Immutable\Traits\HasImmutableAttributes;
26+
use Hareland\LaravelImmutableAttributes\Traits\HasImmutableAttributes;
2627

2728
class Product extends Model
2829
{

composer.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
"license": "MIT",
66
"authors": [
77
{
8-
"name": "Kristian Hareland",
9-
"email": "[email protected]"
8+
"name": "Hareland",
9+
"email": "[email protected]",
10+
"homepage": "https://github.com/hareland"
1011
}
1112
],
1213
"require": {
@@ -23,18 +24,18 @@
2324
},
2425
"autoload": {
2526
"psr-4": {
26-
"Hareland\\Immutable\\": "src/"
27+
"Hareland\\LaravelImmutableAttributes\\": "src/"
2728
}
2829
},
2930
"autoload-dev": {
3031
"psr-4": {
31-
"Hareland\\Immutable\\Tests\\": "tests/"
32+
"Hareland\\LaravelImmutableAttributes\\Tests\\": "tests/"
3233
}
3334
},
3435
"extra": {
3536
"laravel": {
3637
"providers": [
37-
"Hareland\\Immutable\\ImmutableServiceProvider"
38+
"Hareland\\LaravelImmutableAttributes\\LaravelImmutableAttributesServiceProvider"
3839
]
3940
}
4041
},

src/LaravelImmutableAttributesServiceProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Hareland\Immutable;
3+
namespace Hareland\LaravelImmutableAttributes;
44

55

66
use Illuminate\Support\ServiceProvider;

src/Traits/HasImmutableAttributes.php

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
<?php
22

3-
namespace Hareland\Immutable\Traits;
3+
namespace Hareland\LaravelImmutableAttributes\Traits;
44

55
use Illuminate\Database\Eloquent\Model;
66

77
trait HasImmutableAttributes
88
{
9-
public static function bootImmutableAttributes(): void
9+
public static function bootHasImmutableAttributes(): void
1010
{
1111
static::updating(
12-
function (Model $model) {
13-
$this->resetImmutableAttributes($model);
14-
}
12+
fn(Model $model) => $model->resetImmutableAttributes($model),
1513
);
1614
}
1715

1816
private function resetImmutableAttributes(Model $model): void
1917
{
20-
collect($this->getImmutableAttributes())
21-
->each(
22-
function (string $attribute) use ($model) {
23-
if (!is_null($model->getOriginal($attribute))
24-
&& $model->getOriginal($attribute) !== $model->{$attribute}) {
25-
$model->{$attribute} = $model->getOriginal($attribute);
26-
}
27-
}
28-
);
18+
foreach ($this->getImmutableAttributes() as $attribute) {
19+
if (!is_null($model->getOriginal($attribute))
20+
&& $model->getOriginal($attribute) !== $model->{$attribute}) {
21+
$model->{$attribute} = $model->getOriginal($attribute);
22+
}
23+
}
2924
}
3025

3126
public function setAttribute($key, $value)

tests/ExampleImmutableModel.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Hareland\Immutable\Tests;
3+
namespace Hareland\LaravelImmutableAttributes\Tests;
44

55

6-
use Hareland\Immutable\Traits\HasImmutableAttributes;
6+
use Hareland\LaravelImmutableAttributes\Traits\HasImmutableAttributes;
77
use Illuminate\Database\Eloquent\Model;
88

99
/**

tests/TestCase.php renamed to tests/LaravelTestCase.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22

3-
namespace Hareland\Immutable\Tests;
3+
namespace Hareland\LaravelImmutableAttributes\Tests;
44

5-
use Hareland\Immutable\LaravelImmutableAttributesServiceProvider;
5+
use Hareland\LaravelImmutableAttributes\LaravelImmutableAttributesServiceProvider;
66
use Orchestra\Testbench\TestCase as Orchestra;
77

8-
class TestCase extends Orchestra
8+
class LaravelTestCase extends Orchestra
99
{
1010
protected function setUp(): void
1111
{

tests/Pest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

3-
use Hareland\Immutable\Tests\TestCase;
3+
use Hareland\LaravelImmutableAttributes\Tests\LaravelTestCase;
44

5-
uses(TestCase::class)->in(__DIR__);
5+
uses(LaravelTestCase::class)->in(__DIR__);
+6-16
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
<?php
22

3-
use Hareland\Immutable\Tests\ExampleImmutableModel;
3+
use Hareland\LaravelImmutableAttributes\Tests\ExampleImmutableModel;
44

55
const TEST_LABEL = 'Test';
66
const TEST_PRICE = 123.45;
7+
const TEST_OTHER_LABEL = 'other-Test';
78
const TEST_INVALID_PRICE = 456.78;
89

9-
10-
test('can fill a new model', function () {
11-
$model = new ExampleImmutableModel([
12-
'label' => TEST_LABEL,
13-
'price' => TEST_PRICE,
14-
]);
15-
16-
$model->save();
17-
18-
expect($model->exists)->toBe(true);
19-
20-
});
21-
22-
test('cannot fill immutable on update', function () {
10+
it('cannot fill immutable attributes on update', function () {
2311
$model = new ExampleImmutableModel([
2412
'label' => TEST_LABEL,
2513
'price' => TEST_PRICE,
@@ -30,9 +18,11 @@
3018
expect($model->label)->toBe(TEST_LABEL);
3119
expect($model->price)->toBe(TEST_PRICE);
3220

21+
$model->label = TEST_OTHER_LABEL;
3322
$model->price = TEST_INVALID_PRICE;
3423
$model->save();
35-
$model = $model->refresh();
3624

25+
//We expect the label to not be immutable, only the price.
26+
expect($model->label)->toBe(TEST_OTHER_LABEL);
3727
expect($model->price)->not()->toBe(TEST_INVALID_PRICE);
3828
});

tests/example_immutable_model_migration.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public function up()
2323

2424
public function down()
2525
{
26-
//..
26+
Schema::drop('example_immutable_model');
2727
}
2828
};

0 commit comments

Comments
 (0)