|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Dimitri BOUTEILLE (https://github.com/dimitriBouteille) |
| 4 | + * See LICENSE.txt for license details. |
| 5 | + * |
| 6 | + * Author: Dimitri BOUTEILLE <[email protected]> |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Dbout\WpOrm\Tests\WordPress\Orm\Schemas; |
| 10 | + |
| 11 | +use Dbout\WpOrm\Orm\Database; |
| 12 | +use Dbout\WpOrm\Orm\Schemas\WordPressBuilder; |
| 13 | +use Dbout\WpOrm\Tests\WordPress\TestCase; |
| 14 | +use Illuminate\Database\Schema\Blueprint; |
| 15 | + |
| 16 | +class WordPressBuilderTest extends TestCase |
| 17 | +{ |
| 18 | + private Database $database; |
| 19 | + private WordPressBuilder $schema; |
| 20 | + |
| 21 | + /** |
| 22 | + * @return void |
| 23 | + */ |
| 24 | + public static function setUpBeforeClass(): void |
| 25 | + { |
| 26 | + Database::getInstance()->getSchemaBuilder()->create('project', function (Blueprint $table) { |
| 27 | + $table->id(); |
| 28 | + $table->string('name'); |
| 29 | + $table->integer('author'); |
| 30 | + $table->string('address'); |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @return void |
| 36 | + */ |
| 37 | + public function setUp(): void |
| 38 | + { |
| 39 | + $this->database = Database::getInstance(); |
| 40 | + |
| 41 | + /** @var WordPressBuilder $schema */ |
| 42 | + $schema = $this->database->getSchemaBuilder(); |
| 43 | + $this->schema = $schema; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @return void |
| 48 | + * @covers WordPressBuilder::create |
| 49 | + * @covers WordPressBuilder::hasTable |
| 50 | + * @covers WordPressBuilder::getColumns |
| 51 | + * @covers WordPressBuilder::hasColumn |
| 52 | + */ |
| 53 | + public function testCreate(): void |
| 54 | + { |
| 55 | + $this->schema->create('architect', function (Blueprint $table) { |
| 56 | + $table->id(); |
| 57 | + $table->string('name'); |
| 58 | + $table->string('slug'); |
| 59 | + $table->json('data')->nullable(); |
| 60 | + }); |
| 61 | + |
| 62 | + $this->assertTrue($this->schema->hasTable('architect')); |
| 63 | + $table = $this->database->getTablePrefix() . 'architect'; |
| 64 | + $columns = $this->schema->getColumns($table); |
| 65 | + $this->assertCount(4, $columns); |
| 66 | + $this->assertTrue($this->schema->hasColumn($table, 'id')); |
| 67 | + $this->assertTrue($this->schema->hasColumn($table, 'name')); |
| 68 | + $this->assertTrue($this->schema->hasColumn($table, 'slug')); |
| 69 | + $this->assertTrue($this->schema->hasColumn($table, 'data')); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @return void |
| 74 | + * @covers WordPressBuilder::table |
| 75 | + * @covers WordPressBuilder::hasTable |
| 76 | + */ |
| 77 | + public function testUpdate(): void |
| 78 | + { |
| 79 | + $this->schema->table('project', function (Blueprint $table) { |
| 80 | + $table->string('country'); |
| 81 | + $table->boolean('finish'); |
| 82 | + }); |
| 83 | + |
| 84 | + $table = $this->database->getTablePrefix() . 'project'; |
| 85 | + $this->assertTrue($this->schema->hasColumn($table, 'country')); |
| 86 | + $this->assertTrue($this->schema->hasColumn($table, 'finish')); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return void |
| 91 | + * @covers WordPressBuilder::drop |
| 92 | + */ |
| 93 | + public function testDrop(): void |
| 94 | + { |
| 95 | + $this->schema->create('company', function (Blueprint $table) { |
| 96 | + $table->id(); |
| 97 | + $table->string('name'); |
| 98 | + $table->string('slug'); |
| 99 | + }); |
| 100 | + |
| 101 | + $this->assertTrue($this->schema->hasTable('company')); |
| 102 | + $this->schema->drop('company'); |
| 103 | + $this->assertFalse($this->schema->hasTable('company')); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @return void |
| 108 | + * @covers WordPressBuilder::dropColumns |
| 109 | + */ |
| 110 | + public function testDropColumn(): void |
| 111 | + { |
| 112 | + $this->schema->create('address', function (Blueprint $table) { |
| 113 | + $table->id(); |
| 114 | + $table->string('firstname'); |
| 115 | + $table->string('lastname'); |
| 116 | + $table->string('street_1'); |
| 117 | + $table->string('street_2'); |
| 118 | + $table->string('street_3'); |
| 119 | + }); |
| 120 | + |
| 121 | + $table = $this->database->getTablePrefix() . 'address'; |
| 122 | + $columns = $this->schema->getColumns($table); |
| 123 | + $this->assertCount(6, $columns); |
| 124 | + |
| 125 | + $this->schema->dropColumns('address', ['street_3']); |
| 126 | + $columns = $this->schema->getColumns($table); |
| 127 | + $this->assertCount(5, $columns); |
| 128 | + $this->assertFalse($this->schema->hasColumn($table, 'street_3')); |
| 129 | + |
| 130 | + } |
| 131 | +} |
0 commit comments