Skip to content

Commit

Permalink
Make tests backwards compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Kurt Friars committed Oct 7, 2024
1 parent 9e542c7 commit 2e12f2a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ return new class extends SnapshotMigration
}
```

You will notice that in a `SnapshotMigration` you have `$this->schema` available which is the `SnapshotSchemaBuilder` instance. It is a wrapper around the framework's `\Illuminate\Database\Schema\Builder` class. You can also use the `SnapshotSchema` facade, however you should only use that inside a `SnapshotMigration`.
You will notice that in a `SnapshotMigration` you have `$this->schema` available which is the `SnapshotBuilder` instance. It is a wrapper around the framework's `\Illuminate\Database\Schema\Builder` class. You can also use the `SnapshotSchema` facade, however you should only use that inside a `SnapshotMigration`.

The `SnapshotMigrations` allow you to declare migrations in the way you are used to, but under the hood it will handle all of the versioning.

Expand Down
4 changes: 2 additions & 2 deletions src/Factory/SchemaBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Plank\Snapshots\Contracts\VersionedSchema;
use Plank\Snapshots\Schema\MySqlSnapshotBuilder;
use Plank\Snapshots\Schema\PostgresSnapshotBuilder;
use Plank\Snapshots\Schema\SnapshotSchemaBuilder;
use Plank\Snapshots\Schema\SnapshotBuilder;
use Plank\Snapshots\Schema\SQLiteSnapshotBuilder;
use Plank\Snapshots\Schema\SqlServerSnapshotBuilder;

Expand All @@ -29,7 +29,7 @@ public static function make(
PostgresBuilder::class => new PostgresSnapshotBuilder($connection, $versions, $tables),
SQLiteBuilder::class => new SQLiteSnapshotBuilder($connection, $versions, $tables),
SqlServerBuilder::class => new SqlServerSnapshotBuilder($connection, $versions, $tables),
default => new SnapshotSchemaBuilder($connection, $versions, $tables),
default => new SnapshotBuilder($connection, $versions, $tables),
};
}
}
38 changes: 38 additions & 0 deletions src/Schema/SnapshotBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Plank\Snapshots\Schema;

use Illuminate\Database\Schema\Builder;
use Nette\NotImplementedException;
use Plank\Snapshots\Concerns\HasVersionedSchema;
use Plank\Snapshots\Contracts\VersionedSchema;

/**
* @mixin Builder
*/
class SnapshotBuilder extends Builder implements VersionedSchema
{
use HasVersionedSchema;

/**
* Get the indexes for a given table.
*
* @param string $table
* @return array
*/
public function getIndexes($table)
{
throw new NotImplementedException('The `getIndexes` method requires a grammar specific Schema Builder.');
}

/**
* Get the foreign keys for a given table.
*
* @param string $table
* @return array
*/
public function getForeignKeys($table)
{
throw new NotImplementedException('The `getForeignKeys` method requires a grammar specific Schema Builder.');
}
}
15 changes: 0 additions & 15 deletions src/Schema/SnapshotSchemaBuilder.php

This file was deleted.

14 changes: 14 additions & 0 deletions tests/Suites/Feature/Schema/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Nette\NotImplementedException;
use Plank\Snapshots\Exceptions\MigrationFailedException;
use Plank\Snapshots\Facades\SnapshotSchema;
use Plank\Snapshots\Schema\SnapshotBuilder;
use Plank\Snapshots\Tests\Models\Document;

use function Pest\Laravel\artisan;
Expand Down Expand Up @@ -292,6 +294,12 @@
});

it('reads the indexes of versioned tables correctly', function () {
if (SnapshotSchema::getFacadeRoot() instanceof SnapshotBuilder) {
expect(fn () => SnapshotSchema::getIndexes('error'))->toThrow(NotImplementedException::class);

return;
}

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

$indexes = SnapshotSchema::getIndexes('documents');
Expand Down Expand Up @@ -328,6 +336,12 @@
});

it('reads the foreign keys of versioned tables correctly', function () {
if (SnapshotSchema::getFacadeRoot() instanceof SnapshotBuilder) {
expect(fn () => SnapshotSchema::getForeignKeys('error'))->toThrow(NotImplementedException::class);

return;
}

artisan('migrate', [
'--path' => migrationPath('schema/fks'),
'--realpath' => true,
Expand Down
11 changes: 8 additions & 3 deletions tests/Suites/Feature/Schema/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use Illuminate\Database\Schema\PostgresBuilder;
use Illuminate\Database\Schema\SQLiteBuilder;
use Illuminate\Database\Schema\SqlServerBuilder;
use Illuminate\Support\Facades\DB;
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\SnapshotBuilder;
use Plank\Snapshots\Schema\SQLiteSnapshotBuilder;
use Plank\Snapshots\Schema\SqlServerSnapshotBuilder;

Expand Down Expand Up @@ -41,16 +43,19 @@
});

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

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

expect($instance)->toBeInstanceOf(SQLiteBuilder::class);
expect($instance)->toBeInstanceOf(SQLiteSnapshotBuilder::class);
if ($connection->getSchemaBuilder() instanceof SQLiteBuilder) {
expect($instance)->toBeInstanceOf(SQLiteSnapshotBuilder::class);
} else {
expect($instance)->toBeInstanceOf(SnapshotBuilder::class);
}
});

it('resolves the SqlServer Snapshot Builder correctly', function () {
Expand Down

0 comments on commit 2e12f2a

Please sign in to comment.