Skip to content

Commit

Permalink
Merge pull request #1 from carsdotcom/laravel-11-support
Browse files Browse the repository at this point in the history
Update the test runner to support Laravel 9, 10, 11
  • Loading branch information
bbene authored Jul 2, 2024
2 parents 5fd1dd8 + 0b06ff8 commit feb4906
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 44 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ jobs:

strategy:
matrix:
php: ['7.2', '7.3', '7.4', '8.0']
laravel: [5.8.*, 6.*, 7.*, 8.*]
exclude:
- php: 7.2
laravel: 8.*
php: ['8.2']
laravel: [9.*, 10.*, 11.*]
include:
- php: 8.0
laravel: 5.8.*
laravel: 9.*
- php: 8.1
laravel: 9.*
- php: 8.1
laravel: 10.*

name: Unit Tests - PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
steps:
- uses: actions/checkout@v1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ composer.lock
.idea
*.properties
nbproject/project.xml
.phpunit*
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Single Table Inheritance
[![Latest Unstable Version](https://poser.pugx.org/nanigans/single-table-inheritance/v/unstable.svg)](https://packagist.org/packages/nanigans/single-table-inheritance)
[![License](https://poser.pugx.org/nanigans/single-table-inheritance/license.svg)](https://packagist.org/packages/nanigans/single-table-inheritance)

Single Table Inheritance is a trait for Laravel 5.8+ Eloquent models that allows multiple models to be stored in the same database table. We support a few key features
Single Table Inheritance is a trait for Laravel 9+ Eloquent models that allows multiple models to be stored in the same database table. We support a few key features

* Implemented as a Trait so that it plays nice with others, such as Laravel's `SoftDeletingTrait` or the excellent [Validating](https://github.com/dwightwatson/validating), without requiring a complicated mess of Eloquent Model subclasses.
* Allow arbitrary class hierarchies not just two-level parent-child relationships.
Expand All @@ -22,15 +22,17 @@ Single Table Inheritance is a trait for Laravel 5.8+ Eloquent models that allows
Simply add the package to your `composer.json` file and run `composer update`.

```
"nanigans/single-table-inheritance": "~1.0"
"carsdotcom/single-table-inheritance": "^1.0.1"
```

Or go to your project directory where the `composer.json` file is located and type:

```sh
composer require "nanigans/single-table-inheritance:~1.0"
composer require "carsdotcom/single-table-inheritance:^1.0.1"
```

For Laravel before 9.x you can still use [jonspalmer/single-table-inheritance:1.0.0](https://github.com/jonspalmer/single-table-inheritance)

# Overview

Getting started with the Single Table Inheritance Trait is simple. Add the constraint and add a few properties to your models. A complete example of a `Vehicle` super class with two subclasses `Truck` and `Car` is given by
Expand Down Expand Up @@ -158,7 +160,7 @@ Unfortunately there is no efficient way to automatically detect BelongsTo foreig

### Throwing Exceptions for Invalid Attributes

BY default the SingleTableInheritanceTrait will handle invalid attributes silently It ignores non-persisted attributes when a model is saved and ignores non-persisted columns when hydrating a model from a builder query. However, you can force exceptions to be thrown when invalid attributes are encountered in either situation by setting the `$throwInvalidAttributeExceptions` property to true.
By default, the SingleTableInheritanceTrait will handle invalid attributes silently It ignores non-persisted attributes when a model is saved and ignores non-persisted columns when hydrating a model from a builder query. However, you can force exceptions to be thrown when invalid attributes are encountered in either situation by setting the `$throwInvalidAttributeExceptions` property to true.

```php
/**
Expand All @@ -175,7 +177,7 @@ protected static $throwInvalidAttributeExceptions = true;

We've chosen a very particular implementation to support single table inheritance. However, others have written code and articles around a general approach that proved influential.

First, Mark Smith has an excellent article (no long live but available in web archive) [Single Table Inheritance in Laravel 4](https://web.archive.org/web/20171224233536/http://www.colorfultyping.com/single-table-inheritance-in-laravel-4/) amongst other things is introduces the importance of queries returning objects of the correct type. Second, Jacopo Beschi wrote and extension of Eloquent's `Model`, [Laravel-Single-Table-Inheritance](https://github.com/intrip/laravel-single-table-inheritance)`, that introduces the importance of being able to define which attributes each model persists.
First, Mark Smith has an excellent article (no long live but available in web archive) [Single Table Inheritance in Laravel 4](https://web.archive.org/web/20171224233536/http://www.colorfultyping.com/single-table-inheritance-in-laravel-4/) amongst other things it introduces the importance of queries returning objects of the correct type. Second, Jacopo Beschi wrote and extension of Eloquent's `Model`, [Laravel-Single-Table-Inheritance](https://github.com/intrip/laravel-single-table-inheritance)`, that introduces the importance of being able to define which attributes each model persists.

The use of Traits was heavy influence by the Eloquent's `SoftDeletingTrait` and the excellent [Validating Trait](https://github.com/dwightwatson/validating).

Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
}
],
"require": {
"php": ">= 7.2",
"php": ">= 8.0",
"illuminate/database": ">= 5.2",
"illuminate/support": ">= 5.2"
},
"require-dev": {
"mockery/mockery": "~1.0",
"phpunit/phpunit": "~8.0|~9.0",
"orchestra/testbench": "3.8.*|4.*|5.*|6.*",
"orchestra/database": "3.8.*|4.*|5.*|6.*"
"phpunit/phpunit": "~8.0|~9.0|^10.5",
"orchestra/testbench": "7.*|8.*|9.*"
},
"autoload": {
"psr-4": {
Expand Down
21 changes: 6 additions & 15 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
13 changes: 13 additions & 0 deletions tests/Fixtures/Vehicle.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ public function setDates(array $dates) {
$this->dates = $dates;
}

/**
* This is a shim, the unit tests modify what fields are dates on the fly, but
* Laravel 10+ doesn't support $this->dates property natively
*/
public function getDates()
{
$dates = parent::getDates();
if (is_array($this->dates ?? null) && array_diff($this->dates, $dates)) {
$dates = array_unique(array_merge($dates, $this->dates));
}
return $dates;
}

public function setPrimaryKey($primaryKey) {
$this->primaryKey = $primaryKey;
}
Expand Down
1 change: 1 addition & 0 deletions tests/SingleTableInheritanceTraitModelMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function testGetPersistedIncludesDates() {
$attributes = $vehicle->getPersistedAttributes();
sort($attributes);
$this->assertEquals(['color', 'created_at', 'id', 'owner_id', 'purchase_date', 'type', 'updated_at'], $attributes);
$this->assertEmpty(array_diff($vehicle->getDates(), $vehicle->getPersistedAttributes()));
}

public function testGetPersistedIncludesPrimaryKey() {
Expand Down
14 changes: 0 additions & 14 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,4 @@ protected function getEnvironmentSetUp($app) {
'prefix' => '',
));
}

/**
* Get package providers.
*
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
protected function getPackageProviders($app)
{
return [
\Orchestra\Database\ConsoleServiceProvider::class,
];
}
}

0 comments on commit feb4906

Please sign in to comment.