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

tests: Refactored Tests #58

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 3 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
indent_size = 2
22 changes: 7 additions & 15 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.github export-ignore
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/art export-ignore
/docs export-ignore
/tests export-ignore
# /art export-ignore
# /docs export-ignore
/.editorconfig export-ignore
/.php_cs.dist.php export-ignore
/psalm.xml export-ignore
/psalm.xml.dist export-ignore
/testbench.yaml export-ignore
/phpstan.neon export-ignore
/phpunit.xml export-ignore
# /pint.json export-ignore
/tests export-ignore
/UPGRADING.md export-ignore
/phpstan.neon.dist export-ignore
/phpstan-baseline.neon export-ignore
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
.idea
.php_cs
.php_cs.cache
.phpunit.cache
.phpunit.result.cache
build
composer.lock
coverage
docs
phpunit.xml
phpstan.neon
testbench.yaml
vendor
node_modules
.php-cs-fixer.cache
18 changes: 10 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@
"spatie/laravel-package-tools": "^1.14.0"
},
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.0|^8.0",
"orchestra/testbench": "^7.0|^8.0|^9.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-type-coverage": "^2.0"
"pestphp/pest-plugin-arch": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
"pestphp/pest-plugin-type-coverage": "^2.0",
"laravel/pint": "^1.0"
},
"autoload": {
"psr-4": {
"Cjmellor\\Approval\\": "src"
"Approval\\Approval\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Cjmellor\\Approval\\Tests\\": "tests"
"Approval\\Approval\\Tests\\": "tests/"
}
},
"scripts": {
Expand All @@ -43,18 +45,18 @@
"test-coverage": "vendor/bin/pest --coverage"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Cjmellor\\Approval\\ApprovalServiceProvider"
"Approval\\Approval\\ApprovalServiceProvider"
],
"aliases": {
"Approval": "Cjmellor\\Approval\\Facades\\Approval"
"Approval": "Approval\\Approval\\Facades\\Approval"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('approvals', function (Blueprint $table) {
$table->unsignedBigInteger('foreign_key')->nullable()->after('original_data');
});
}

public function down(): void
{
Schema::table('approvals', function (Blueprint $table) {
$table->dropColumn('foreign_key');
});
}
};
16 changes: 8 additions & 8 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
colors="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
<directory>./src</directory>
<directory>./tests</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
3 changes: 2 additions & 1 deletion src/ApprovalServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Cjmellor\Approval;
namespace Approval\Approval;

use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
Expand All @@ -16,6 +16,7 @@ public function configurePackage(Package $package): void
'2022_02_12_195950_create_approvals_table',
'2023_10_09_204810_add_rolled_back_at_column_to_approvals_table',
'2023_11_17_002135_add_audited_by_column_to_approvals_table',
'2024_03_16_173148_add_foreign_id_column_to_approvals_table',
]);
}
}
46 changes: 28 additions & 18 deletions src/Concerns/MustBeApproved.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Cjmellor\Approval\Concerns;
namespace Approval\Approval\Concerns;

use Cjmellor\Approval\Enums\ApprovalStatus;
use Cjmellor\Approval\Models\Approval;
use Approval\Approval\Enums\ApprovalStatus;
use Approval\Approval\Models\Approval;
use Illuminate\Database\Eloquent\Relations\MorphMany;

trait MustBeApproved
Expand All @@ -12,16 +12,21 @@ trait MustBeApproved

public static function bootMustBeApproved(): void
{
static::creating(callback: fn ($model) => static::insertApprovalRequest($model));
static::updating(callback: fn ($model) => static::insertApprovalRequest($model));
static::creating(callback: fn ($model): ?bool => static::insertApprovalRequest($model));
static::updating(callback: fn ($model): ?bool => static::insertApprovalRequest($model));
}

