|
6 | 6 |
|
7 | 7 | use Illuminate\Support\Facades\DB;
|
8 | 8 | use Illuminate\Support\Facades\Schema;
|
| 9 | +use MongoDB\BSON\Binary; |
| 10 | +use MongoDB\BSON\UTCDateTime; |
9 | 11 | use MongoDB\Laravel\Schema\Blueprint;
|
10 | 12 |
|
| 13 | +use function collect; |
11 | 14 | use function count;
|
12 | 15 |
|
13 | 16 | class SchemaTest extends TestCase
|
@@ -416,6 +419,70 @@ public function testGetTableListing()
|
416 | 419 | $this->assertContains('newcollection_two', $tables);
|
417 | 420 | }
|
418 | 421 |
|
| 422 | + public function testGetColumns() |
| 423 | + { |
| 424 | + $collection = DB::connection('mongodb')->collection('newcollection'); |
| 425 | + $collection->insert(['text' => 'value', 'mixed' => ['key' => 'value']]); |
| 426 | + $collection->insert(['date' => new UTCDateTime(), 'binary' => new Binary('binary'), 'mixed' => true]); |
| 427 | + |
| 428 | + $columns = Schema::getColumns('newcollection'); |
| 429 | + $this->assertIsArray($columns); |
| 430 | + $this->assertCount(5, $columns); |
| 431 | + |
| 432 | + $columns = collect($columns)->keyBy('name'); |
| 433 | + |
| 434 | + $columns->each(function ($column) { |
| 435 | + $this->assertIsString($column['name']); |
| 436 | + $this->assertEquals($column['type'], $column['type_name']); |
| 437 | + $this->assertNull($column['collation']); |
| 438 | + $this->assertIsBool($column['nullable']); |
| 439 | + $this->assertNull($column['default']); |
| 440 | + $this->assertFalse($column['auto_increment']); |
| 441 | + $this->assertIsString($column['comment']); |
| 442 | + }); |
| 443 | + |
| 444 | + $this->assertEquals('objectId', $columns->get('_id')['type']); |
| 445 | + $this->assertEquals('objectId', $columns->get('_id')['generation']['type']); |
| 446 | + $this->assertNull($columns->get('text')['generation']); |
| 447 | + $this->assertEquals('string', $columns->get('text')['type']); |
| 448 | + $this->assertEquals('date', $columns->get('date')['type']); |
| 449 | + $this->assertEquals('binData', $columns->get('binary')['type']); |
| 450 | + $this->assertEquals('bool, object', $columns->get('mixed')['type']); |
| 451 | + $this->assertEquals('2 occurrences', $columns->get('mixed')['comment']); |
| 452 | + |
| 453 | + // Non-existent collection |
| 454 | + $columns = Schema::getColumns('missing'); |
| 455 | + $this->assertSame([], $columns); |
| 456 | + } |
| 457 | + |
| 458 | + public function testGetIndexes() |
| 459 | + { |
| 460 | + Schema::create('newcollection', function (Blueprint $collection) { |
| 461 | + $collection->index('mykey1'); |
| 462 | + $collection->string('mykey2')->unique('unique_index'); |
| 463 | + $collection->string('mykey3')->index(); |
| 464 | + }); |
| 465 | + $indexes = Schema::getIndexes('newcollection'); |
| 466 | + $this->assertIsArray($indexes); |
| 467 | + $this->assertCount(4, $indexes); |
| 468 | + |
| 469 | + $indexes = collect($indexes)->keyBy('name'); |
| 470 | + |
| 471 | + $indexes->each(function ($index) { |
| 472 | + $this->assertIsString($index['name']); |
| 473 | + $this->assertIsString($index['type']); |
| 474 | + $this->assertIsArray($index['columns']); |
| 475 | + $this->assertIsBool($index['unique']); |
| 476 | + $this->assertIsBool($index['primary']); |
| 477 | + }); |
| 478 | + $this->assertTrue($indexes->get('_id_')['primary']); |
| 479 | + $this->assertTrue($indexes->get('unique_index_1')['unique']); |
| 480 | + |
| 481 | + // Non-existent collection |
| 482 | + $indexes = Schema::getIndexes('missing'); |
| 483 | + $this->assertSame([], $indexes); |
| 484 | + } |
| 485 | + |
419 | 486 | protected function getIndex(string $collection, string $name)
|
420 | 487 | {
|
421 | 488 | $collection = DB::getCollection($collection);
|
|
0 commit comments