Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Enum tests #114

Merged
merged 5 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [8.0]
laravel: [9.*]
php: [8.0, 8.1, 8.2]
laravel: [9.*, 10.*]
dependency-version: [prefer-stable]
include:
- laravel: 9.*
testbench: 7.*
- laravel: 10.*
testbench: 8.*
exclude:
- php: '8.0'
laravel: 10.*

name: PHP ${{ matrix.php }} - LARAVEL ${{ matrix.laravel }} - ${{ matrix.dependency-version }}

Expand All @@ -41,4 +46,4 @@ jobs:
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "symfony/console:>=4.3.4" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
- name: Execute Tests
run: ./vendor/bin/phpunit --testdox
run: ./vendor/bin/phpunit --testdox
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor/
.idea
composer.lock
.phpunit.result.cache
.phpunit.result.cache
.phpunit.cache
31 changes: 12 additions & 19 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
<?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="Unit Tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<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.2/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage/>
<testsuites>
<testsuite name="Unit Tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
10 changes: 10 additions & 0 deletions tests/Enums/ToolNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Parental\Tests\Enums;

enum ToolNames: string
{
case ClawHammer = 'claw_hammer';
case Mallet = 'mallet';
case SledgeHammer = 'sledge_hammer';
}
26 changes: 26 additions & 0 deletions tests/Features/TypeColumnCanBeAliasedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

namespace Parental\Tests\Features;

use Parental\Tests\Enums\ToolNames;
use Parental\Tests\Models\Car;
use Parental\Tests\Models\ChildFromAbstractParent;
use Parental\Tests\Models\ClawHammer;
use Parental\Tests\Models\Mallet;
use Parental\Tests\Models\Plane;
use Parental\Tests\Models\SledgeHammer;
use Parental\Tests\Models\Tool;
use Parental\Tests\Models\Vehicle;
use Parental\Tests\TestCase;

Expand Down Expand Up @@ -39,4 +44,25 @@ function type_column_values_can_accept_type_aliases_from_abstract_parent()

$this->assertInstanceOf(ChildFromAbstractParent::class, $child[0]);
}

/** @test */
function enums_can_be_used_as_type_alias()
{
if (phpversion() < 8.1) {
$this->markTestSkipped('Enums are not supported in this version of PHP');
}

ClawHammer::create();
Mallet::create();
SledgeHammer::create();

$tools = Tool::all();

$this->assertInstanceOf(ClawHammer::class, $tools[0]);
$this->assertEquals(ToolNames::ClawHammer->value, $tools[0]->type);
$this->assertInstanceOf(Mallet::class, $tools[1]);
$this->assertEquals(ToolNames::Mallet->value, $tools[1]->type);
$this->assertInstanceOf(SledgeHammer::class, $tools[2]);
$this->assertEquals(ToolNames::SledgeHammer->value, $tools[2]->type);
}
}
12 changes: 12 additions & 0 deletions tests/Models/ClawHammer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Parental\Tests\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Parental\HasParent;

class ClawHammer extends Tool
{
use HasParent;
use HasFactory;
}
12 changes: 12 additions & 0 deletions tests/Models/Mallet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Parental\Tests\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Parental\HasParent;

class Mallet extends Tool
{
use HasParent;
use HasFactory;
}
12 changes: 12 additions & 0 deletions tests/Models/SledgeHammer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Parental\Tests\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Parental\HasParent;

class SledgeHammer extends Tool
{
use HasParent;
use HasFactory;
}
23 changes: 23 additions & 0 deletions tests/Models/Tool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Parental\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Parental\HasChildren;
use Parental\Tests\Enums\ToolNames;

class Tool extends Model
{
use HasChildren;

protected $fillable = ['type'];

protected function childTypes(): array
{
return [
ToolNames::ClawHammer->value => ClawHammer::class,
ToolNames::Mallet->value => Mallet::class,
ToolNames::SledgeHammer->value => SledgeHammer::class,
];
}
}
6 changes: 6 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,11 @@ public function runMigrations()
$table->string('type')->nullable();
$table->timestamps();
});

Schema::create('tools', function ($table) {
$table->increments('id');
$table->string('type')->nullable();
$table->timestamps();
});
}
}
File renamed without changes.