Skip to content

Commit

Permalink
Add test coverage]
Browse files Browse the repository at this point in the history
  • Loading branch information
Kurt Friars committed Oct 3, 2024
1 parent 1b77fe0 commit c035e4c
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Facades/SnapshotSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Plank\Snapshots\Facades;

use Illuminate\Database\Schema\Builder;
use Illuminate\Database\Schema\ForeignKeyDefinition;
use Illuminate\Database\Schema\IndexDefinition;
use Illuminate\Support\Facades\Facade;
use Plank\Snapshots\Contracts\VersionedSchema;

Expand All @@ -17,6 +19,8 @@
* @method static bool hasTable(string $table)
* @method static bool hasColumn(string $table, string $column)
* @method static bool hasColumns(string $table, array $columns)
* @method static IndexDefinition[] getIndexes(string $table)
* @method static ForeignKeyDefinition[] getForeignKeys(string $table)
* @method static void whenTableHasColumn(string $table, string $column, \Closure $callback)
* @method static void whenTableDoesntHaveColumn(string $table, string $column, \Closure $callback)
* @method static string getColumnType(string $table, string $column)
Expand Down
26 changes: 26 additions & 0 deletions tests/Database/Migrations/schema/fks/create_documents_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Plank\Snapshots\Migrator\SnapshotBlueprint;
use Plank\Snapshots\Migrator\SnapshotMigration;

return new class extends SnapshotMigration
{
public function up()
{
$this->schema->create('documents', function (SnapshotBlueprint $table) {
$table->id();
$table->string('title');
$table->string('text');
$table->timestamp('released_at')->nullable();
$table->timestamps();

$table->index('title', 'idx_title');
$table->index('released_at');
});
}

public function down()
{
$this->schema->drop('documents');
}
};
22 changes: 22 additions & 0 deletions tests/Database/Migrations/schema/fks/y_add_folders_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Plank\Snapshots\Migrator\SnapshotBlueprint;
use Plank\Snapshots\Migrator\SnapshotMigration;

return new class extends SnapshotMigration
{
public function up()
{
$this->schema->create('folders', function (SnapshotBlueprint $table) {
$table->id();
$table->string('name');
$table->string('disk');
$table->timestamps();
});
}

public function down()
{
$this->schema->drop('folders');
}
};
26 changes: 26 additions & 0 deletions tests/Database/Migrations/schema/fks/z_alter_documents_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Plank\Snapshots\Migrator\SnapshotBlueprint;
use Plank\Snapshots\Migrator\SnapshotMigration;
use Plank\Snapshots\Tests\Models\Folder;

return new class extends SnapshotMigration
{
public function up()
{
$this->schema->table('documents', function (SnapshotBlueprint $table) {
$table->string('folder_id')->after('id');

$table->foreign('folder_id')
->references('id')
->onSnapshot('folders');
});
}

public function down()
{
$this->schema->table('documents', function (SnapshotBlueprint $table) {
$table->dropForeignIdFor(Folder::class);
});
}
};
29 changes: 29 additions & 0 deletions tests/Models/Folder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Plank\Snapshots\Tests\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Plank\Snapshots\Concerns\AsVersionedContent;
use Plank\Snapshots\Concerns\HasHistory;
use Plank\Snapshots\Contracts\Trackable;
use Plank\Snapshots\Contracts\Versioned;

class Folder extends Model implements Trackable, Versioned
{
use AsVersionedContent;
use HasFactory;
use HasHistory;

protected $guarded = [];

/**
* The attributes that should be visible in serialization.
*
* @var array<string>
*/
protected $visible = [
'name',
'disk',
];
}
32 changes: 32 additions & 0 deletions tests/Suites/Feature/Migrations/TableRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Events\MigrationsEnded;
use Illuminate\Support\Facades\Event;
use Plank\Snapshots\Contracts\ManagesCreatedTables;

use function Pest\Laravel\artisan;

