Skip to content

Commit 1520dd9

Browse files
committed
Added Laravel 12 support
1 parent 7cbcf3b commit 1520dd9

15 files changed

+339
-128
lines changed

.github/workflows/unittests.yml

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,89 @@ jobs:
1212
laravel: 10.*
1313
testbench: 8.*
1414
composer-flag: '--prefer-stable'
15+
phpunit-config: 'phpunit-10.xml'
1516
- php: 8.2
1617
laravel: 10.*
1718
testbench: 8.*
1819
composer-flag: '--prefer-stable'
20+
phpunit-config: 'phpunit-10.xml'
1921
- php: 8.3
2022
laravel: 10.*
2123
testbench: 8.*
2224
composer-flag: '--prefer-stable'
25+
phpunit-config: 'phpunit-10.xml'
2326
- php: 8.1
2427
laravel: 10.*
2528
testbench: 8.*
2629
composer-flag: '--prefer-lowest'
30+
phpunit-config: 'phpunit-10.xml'
2731
- php: 8.2
2832
laravel: 10.*
2933
testbench: 8.*
3034
composer-flag: '--prefer-lowest'
35+
phpunit-config: 'phpunit-10.xml'
3136
- php: 8.3
3237
laravel: 10.*
3338
testbench: 8.*
3439
composer-flag: '--prefer-lowest'
40+
phpunit-config: 'phpunit-10.xml'
3541
# Laravel 11.*
3642
- php: 8.2
3743
laravel: 11.*
3844
testbench: 9.*
3945
composer-flag: '--prefer-stable'
46+
phpunit-config: 'phpunit.xml'
4047
- php: 8.3
4148
laravel: 11.*
4249
testbench: 9.*
4350
composer-flag: '--prefer-stable'
51+
phpunit-config: 'phpunit.xml'
4452
- php: 8.2
4553
laravel: 11.*
4654
testbench: 9.*
4755
composer-flag: '--prefer-lowest'
56+
phpunit-config: 'phpunit.xml'
4857
- php: 8.2
4958
laravel: 11.*
5059
testbench: 9.*
5160
composer-flag: '--prefer-lowest'
61+
phpunit-config: 'phpunit.xml'
62+
- php: 8.4
63+
laravel: 11.*
64+
testbench: 9.*
65+
composer-flag: '--prefer-stable'
66+
phpunit-config: 'phpunit.xml'
67+
# Laravel 12
68+
- php: 8.2
69+
laravel: 12.*
70+
testbench: 10.*
71+
composer-flag: '--prefer-stable'
72+
phpunit-config: 'phpunit.xml'
73+
- php: 8.3
74+
laravel: 12.*
75+
testbench: 10.*
76+
composer-flag: '--prefer-stable'
77+
phpunit-config: 'phpunit.xml'
78+
- php: 8.4
79+
laravel: 12.*
80+
testbench: 10.*
81+
composer-flag: '--prefer-stable'
82+
phpunit-config: 'phpunit.xml'
83+
- php: 8.2
84+
laravel: 12.*
85+
testbench: 10.*
86+
composer-flag: '--prefer-lowest'
87+
phpunit-config: 'phpunit.xml'
88+
- php: 8.3
89+
laravel: 12.*
90+
testbench: 10.*
91+
composer-flag: '--prefer-lowest'
92+
phpunit-config: 'phpunit.xml'
93+
- php: 8.4
94+
laravel: 12.*
95+
testbench: 10.*
96+
composer-flag: '--prefer-lowest'
97+
phpunit-config: 'phpunit.xml'
5298

5399
runs-on: ubuntu-latest
54100

@@ -69,7 +115,7 @@ jobs:
69115
run: composer update ${{ matrix.composer-flag }} --prefer-dist --no-interaction
70116

71117
- name: Run PHPUnit
72-
run: XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml
118+
run: XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --config="${{ matrix.phpunit-config }}" --coverage-clover=coverage.xml
73119

