Skip to content

Commit

Permalink
Tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Mar 15, 2024
1 parent 88a8e97 commit ecc77ed
Show file tree
Hide file tree
Showing 21 changed files with 455 additions and 52 deletions.
131 changes: 108 additions & 23 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,115 @@ name: Tests
on: [ push, pull_request ]

jobs:
build:
runs-on: ubuntu-latest

sqlite:
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
php: [ "8.2", "8.3" ]
laravel: [ "10.0", "11.0" ]

name: PHP ${{ matrix.php }}, Laravel ${{ matrix.laravel }}, SQLite 3

strategy:
fail-fast: true
matrix:
php: [ "8.2", "8.3" ]
laravel: [ "10.0", "11.0" ]
steps:
- name: Checkout code
uses: actions/checkout@v4

name: PHP ${{ matrix.php }}, Laravel ${{ matrix.laravel }}
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd, redis, pdo_mysql, pdo_pgsql
coverage: xdebug

steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install dependencies
run: composer require laravel/framework:^${{ matrix.laravel }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, mbstring, zip, pcntl, pdo, pdo_sqlite, iconv
coverage: xdebug

- name: Install dependencies
run: composer require laravel/framework:^${{ matrix.laravel }}

- name: Execute tests
run: sudo vendor/bin/phpunit
- name: Execute tests
run: sudo vendor/bin/phpunit

# mysql:
# runs-on: ubuntu-latest
#
# strategy:
# fail-fast: true
# matrix:
# php: [ "8.2", "8.3" ]
# laravel: [ "10.0", "11.0" ]
# mysql: [ "5.7", "8" ]
#
# name: PHP ${{ matrix.php }}, Laravel ${{ matrix.laravel }}, PostgreSQL ${{ matrix.psql }}
#
# services:
# mysql:
# image: mysql:${{ matrix.mysql }}
# env:
# MYSQL_ROOT_PASSWORD: root
# MYSQL_DATABASE: default
# ports:
# - 3306:3306
# options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
#
# steps:
# - name: Checkout code
# uses: actions/checkout@v4
#
# - name: Setup PHP
# uses: shivammathur/setup-php@v2
# with:
# php-version: ${{ matrix.php }}
# extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd, redis, pdo_mysql, pdo_pgsql
# coverage: xdebug
#
# - name: Install dependencies
# run: composer require laravel/framework:^${{ matrix.laravel }}
#
# - name: Execute tests
# run: sudo vendor/bin/phpunit
# env:
# DB_CONNECTION: mysql
# MYSQL_HOST: 127.0.0.1
#
# postgres:
# runs-on: ubuntu-latest
#
# strategy:
# fail-fast: true
# matrix:
# php: [ "8.2", "8.3" ]
# laravel: [ "10.0", "11.0" ]
# psql: [ "12", "13", "14", "15", "16" ]
#
# name: PHP ${{ matrix.php }}, Laravel ${{ matrix.laravel }}, PostgreSQL ${{ matrix.psql }}
#
# services:
# postgres:
# image: postgres:${{ matrix.psql }}-alpine
# ports:
# - 5432:5432
# env:
# POSTGRES_DB: default
# POSTGRES_USER: root
# POSTGRES_PASSWORD: root
#
# steps:
# - name: Checkout code
# uses: actions/checkout@v4
#
# - name: Setup PHP
# uses: shivammathur/setup-php@v2
# with:
# php-version: ${{ matrix.php }}
# extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd, redis, pdo_mysql, pdo_pgsql
# coverage: xdebug
#
# - name: Install dependencies
# run: composer require laravel/framework:^${{ matrix.laravel }}
#
# - name: Execute tests
# run: sudo vendor/bin/phpunit
# env:
# DB_CONNECTION: pgsql
# PGSQL_HOST: 127.0.0.1
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
.idea/
_site/
build/
node_modules/
tmp/
tests/tmp/
vendor/

.cache
.DS_Store
.env
.php_cs.cache
.phpintel
.temp

*.bak
Expand All @@ -18,4 +14,3 @@ vendor/
*.orig

composer.lock
package-lock.json
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
"require-dev": {
"mockery/mockery": "^1.3.1",
"orchestra/testbench": "^8.0 || ^9.0",
"phpunit/phpunit": "^10.0"
"pestphp/pest": "^2.34",
"pestphp/pest-plugin-laravel": "^2.3",
"ext-pdo": "*"
},
"minimum-stability": "stable",
"prefer-stable": true,
Expand All @@ -68,6 +70,7 @@
"dragon-code/codestyler": true,
"ergebnis/composer-normalize": true,
"friendsofphp/php-cs-fixer": true,
"pestphp/pest-plugin": true,
"symfony/thanks": true
},
"preferred-install": "dist",
Expand All @@ -79,5 +82,10 @@
"DragonCode\\LaravelDataDumper\\ServiceProvider"
]
}
},
"scripts": {
"test": [
"vendor/bin/pest --parallel"
]
}
}
8 changes: 5 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
<text outputFile="build/logs/coverage.txt"/>
</report>
</coverage>
<php>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
</php>
<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
Expand All @@ -25,4 +22,9 @@
<directory>./src</directory>
</include>
</source>
<php>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
<env name="DB_USERNAME" value="root"/>
<env name="DB_PASSWORD" value="root"/>
</php>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use DragonCode\LaravelDataDumper\Service\Tables;
use Illuminate\Database\Events\SchemaDumped;