describe('The Table Repository Gathers TableCreated events correctly', function () {
beforeEach(function () {
artisan('migrate', [
'--path' => migrationPath('query'),
'--realpath' => true,
])->run();
});

it('gathers tables correctly', function () {
Event::forget(MigrationsEnded::class);

Event::listen(MigrationsEnded::class, function () {
expect(app(ManagesCreatedTables::class)->all())
->toHaveKeys([
'posts',
'post_post',
'seos',
'taggables',
]);
});

versions()->setActive(createFirstVersion('query'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,78 @@
expect(SnapshotSchema::getColumnType('documents', 'text'))->toBe('text');
});

it('reads the indexes of versioned tables correctly', function () {
versions()->setActive(createFirstVersion('schema/create'));

$indexes = SnapshotSchema::getIndexes('documents');

expect($indexes)->toContain([
"name" => "primary",
"columns" => [
0 => "id"
],
"type" => null,
"unique" => true,
"primary" => true,
]);

expect($indexes)->toContain([
"name" => "v1_0_0_documents_released_at_index",
"columns" => [
0 => "released_at",
],
"type" => null,
"unique" => false,
"primary" => false,
]);

expect($indexes)->toContain([
"name" => "v1_0_0_idx_title",
"columns" => [
0 => "title"
],
"type" => null,
"unique" => false,
"primary" => false,
]);
});

it('reads the foreign keys of versioned tables correctly', function () {
artisan('migrate', [
'--path' => migrationPath('schema/fks'),
'--realpath' => true,
])->run();

assertDatabaseHas('migrations', [
'migration' => 'y_add_folders_table',
'batch' => 4,
]);

assertDatabaseHas('migrations', [
'migration' => 'z_alter_documents_table',
'batch' => 4,
]);

versions()->setActive(createFirstVersion('schema/fks'));


$fks = SnapshotSchema::getForeignKeys('documents');

expect($fks)->toContain([
"name" => null,
"columns" => [
0 => "folder_id",
],
"foreign_schema" => null,
"foreign_table" => "v1_0_0_folders",
"foreign_columns" => [
0 => "id",
],
"on_update" => "no action",
"on_delete" => "no action",
]);
});

it('forwards non-table schema builder methods to the frameworks schema builder', function () {
versions()->setActive(createFirstVersion('schema/create'));

Expand Down
68 changes: 68 additions & 0 deletions tests/Suites/Feature/Schema/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\Schema\MySqlBuilder;
use Illuminate\Database\Schema\PostgresBuilder;
use Illuminate\Database\Schema\SQLiteBuilder;
use Illuminate\Database\Schema\SqlServerBuilder;
use Plank\Snapshots\Contracts\ManagesCreatedTables;
use Plank\Snapshots\Contracts\ManagesVersions;
use Plank\Snapshots\Factory\SchemaBuilderFactory;
use Plank\Snapshots\Schema\MySqlSnapshotBuilder;
use Plank\Snapshots\Schema\PostgresSnapshotBuilder;
use Plank\Snapshots\Schema\SQLiteSnapshotBuilder;
use Plank\Snapshots\Schema\SqlServerSnapshotBuilder;

describe('The Schema Builder is resolved in the container based on the db connection', function () {
it('resolves the MySql Snapshot Builder correctly', function () {
$connection = (new ConnectionFactory(app()))->make(config()->get('database.connections.mysql'));

$instance = SchemaBuilderFactory::make(
$connection,
app(ManagesVersions::class),
app(ManagesCreatedTables::class)
);

expect($instance)->toBeInstanceOf(MySqlBuilder::class);
expect($instance)->toBeInstanceOf(MySqlSnapshotBuilder::class);
});

it('resolves the Postgres Snapshot Builder correctly', function () {
$connection = (new ConnectionFactory(app()))->make(config()->get('database.connections.pgsql'));

$instance = SchemaBuilderFactory::make(
$connection,
app(ManagesVersions::class),
app(ManagesCreatedTables::class)
);

expect($instance)->toBeInstanceOf(PostgresBuilder::class);
expect($instance)->toBeInstanceOf(PostgresSnapshotBuilder::class);
});

it('resolves the SQLite Snapshot Builder correctly', function () {
$connection = (new ConnectionFactory(app()))->make(config()->get('database.connections.sqlite'));

$instance = SchemaBuilderFactory::make(
$connection,
app(ManagesVersions::class),
app(ManagesCreatedTables::class)
);

expect($instance)->toBeInstanceOf(SQLiteBuilder::class);
expect($instance)->toBeInstanceOf(SQLiteSnapshotBuilder::class);
});

it('resolves the SqlServer Snapshot Builder correctly', function () {
$connection = (new ConnectionFactory(app()))->make(config()->get('database.connections.sqlsrv'));

$instance = SchemaBuilderFactory::make(
$connection,
app(ManagesVersions::class),
app(ManagesCreatedTables::class)
);

expect($instance)->toBeInstanceOf(SqlServerBuilder::class);
expect($instance)->toBeInstanceOf(SqlServerSnapshotBuilder::class);
});
});

0 comments on commit c035e4c

Please sign in to comment.