74120
- name: Upload coverage reports to Codecov
75121
uses: codecov/codecov-action@v5

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
"require": {
1414
"php": ">=8.1",
1515
"composer/composer": "^2",
16-
"illuminate/console": "^10|^11",
17-
"illuminate/database": "^10|^11",
18-
"illuminate/support": "^10|^11"
16+
"illuminate/console": "^10|^11|^12",
17+
"illuminate/database": "^10|^11|^12",
18+
"illuminate/support": "^10|^11|^12"
1919
},
2020
"require-dev": {
2121
"friendsofphp/php-cs-fixer": "^3",
22-
"larastan/larastan": "^2.9",
23-
"orchestra/testbench": "^8|^9",
24-
"phpunit/phpunit": "^10",
22+
"larastan/larastan": "^2|^3",
23+
"orchestra/testbench": "^8|^9|^10",
24+
"phpunit/phpunit": "^10|^11",
2525
"squizlabs/php_codesniffer": "^3.5"
2626
},
2727
"autoload": {
@@ -53,6 +53,6 @@
5353
"config": {
5454
"sort-packages": true
5555
},
56-
"minimum-stability": "dev",
56+
"minimum-stability": "stable",
5757
"prefer-stable": true
5858
}

phpunit-10.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false"
3+
colors="true" processIsolation="false" stopOnFailure="false"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache"
5+
backupStaticProperties="false">
6+
<coverage>
7+
<include>
8+
<directory suffix=".php">src/</directory>
9+
</include>
10+
</coverage>
11+
<testsuites>
12+
<testsuite name="Feature">
13+
<directory>tests/Feature</directory>
14+
</testsuite>
15+
</testsuites>
16+
<php>
17+
<env name="APP_ENV" value="testing"/>
18+
<env name="CACHE_DRIVER" value="array"/>
19+
<env name="SESSION_DRIVER" value="array"/>
20+
<env name="QUEUE_DRIVER" value="sync"/>
21+
<env name="DB_CONNECTION" value="sqlite"/>
22+
<env name="DB_DATABASE" value=":memory:"/>
23+
<server name="APP_ENV" value="testing"/>
24+
<server name="CACHE_DRIVER" value="array"/>
25+
<server name="SESSION_DRIVER" value="array"/>
26+
<server name="QUEUE_DRIVER" value="sync"/>
27+
<server name="DB_CONNECTION" value="sqlite"/>
28+
<server name="DB_DATABASE" value=":memory:"/>
29+
</php>
30+
</phpunit>

phpunit.xml

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3-
<coverage>
4-
<include>
5-
<directory suffix=".php">src/</directory>
6-
</include>
7-
</coverage>
8-
<testsuites>
9-
<testsuite name="Feature">
10-
<directory>tests/Feature</directory>
11-
</testsuite>
12-
</testsuites>
13-
<php>
14-
<env name="APP_ENV" value="testing"/>
15-
<env name="CACHE_DRIVER" value="array"/>
16-
<env name="SESSION_DRIVER" value="array"/>
17-
<env name="QUEUE_DRIVER" value="sync"/>
18-
<env name="DB_CONNECTION" value="sqlite"/>
19-
<env name="DB_DATABASE" value=":memory:"/>
20-
<server name="APP_ENV" value="testing"/>
21-
<server name="CACHE_DRIVER" value="array"/>
22-
<server name="SESSION_DRIVER" value="array"/>
23-
<server name="QUEUE_DRIVER" value="sync"/>
24-
<server name="DB_CONNECTION" value="sqlite"/>
25-
<server name="DB_DATABASE" value=":memory:"/>
26-
</php>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false"
3+
colors="true" processIsolation="false" stopOnFailure="false"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd" cacheDirectory=".phpunit.cache"
5+
backupStaticProperties="false">
6+
<testsuites>
7+
<testsuite name="Feature">
8+
<directory>tests/Feature</directory>
9+
</testsuite>
10+
</testsuites>
11+
<php>
12+
<env name="APP_ENV" value="testing"/>
13+
<env name="CACHE_DRIVER" value="array"/>
14+
<env name="SESSION_DRIVER" value="array"/>
15+
<env name="QUEUE_DRIVER" value="sync"/>
16+
<env name="DB_CONNECTION" value="sqlite"/>
17+
<env name="DB_DATABASE" value=":memory:"/>
18+
<server name="APP_ENV" value="testing"/>
19+
<server name="CACHE_DRIVER" value="array"/>
20+
<server name="SESSION_DRIVER" value="array"/>
21+
<server name="QUEUE_DRIVER" value="sync"/>
22+
<server name="DB_CONNECTION" value="sqlite"/>
23+
<server name="DB_DATABASE" value=":memory:"/>
24+
</php>
25+
<source>
26+
<include>
27+
<directory suffix=".php">src/</directory>
28+
</include>
29+
</source>
2730
</phpunit>

readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@ php artisan vendor:publish --tag=computed-attributes-config
3939

