|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use CalebDW\SqlEntities\Entities\Entity; |
| 6 | +use CalebDW\SqlEntities\Entities\View; |
| 7 | +use CalebDW\SqlEntities\EntityManager; |
| 8 | +use Illuminate\Database\Connection; |
| 9 | +use Illuminate\Database\DatabaseManager; |
| 10 | +use Workbench\Database\Entities\views\FooConnectionUserView; |
| 11 | +use Workbench\Database\Entities\views\UserView; |
| 12 | + |
| 13 | +beforeEach(function () { |
| 14 | + test()->connection = test()->mock(Connection::class); |
| 15 | + |
| 16 | + $db = test()->mock(DatabaseManager::class) |
| 17 | + ->shouldReceive('connection') |
| 18 | + ->andReturn(test()->connection) |
| 19 | + ->getMock(); |
| 20 | + app()->instance('db', $db); |
| 21 | + |
| 22 | + test()->manager = resolve(EntityManager::class); |
| 23 | +}); |
| 24 | + |
| 25 | +afterEach(function () { |
| 26 | + Mockery::close(); |
| 27 | +}); |
| 28 | + |
| 29 | +it('loads the entities') |
| 30 | + ->expect(test()->manager->entities) |
| 31 | + ->not->toBeEmpty(); |
| 32 | + |
| 33 | +describe('get', function () { |
| 34 | + it('returns the entity by name', function () { |
| 35 | + $entity = test()->manager->get('users_view'); |
| 36 | + |
| 37 | + expect($entity)->toBeInstanceOf(UserView::class); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns the entity by name and connection', function () { |
| 41 | + $entity = test()->manager->get('users_view', 'foo'); |
| 42 | + |
| 43 | + expect($entity)->toBeInstanceOf(FooConnectionUserView::class); |
| 44 | + }); |
| 45 | + |
| 46 | + it('throws an exception for unknown entity', function () { |
| 47 | + $entity = test()->manager->get('unknown'); |
| 48 | + })->throws(InvalidArgumentException::class, 'Entity [unknown] not found.'); |
| 49 | +}); |
| 50 | + |
| 51 | +it('creates an entity', function (string|Entity $entity) { |
| 52 | + test()->connection |
| 53 | + ->shouldReceive('getDriverName')->once()->andReturn('sqlite') |
| 54 | + ->shouldReceive('statement') |
| 55 | + ->once() |
| 56 | + ->withArgs(fn ($sql) => str_contains($sql, 'CREATE VIEW')); |
| 57 | + |
| 58 | + test()->manager->create($entity); |
| 59 | +})->with([ |
| 60 | + 'name' => 'users_view', |
| 61 | + 'entity' => new UserView(), |
| 62 | +]); |
| 63 | + |
| 64 | +it('drops an entity', function (string|Entity $entity) { |
| 65 | + test()->connection |
| 66 | + ->shouldReceive('getDriverName')->once()->andReturn('pgsql') |
| 67 | + ->shouldReceive('statement') |
| 68 | + ->once() |
| 69 | + ->withArgs(fn ($sql) => str_contains($sql, 'DROP VIEW')); |
| 70 | + |
| 71 | + test()->manager->drop($entity); |
| 72 | +})->with([ |
| 73 | + 'name' => 'users_view', |
| 74 | + 'entity' => new UserView(), |
| 75 | +]); |
| 76 | + |
| 77 | +it('creates entities by type and connection', function () { |
| 78 | + test()->connection |
| 79 | + ->shouldReceive('getDriverName')->once()->andReturn('sqlite') |
| 80 | + ->shouldReceive('statement') |
| 81 | + ->once() |
| 82 | + ->withArgs(fn ($sql) => str_contains($sql, 'CREATE VIEW')); |
| 83 | + |
| 84 | + test()->manager->createAll(View::class, 'foo'); |
| 85 | +}); |
| 86 | + |
| 87 | +it('drops entities by type and connection', function () { |
| 88 | + test()->connection |
| 89 | + ->shouldReceive('getDriverName')->once()->andReturn('pgsql') |
| 90 | + ->shouldReceive('statement') |
| 91 | + ->once() |
| 92 | + ->withArgs(fn ($sql) => str_contains($sql, 'DROP VIEW')); |
| 93 | + |
| 94 | + test()->manager->dropAll(View::class, 'foo'); |
| 95 | +}); |
| 96 | + |
| 97 | +it('throws exception for unsupported driver', function () { |
| 98 | + test()->connection |
| 99 | + ->shouldReceive('getDriverName') |
| 100 | + ->andReturn('unknown'); |
| 101 | + |
| 102 | + resolve(EntityManager::class)->create(new UserView()); |
| 103 | +})->throws(InvalidArgumentException::class, 'Unsupported driver [unknown].'); |
0 commit comments