Skip to content

Commit 4f1177c

Browse files
committed
refactor: use assert in driver constructor
1 parent 76cee83 commit 4f1177c

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

Diff for: .github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd
2929
tools: composer:v2
3030
coverage: none
31-
ini-values: error_reporting=E_ALL
31+
ini-values: error_reporting=E_ALL, zend.assertions=1
3232

3333
- name: Set Laravel Version
3434
run: composer require "laravel/framework:^${{ matrix.laravel }}" --no-update -n

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file. This projec
99

1010
- Upgraded to Laravel 10.
1111
- Minimum PHP version is now 8.1.
12+
- Use `assert()` to ensure the soft delete boolean driver receives the correct type of model.
1213

1314
## [1.1.0] - 2022-09-14
1415

Diff for: src/Drivers/SoftDeleteBooleanDriver.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,23 @@
2121

2222
use Illuminate\Database\Eloquent\Builder;
2323
use Illuminate\Database\Eloquent\Model;
24-
use InvalidArgumentException;
2524
use LaravelJsonApi\Eloquent\Drivers\StandardDriver;
2625
use Webkid\LaravelBooleanSoftdeletes\SoftDeletesBoolean;
2726
use function boolval;
2827

2928
class SoftDeleteBooleanDriver extends StandardDriver
3029
{
31-
3230
/**
3331
* SoftDeleteDriver constructor.
3432
*
35-
* @param Model|SoftDeletesBoolean $model
33+
* @param Model&SoftDeletesBoolean $model
3634
*/
3735
public function __construct($model)
3836
{
39-
if (!in_array(SoftDeletesBoolean::class, class_uses_recursive($model), true)) {
40-
throw new InvalidArgumentException('Expecting a model that is boolean soft-deletable.');
41-
}
37+
assert(
38+
in_array(SoftDeletesBoolean::class, class_uses_recursive($model), true),
39+
'Expecting a model that is boolean soft-deletable.',
40+
);
4241

4342
parent::__construct($model);
4443
}
@@ -91,7 +90,7 @@ public function destroy(Model $model): bool
9190
}
9291

9392
/**
94-
* @param Model|SoftDeletesBoolean $model
93+
* @param Model&SoftDeletesBoolean $model
9594
* @return bool
9695
*/
9796
private function restore(Model $model): bool
@@ -102,7 +101,7 @@ private function restore(Model $model): bool
102101
/**
103102
* Will the hydration operation restore the model?
104103
*
105-
* @param Model|SoftDeletesBoolean $model
104+
* @param Model&SoftDeletesBoolean $model
106105
* @return bool
107106
*/
108107
private function willRestore(Model $model): bool
@@ -119,7 +118,7 @@ private function willRestore(Model $model): bool
119118
/**
120119
* Will the hydration operation result in the model being soft deleted?
121120
*
122-
* @param Model|SoftDeletesBoolean $model
121+
* @param Model&SoftDeletesBoolean $model
123122
* @return bool
124123
*/
125124
private function willSoftDelete(Model $model): bool

Diff for: tests/lib/Unit/.gitkeep

Whitespace-only changes.
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/*
3+
* Copyright 2023 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace LaravelJsonApi\BooleanSoftDeletes\Tests\Unit\Drivers;
21+
22+
use Illuminate\Database\Eloquent\Model;
23+
use Illuminate\Database\Eloquent\SoftDeletes;
24+
use LaravelJsonApi\BooleanSoftDeletes\Drivers\SoftDeleteBooleanDriver;
25+
use PHPUnit\Framework\TestCase;
26+
27+
class SoftDeleteBooleanDriverTest extends TestCase
28+
{
29+
/**
30+
* @return void
31+
*/
32+
public function testModelMustHaveBooleanSoftDeletesTrait(): void
33+
{
34+
$model = new class extends Model {
35+
use SoftDeletes;
36+
};
37+
38+
$this->expectException(\AssertionError::class);
39+
$this->expectExceptionMessage('Expecting a model that is boolean soft-deletable.');
40+
41+
new SoftDeleteBooleanDriver($model);
42+
}
43+
}

0 commit comments

Comments
 (0)