4040
This package is tested for the following Laravel and PHP versions:
4141

42+
- 12.* (PHP 8.2, 8.3, 8.4)
43+
- 11.* (PHP 8.2, 8.3, 8.4)
4244
- 10.* (PHP 8.1, 8.2, 8.3)
43-
- 11.* (PHP 8.2, 8.3)
4445

4546
## Usage examples
4647

4748
Here is an example of two computed attributes `complex_calculation` and `sum_of_votes`.
4849
The functions `getComplexCalculationComputed` and `getSumOfVotesComputed` are calculating the computed attributes.
4950

5051
```php
52+
use Illuminate\Database\Eloquent\Model;
5153
use Korridor\LaravelComputedAttributes\ComputedAttributes;
5254

53-
class Post {
55+
class Post extends Model {
5456

5557
use ComputedAttributes;
5658

src/ComputedAttributes.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
use Illuminate\Support\Str;
1010

1111
/**
12-
* @method static Builder|Model computedAttributesValidate(array $attributes)
13-
* @method static Builder|Model computedAttributesGenerate(array $attributes)
12+
* @method Builder<static> computedAttributesValidate(array<string> $attributes)
13+
* @method Builder<static> computedAttributesGenerate(array<string> $attributes)
14+
* @phpstan-require-extends Model
1415
*/
1516
trait ComputedAttributes
1617
{
@@ -41,9 +42,9 @@ public function setComputedAttributeValue(string $attributeName): void
4142
/**
4243
* This scope will be applied during the computed property generation with artisan computed-attributes:generate.
4344
*
44-
* @param Builder $builder
45-
* @param array $attributes Attributes that will be generated.
46-
* @return Builder
45+
* @param Builder<static> $builder
46+
* @param array<string> $attributes Attributes that will be generated.
47+
* @return Builder<static>
4748
*/
4849
public function scopeComputedAttributesGenerate(Builder $builder, array $attributes): Builder
4950
{
@@ -53,9 +54,9 @@ public function scopeComputedAttributesGenerate(Builder $builder, array $attribu
5354
/**
5455
* This scope will be applied during the computed property validation with artisan computed-attributes:validate.
5556
*
56-
* @param Builder $builder
57-
* @param array $attributes Attributes that will be validated.
58-
* @return Builder
57+
* @param Builder<static> $builder
58+
* @param array<string> $attributes Attributes that will be validated.
59+
* @return Builder<static>
5960
*/
6061
public function scopeComputedAttributesValidate(Builder $builder, array $attributes): Builder
6162
{

src/ComputedAttributesInterface.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Korridor\LaravelComputedAttributes;
6+
7+
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Support\Str;
10+
11+
/**
12+
* @method Builder<static> computedAttributesValidate(array<string> $attributes)
13+
* @method Builder<static> computedAttributesGenerate(array<string> $attributes)
14+
* @phpstan-require-extends Model
15+
*/
16+
interface ComputedAttributesInterface
17+
{
18+
/**
19+
* Compute the given attribute and return the result.
20+
*
21+
* @param string $attributeName
22+
* @return mixed
23+
*/
24+
public function getComputedAttributeValue(string $attributeName): mixed;
25+
26+
/**
27+
* Compute the given attribute and assign the result in the model.
28+
*
29+
* @param string $attributeName
30+
*/
31+
public function setComputedAttributeValue(string $attributeName): void;
32+
33+
/**
34+
* This scope will be applied during the computed property generation with artisan computed-attributes:generate.
35+
*
36+
* @template TModel of Model
37+
* @param Builder<TModel> $builder
38+
* @param array<string> $attributes Attributes that will be generated.
39+
* @return Builder<TModel>
40+
*/
41+
public function scopeComputedAttributesGenerate(Builder $builder, array $attributes): Builder;
42+
43+
/**
44+
* This scope will be applied during the computed property validation with artisan computed-attributes:validate.
45+
*
46+
* @template TModel of Model
47+
* @param Builder<TModel> $builder
48+
* @param array<string> $attributes Attributes that will be validated.
49+
* @return Builder<TModel>
50+
*/
51+
public function scopeComputedAttributesValidate(Builder $builder, array $attributes): Builder;
52+
53+
/**
54+
* Return the configuration array for this model.
55+
* If the configuration array does not exist the function will return an empty array.
56+
*
57+
* @return array<int, string>
58+
*/
59+
public function getComputedAttributeConfiguration(): array;
60+
}

0 commit comments

Comments
 (0)