|
| 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 @group current */ |
| 29 | + public function it_adds_roles_to_the_database() |
| 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() |
| 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() |
| 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() |
| 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 | + /** @test */ |
| 83 | + public function exception_is_thrown_if_the_directory_does_not_exists() |
| 84 | + { |
| 85 | + try { |
| 86 | + new UpdaterFake(null, null, true, 's3'); |
| 87 | + |
| 88 | + $this->fail('exception was thrown'); |
| 89 | + } catch (Exception $e) { |
| 90 | + $this->assertEquals('Specified sync file directory does not exist', $e->getMessage()); |
| 91 | + } |
| 92 | + } |
| 93 | +// |
| 94 | +// /** @test */ |
| 95 | +// public function invalid_json_throws_an_exception() |
| 96 | +// { |
| 97 | +// try { |
| 98 | +// $updater = new UpdaterFake(__DIR__.'/../test-data/invalid-json'); |
| 99 | +// $updater->run(); |
| 100 | +// |
| 101 | +// $this->fail('exception was thrown'); |
| 102 | +// } catch (Exception $e) { |
| 103 | +// $this->assertStringContainsString('No records or invalid JSON for', $e->getMessage()); |
| 104 | +// } |
| 105 | +// } |
| 106 | +// |
| 107 | +// /** @test */ |
| 108 | +// public function the_json_must_contain_a_key_with_an_underscore() |
| 109 | +// { |
| 110 | +// try { |
| 111 | +// $updater = new UpdaterFake(__DIR__.'/../test-data/no-criteria'); |
| 112 | +// $updater->run(); |
| 113 | +// |
| 114 | +// $this->fail('exception was thrown'); |
| 115 | +// } catch (Exception $e) { |
| 116 | +// $this->assertEquals('No criteria/attributes detected', $e->getMessage()); |
| 117 | +// } |
| 118 | +// } |
| 119 | +// |
| 120 | +// /** @test */ |
| 121 | +// public function order_of_imports_can_be_defined_in_config() |
| 122 | +// { |
| 123 | +// config()->set('data-sync.order', [ |
| 124 | +// 'Supervisor', |
| 125 | +// 'Roles', |
| 126 | +// ]); |
| 127 | +// |
| 128 | +// $updater = new UpdaterFake(__DIR__.'/../test-data/ordered'); |
| 129 | +// $updater->run(); |
| 130 | +// |
| 131 | +// $this->assertDatabaseHas('roles', ['slug' => 'update-student-records']); |
| 132 | +// $this->assertDatabaseHas('supervisors', ['name' => 'CEO']); |
| 133 | +// } |
| 134 | +// |
| 135 | +// /** @test */ |
| 136 | +// public function exception_is_thrown_if_imports_are_in_incorrect_order() |
| 137 | +// { |
| 138 | +// config()->set('data-sync.order', [ |
| 139 | +// 'Roles', |
| 140 | +// 'Supervisor', |
| 141 | +// ]); |
| 142 | +// |
| 143 | +// $this->expectException(ErrorUpdatingModelException::class); |
| 144 | +// |
| 145 | +// $updater = new UpdaterFake(__DIR__.'/../test-data/ordered'); |
| 146 | +// $updater->run(); |
| 147 | +// } |
| 148 | +// |
| 149 | +// /** @test */ |
| 150 | +// public function it_ignores_non_json_files() |
| 151 | +// { |
| 152 | +// $updater = new UpdaterFake(__DIR__.'/../test-data/not-json'); |
| 153 | +// $updater->run(); |
| 154 | +// |
| 155 | +// $this->assertDatabaseMissing('roles', ['slug' => 'update-student-records']); |
| 156 | +// } |
| 157 | +} |
0 commit comments