/**
* Create an Approval request before committing to the database.
*/
protected static function insertApprovalRequest($model)
protected static function insertApprovalRequest($model): ?bool
{
$filteredDirty = $model->getDirtyAttributes();
$foreignKey = $model->getApprovalForeignKeyName();
$foreignKeyValue = $filteredDirty[$foreignKey] ?? null;

// Remove the foreign key from the dirty attributes
unset($filteredDirty[$foreignKey]);

foreach ($filteredDirty as $key => $value) {
if (isset($model->casts[$key]) && $model->casts[$key] === 'json') {
Expand All @@ -30,40 +35,37 @@ protected static function insertApprovalRequest($model)
}

if ($model->isApprovalBypassed() || empty($filteredDirty)) {
return;
return null;
}

$noNeedToProceed = true;
$approvalAttributes = $model->getApprovalAttributes();

if (! empty($approvalAttributes)) {
$noNeedToProceed = collect($model->getDirty())
$noApprovalNeeded = collect($model->getDirty())
->except($approvalAttributes)
->isEmpty();

if (! $noNeedToProceed) {
$noApprovalNeeded = collect($model->getDirty())
->except($approvalAttributes)
->toArray();
->toArray();

if (! empty($noApprovalNeeded)) {
$model->discardChanges();

$model->forceFill($noApprovalNeeded);
}
}

if (self::approvalModelExists($model) && $noNeedToProceed) {
if (self::approvalModelExists($model) && empty($noApprovalNeeded)) {
return false;
}

$model->approvals()->create([
'new_data' => $filteredDirty,
'original_data' => $model->getOriginalMatchingChanges(),
'foreign_key' => $foreignKeyValue,
]);

if ($noNeedToProceed) {
if (empty($noApprovalNeeded)) {
return false;
}

return true;
}

/**
Expand All @@ -85,6 +87,14 @@ public function getApprovalAttributes(): array
return $this->approvalAttributes ?? [];
}

/**
* Get the name of the foreign key for the model.
*/
public function getApprovalForeignKeyName(): string
{
return 'user_id';
}

/**
* Check is the approval can be bypassed.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Enums/ApprovalStatus.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Cjmellor\Approval\Enums;
namespace Approval\Approval\Enums;

enum ApprovalStatus: string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Events/ModelApproved.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Cjmellor\Approval\Events;
namespace Approval\Approval\Events;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
Expand Down
2 changes: 1 addition & 1 deletion src/Events/ModelRejected.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Cjmellor\Approval\Events;
namespace Approval\Approval\Events;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
Expand Down
2 changes: 1 addition & 1 deletion src/Events/ModelRolledBackEvent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Cjmellor\Approval\Events;
namespace Approval\Approval\Events;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
Expand Down
2 changes: 1 addition & 1 deletion src/Events/ModelSetPending.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Cjmellor\Approval\Events;
namespace Approval\Approval\Events;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
Expand Down
4 changes: 2 additions & 2 deletions src/Facades/Approval.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Cjmellor\Approval\Facades;
namespace Approval\Approval\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @see \Cjmellor\Approval\Approval
* @see \Approval\Approval\Approval
*/
class Approval extends Facade
{
Expand Down
8 changes: 4 additions & 4 deletions src/Models/Approval.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Cjmellor\Approval\Models;
namespace Approval\Approval\Models;

use Cjmellor\Approval\Enums\ApprovalStatus;
use Cjmellor\Approval\Events\ModelRolledBackEvent;
use Cjmellor\Approval\Scopes\ApprovalStateScope;
use Approval\Approval\Enums\ApprovalStatus;
use Approval\Approval\Events\ModelRolledBackEvent;
use Approval\Approval\Scopes\ApprovalStateScope;
use Closure;
use Exception;
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
Expand Down
10 changes: 5 additions & 5 deletions src/Scopes/ApprovalStateScope.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Cjmellor\Approval\Scopes;
namespace Approval\Approval\Scopes;

use Cjmellor\Approval\Enums\ApprovalStatus;
use Cjmellor\Approval\Events\ModelApproved;
use Cjmellor\Approval\Events\ModelRejected;
use Cjmellor\Approval\Events\ModelSetPending;
use Approval\Approval\Enums\ApprovalStatus;
use Approval\Approval\Events\ModelApproved;
use Approval\Approval\Events\ModelRejected;
use Approval\Approval\Events\ModelSetPending;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
Expand Down
7 changes: 7 additions & 0 deletions tests/Feature/ArchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

test('Arch tests')
->expect(['dd', 'ddd', 'dump'])
->each
->not
->toBeUsed();
Loading
Loading