class DataDumpListener
class DumpListener
{
public function __construct(
protected readonly Tables $tables,
Expand Down
15 changes: 3 additions & 12 deletions src/Service/Tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace DragonCode\LaravelDataDumper\Service;

use stdClass;
use Illuminate\Support\Facades\Schema;

class Tables
{
Expand All @@ -20,20 +20,11 @@ protected function toDump(): array

protected function flatten(): array
{
return array_map(function (array | stdClass $item) {
return is_array($item) ? $item['name'] : $item->name;
}, $this->getTables());
return array_column($this->getTables(), 'name');
}

protected function getTables(): array
{
return method_exists($this->schema(), 'getAllTables')
? $this->schema()->getAllTables()
: $this->schema()->getTables();
}

protected function schema()
{
return app('db.schema');
return Schema::getTables();
}
}
4 changes: 2 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace DragonCode\LaravelDataDumper;

use DragonCode\LaravelDataDumper\Concerns\About;
use DragonCode\LaravelDataDumper\Listeners\DataDumpListener;
use DragonCode\LaravelDataDumper\Listeners\DumpListener;
use Illuminate\Database\Events\SchemaDumped;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
Expand All @@ -29,7 +29,7 @@ public function register(): void

protected function bootListener(): void
{
Event::listen(SchemaDumped::class, DataDumpListener::class);
Event::listen(SchemaDumped::class, DumpListener::class);
}

protected function registerConfig(): void
Expand Down
61 changes: 61 additions & 0 deletions tests/Fixtures/Database/Migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Database;

use Illuminate\Database\Migrations\Migration as BaseMigration;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

abstract class Migration extends BaseMigration
{
protected string $tableName;

public function up(): void
{
$this->createTable();
$this->fillTable();
}

protected function createTable(): void
{
Schema::create($this->tableName(), function (Blueprint $table) {
$table->id();

$table->string($this->column());

$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent();
});
}

protected function fillTable(): void
{
circleProcess(fn (int $i) => $this->table()->insert([
$this->column() => $this->value($i),
]));
}

protected function column(): string
{
return columnName($this->tableName);
}

protected function value(int $index): string
{
return valueName($this->tableName, $index);
}

protected function tableName(): string
{
return tableName($this->tableName);
}

protected function table(): Builder
{
return DB::table($this->tableName());
}
}
20 changes: 20 additions & 0 deletions tests/Fixtures/Providers/TestServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Providers;

use Illuminate\Support\ServiceProvider;

class TestServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->bootMigrations();
}

protected function bootMigrations(): void
{
$this->loadMigrationsFrom(__DIR__ . '/../migrations');
}
}
9 changes: 9 additions & 0 deletions tests/Fixtures/migrations/2024_03_15_000001_foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Tests\Fixtures\Database\Migration;

return new class extends Migration {
protected string $tableName = 'foo';
};
9 changes: 9 additions & 0 deletions tests/Fixtures/migrations/2024_03_15_000002_bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Tests\Fixtures\Database\Migration;

return new class extends Migration {
protected string $tableName = 'bar';
};
9 changes: 9 additions & 0 deletions tests/Fixtures/migrations/2024_03_15_000003_baz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Tests\Fixtures\Database\Migration;

return new class extends Migration {
protected string $tableName = 'baz';
};
10 changes: 10 additions & 0 deletions tests/Helpers/circle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

function circleProcess(Closure $callback): void
{
for ($i = 0; $i < 10; $i++) {
$callback($i);
}
}
Loading

0 comments on commit ecc77ed

Please sign in to comment.