-
Notifications
You must be signed in to change notification settings - Fork 3
Add migration generator tests #69
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c1f3a1e
test: add migration generation test.
adf19bf
Merge branch 'master' into 49-add-migration-generator-tests
250ab64
Merge branch 'master' into 49-add-migration-generator-tests
t0xas 9caa6f7
Merge branch '49-add-factory-tests' into 49-add-migration-generator-t…
t0xas b8bd8e3
chore: add migration generator tests
t0xas 1a07224
chore: add migration generator tests
t0xas 0fd2579
chore: add migration generator tests
t0xas a57ba08
chore: add migration generator tests
t0xas 4abc19a
chore: add migration generator tests
t0xas 3846d71
fix: conflicts
pirs1337 82131a2
Merge remote-tracking branch 'origin/master' into 49-add-migration-ge…
pirs1337 ab7a2dc
Merge remote-tracking branch 'origin/master' into 49-add-migration-ge…
pirs1337 3473212
refactor: test
pirs1337 f4eaead
refactor: remove useless use ReflectionClass
pirs1337 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Support\Exceptions; | ||
|
|
||
| use Exception; | ||
|
|
||
| class FakerMethodNotFoundException extends Exception | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,6 @@ | |
|
|
||
| use Exception; | ||
|
|
||
| class FakerMethodNotFound extends Exception | ||
| class UnknownFieldTypeException extends Exception | ||
| { | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,4 +71,4 @@ public function createTable() | |
| }); | ||
| } | ||
| @endif | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Support\Tests; | ||
|
|
||
| use Illuminate\Support\Carbon; | ||
| use ReflectionClass; | ||
| use RonasIT\Support\Exceptions\UnknownFieldTypeException; | ||
| use RonasIT\Support\Generators\MigrationGenerator; | ||
| use RonasIT\Support\Tests\Support\Migration\MigrationMockTrait; | ||
|
|
||
| class MigrationGeneratorTest extends TestCase | ||
| { | ||
| use MigrationMockTrait; | ||
|
|
||
| public function testSetUnknownFieldType() | ||
| { | ||
| $this->setupConfigurations(); | ||
|
|
||
| $this->expectException(UnknownFieldTypeException::class); | ||
| $this->expectExceptionMessage('Unknown field type unknown-type in MigrationGenerator.'); | ||
|
||
|
|
||
| app(MigrationGenerator::class) | ||
| ->setModel('Post') | ||
| ->setRelations([ | ||
| 'belongsTo' => [], | ||
| 'belongsToMany' => [], | ||
| 'hasOne' => [], | ||
| 'hasMany' => [], | ||
| ]) | ||
| ->setFields([ | ||
| 'integer-required' => ['media_id', 'user_id'], | ||
| 'unknown-type' => ['title'], | ||
| ]) | ||
| ->generate(); | ||
| } | ||
|
|
||
| public function testCreateMigration() | ||
| { | ||
| Carbon::setTestNow('2022-02-02'); | ||
|
|
||
| $this->mockFilesystem(); | ||
| $this->setupConfigurations(); | ||
|
|
||
| app(MigrationGenerator::class) | ||
| ->setModel('Post') | ||
| ->setRelations([ | ||
| 'belongsTo' => [], | ||
| 'belongsToMany' => [], | ||
| 'hasOne' => [], | ||
| 'hasMany' => [], | ||
| ]) | ||
| ->setFields([ | ||
| 'integer-required' => ['media_id', 'user_id'], | ||
| 'string' => ['title', 'body'], | ||
| 'json' => ['meta'], | ||
| 'timestamp' => ['created_at'], | ||
| ]) | ||
| ->generate(); | ||
|
|
||
| $this->assertGeneratedFileEquals('migrations.php', 'database/migrations/2022_02_02_000000_posts_create_table.php'); | ||
| } | ||
|
|
||
| public function testMethodForMYSQLConnection() | ||
|
||
| { | ||
| putenv('DB_CONNECTION=mysql'); | ||
|
|
||
| $generatorClassName = MigrationGenerator::class; | ||
|
|
||
| $generator = app($generatorClassName); | ||
|
|
||
| $reflectionClass = new ReflectionClass($generatorClassName); | ||
| $jsonLineMethod = $reflectionClass->getMethod('getJsonLine'); | ||
| $requiredLineMethod = $reflectionClass->getMethod('getRequiredLine'); | ||
|
|
||
| $jsonLineMethod->setAccessible(true); | ||
| $requiredLineMethod->setAccessible(true); | ||
|
|
||
| $jsonLineResult = $jsonLineMethod->invoke($generator, 'meta'); | ||
| $requiredLineResult = $requiredLineMethod->invoke($generator, 'created_at', 'timestamp-required'); | ||
|
|
||
| $this->assertEquals("\$table->json('meta')->nullable();", $jsonLineResult); | ||
| $this->assertEquals("\$table->timestamp('created_at')->nullable();", $requiredLineResult); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Support\Tests\Support\Migration; | ||
|
|
||
| use org\bovigo\vfs\vfsStream; | ||
| use RonasIT\Support\Tests\Support\GeneratorMockTrait; | ||
|
|
||
| trait MigrationMockTrait | ||
| { | ||
| use GeneratorMockTrait; | ||
|
|
||
| public function setupConfigurations(): void | ||
| { | ||
| config([ | ||
| 'entity-generator.stubs.migration' => 'entity-generator::migration', | ||
| 'entity-generator.paths' => [ | ||
| 'migrations' => 'database/migrations', | ||
| ] | ||
| ]); | ||
| } | ||
|
|
||
| public function mockFilesystem(): void | ||
| { | ||
| $structure = [ | ||
| 'database' => [ | ||
| 'migrations' => [], | ||
| ], | ||
| ]; | ||
|
|
||
| vfsStream::create($structure); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
| use RonasIT\Support\Traits\MigrationTrait; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| use MigrationTrait; | ||
|
|
||
| public function up() | ||
| { | ||
| Schema::create('posts', function (Blueprint $table) { | ||
| $table->increments('id'); | ||
| $table->integer('media_id'); | ||
| $table->integer('user_id'); | ||
| $table->string('title')->nullable(); | ||
| $table->string('body')->nullable(); | ||
| $table->jsonb('meta')->default("{}"); | ||
| $table->timestamp('created_at')->nullable(); | ||
| $table->timestamps(); | ||
| }); | ||
| } | ||
|
|
||
| public function down() | ||
| { | ||
| Schema::dropIfExists('posts'); | ||
| } | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's move message generation into the exception class