|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace nullthoughts\LaravelDataSync\Tests; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\File; |
| 6 | +use Illuminate\Support\Facades\Storage; |
| 7 | +use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException; |
| 8 | +use nullthoughts\LaravelDataSync\Tests\fakes\UpdaterFake; |
| 9 | +use Exception; |
| 10 | + |
| 11 | +class UpdaterRemoteTest extends TestCase |
| 12 | +{ |
| 13 | + protected function setUp(): void |
| 14 | + { |
| 15 | + parent::setUp(); |
| 16 | + |
| 17 | + Storage::fake('s3'); |
| 18 | + Storage::disk('s3')->put('test-data/roles.json', File::get(__DIR__.'/../test-data/roles.json')); |
| 19 | + foreach (File::directories(__DIR__.'/../test-data/') as $directory) { |
| 20 | + $files = File::files($directory); |
| 21 | + |
| 22 | + foreach ($files as $file) { |
| 23 | + Storage::disk('s3')->put('test-data/'.basename($directory).'/'.$file->getRelativePathname(), File::get($file->getPathname())); |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /** @test */ |
| 29 | + public function it_adds_roles_to_the_database_in_remote() |
| 30 | + { |
| 31 | + $updater = new UpdaterFake('test-data', 'roles', true, 's3'); |
| 32 | + |
| 33 | + $updater->run(); |
| 34 | + |
| 35 | + $this->assertDatabaseHas('roles', ['slug' => 'update-student-records']); |
| 36 | + $this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']); |
| 37 | + $this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']); |
| 38 | + } |
| 39 | + |
| 40 | + /** @test */ |
| 41 | + public function it_can_default_to_configuration_in_remote() |
| 42 | + { |
| 43 | + config()->set('data-sync.path', 'test-data'); |
| 44 | + |
| 45 | + $updater = new UpdaterFake(null, null, true, 's3'); |
| 46 | + |
| 47 | + $updater->run(); |
| 48 | + |
| 49 | + $this->assertDatabaseHas('roles', ['slug' => 'update-student-records']); |
| 50 | + $this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']); |
| 51 | + $this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']); |
| 52 | + } |
| 53 | + |
| 54 | + /** @test */ |
| 55 | + public function it_can_update_an_existing_record_in_remote() |
| 56 | + { |
| 57 | + config()->set('data-sync.path', 'test-data'); |
| 58 | + (new UpdaterFake(null, null, true, 's3'))->run(); |
| 59 | + |
| 60 | + config()->set('data-sync.path', 'test-data/valid'); |
| 61 | + (new UpdaterFake(null, null, true, 's3'))->run(); |
| 62 | + |
| 63 | + $this->assertDatabaseHas('roles', ['category' => 'changed']); |
| 64 | + $this->assertDatabaseHas('roles', ['category' => 'changed']); |
| 65 | + $this->assertDatabaseHas('roles', ['category' => 'changed']); |
| 66 | + } |
| 67 | + |
| 68 | + /** @test */ |
| 69 | + public function it_can_update_the_relationship_in_remote() |
| 70 | + { |
| 71 | + $supervisor = Supervisor::create([ |
| 72 | + 'name' => 'CEO', |
| 73 | + ]); |
| 74 | + |
| 75 | + config()->set('data-sync.path', 'test-data/relationship'); |
| 76 | + (new UpdaterFake(null, null, true, 's3'))->run(); |
| 77 | + |
| 78 | + $this->assertEquals($supervisor->id, Roles::first()->supervisor_id); |
| 79 | + $this->assertTrue($supervisor->is(Roles::first()->supervisor)); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @test |
| 84 | + * @group current |
| 85 | + */ |
| 86 | + public function exception_is_thrown_if_the_directory_does_not_exists() |
| 87 | + { |
| 88 | + try { |
| 89 | + new UpdaterFake(null, null, true, 's3'); |
| 90 | + |
| 91 | + $this->fail('exception was thrown'); |
| 92 | + } catch (Exception $e) { |
| 93 | + $this->assertEquals('Specified sync file directory does not exist', $e->getMessage()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** @test */ |
| 98 | + public function invalid_json_throws_an_exception_in_remote() |
| 99 | + { |
| 100 | + try { |
| 101 | + $updater = new UpdaterFake('test-data/invalid-json', null, true, 's3'); |
| 102 | + $updater->run(); |
| 103 | + |
| 104 | + $this->fail('exception was thrown'); |
| 105 | + } catch (Exception $e) { |
| 106 | + $this->assertStringContainsString('No records or invalid JSON for', $e->getMessage()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** @test */ |
| 111 | + public function the_json_must_contain_a_key_with_an_underscore_in_remote() |
| 112 | + { |
| 113 | + try { |
| 114 | + $updater = new UpdaterFake('test-data/no-criteria', null, true, 's3'); |
| 115 | + $updater->run(); |
| 116 | + |
| 117 | + $this->fail('exception was thrown'); |
| 118 | + } catch (Exception $e) { |
| 119 | + $this->assertEquals('No criteria/attributes detected', $e->getMessage()); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** @test */ |
| 124 | + public function order_of_imports_can_be_defined_in_config_in_remote() |
| 125 | + { |
| 126 | + config()->set('data-sync.order', [ |
| 127 | + 'Supervisor', |
| 128 | + 'Roles', |
| 129 | + ]); |
| 130 | + |
| 131 | + $updater = new UpdaterFake('test-data/ordered', null, true, 's3'); |
| 132 | + $updater->run(); |
| 133 | + |
| 134 | + $this->assertDatabaseHas('roles', ['slug' => 'update-student-records']); |
| 135 | + $this->assertDatabaseHas('supervisors', ['name' => 'CEO']); |
| 136 | + } |
| 137 | + |
| 138 | + /** @test */ |
| 139 | + public function exception_is_thrown_if_imports_are_in_incorrect_order_in_remote() |
| 140 | + { |
| 141 | + config()->set('data-sync.order', [ |
| 142 | + 'Roles', |
| 143 | + 'Supervisor', |
| 144 | + ]); |
| 145 | + |
| 146 | + $this->expectException(ErrorUpdatingModelException::class); |
| 147 | + |
| 148 | + $updater = new UpdaterFake('test-data/ordered', null, true, 's3'); |
| 149 | + $updater->run(); |
| 150 | + } |
| 151 | + |
| 152 | + /** @test */ |
| 153 | + public function it_ignores_non_json_files_in_remote() |
| 154 | + { |
| 155 | + $updater = new UpdaterFake('test-data/not-json', null, true, 's3'); |
| 156 | + $updater->run(); |
| 157 | + |
| 158 | + $this->assertDatabaseMissing('roles', ['slug' => 'update-student-records']); |
| 159 | + } |
| 160 | +} |
0 commit comments