|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BeyondCode\LaravelMaskedDumper\Tests; |
| 4 | + |
| 5 | +use BeyondCode\LaravelMaskedDumper\DumpSchema; |
| 6 | +use BeyondCode\LaravelMaskedDumper\LaravelMaskedDumpServiceProvider; |
| 7 | +use BeyondCode\LaravelMaskedDumper\TableDefinitions\TableDefinition; |
| 8 | +use Faker\Generator; |
| 9 | +use Illuminate\Auth\Authenticatable; |
| 10 | +use Illuminate\Support\Facades\DB; |
| 11 | +use Orchestra\Testbench\TestCase; |
| 12 | +use Spatie\Snapshots\MatchesSnapshots; |
| 13 | + |
| 14 | +class DumperTest extends TestCase |
| 15 | +{ |
| 16 | + use MatchesSnapshots; |
| 17 | + |
| 18 | + protected function getPackageProviders($app) |
| 19 | + { |
| 20 | + return [LaravelMaskedDumpServiceProvider::class]; |
| 21 | + } |
| 22 | + |
| 23 | + protected function getEnvironmentSetUp($app) |
| 24 | + { |
| 25 | + // Setup default database to use sqlite :memory: |
| 26 | + $app['config']->set('database.default', 'testbench'); |
| 27 | + $app['config']->set('database.connections.testbench', [ |
| 28 | + 'driver' => 'sqlite', |
| 29 | + 'database' => ':memory:', |
| 30 | + 'prefix' => '', |
| 31 | + ]); |
| 32 | + } |
| 33 | + |
| 34 | + /** @test */ |
| 35 | + public function it_can_dump_all_tables_without_modifications() |
| 36 | + { |
| 37 | + $this->loadLaravelMigrations(); |
| 38 | + |
| 39 | + DB::table('users') |
| 40 | + ->insert([ |
| 41 | + 'name' => 'Marcel', |
| 42 | + 'email' => 'marcel@beyondco.de', |
| 43 | + 'password' => 'test', |
| 44 | + 'created_at' => '2021-01-01 00:00:00', |
| 45 | + 'updated_at' => '2021-01-01 00:00:00', |
| 46 | + ]); |
| 47 | + |
| 48 | + $outputFile = base_path('test.sql'); |
| 49 | + |
| 50 | + $this->app['config']['masked-dump.default'] = DumpSchema::define()->allTables(); |
| 51 | + |
| 52 | + $this->artisan('db:dump', [ |
| 53 | + 'output' => $outputFile |
| 54 | + ]); |
| 55 | + |
| 56 | + $this->assertMatchesTextSnapshot(file_get_contents($outputFile)); |
| 57 | + } |
| 58 | + |
| 59 | + /** @test */ |
| 60 | + public function it_can_mask_user_names() |
| 61 | + { |
| 62 | + $this->loadLaravelMigrations(); |
| 63 | + |
| 64 | + DB::table('users') |
| 65 | + ->insert([ |
| 66 | + 'name' => 'Marcel', |
| 67 | + 'email' => 'marcel@beyondco.de', |
| 68 | + 'password' => 'test', |
| 69 | + 'created_at' => '2021-01-01 00:00:00', |
| 70 | + 'updated_at' => '2021-01-01 00:00:00', |
| 71 | + ]); |
| 72 | + |
| 73 | + $outputFile = base_path('test.sql'); |
| 74 | + |
| 75 | + $this->app['config']['masked-dump.default'] = DumpSchema::define() |
| 76 | + ->allTables() |
| 77 | + ->table('users', function (TableDefinition $table) { |
| 78 | + $table->mask('name'); |
| 79 | + }); |
| 80 | + |
| 81 | + $this->artisan('db:dump', [ |
| 82 | + 'output' => $outputFile |
| 83 | + ]); |
| 84 | + |
| 85 | + $this->assertMatchesTextSnapshot(file_get_contents($outputFile)); |
| 86 | + } |
| 87 | + |
| 88 | + /** @test */ |
| 89 | + public function it_can_replace_columns_with_static_values() |
| 90 | + { |
| 91 | + $this->loadLaravelMigrations(); |
| 92 | + |
| 93 | + DB::table('users') |
| 94 | + ->insert([ |
| 95 | + 'name' => 'Marcel', |
| 96 | + 'email' => 'marcel@beyondco.de', |
| 97 | + 'password' => 'test', |
| 98 | + 'created_at' => '2021-01-01 00:00:00', |
| 99 | + 'updated_at' => '2021-01-01 00:00:00', |
| 100 | + ]); |
| 101 | + |
| 102 | + $outputFile = base_path('test.sql'); |
| 103 | + |
| 104 | + $this->app['config']['masked-dump.default'] = DumpSchema::define() |
| 105 | + ->allTables() |
| 106 | + ->table('users', function (TableDefinition $table) { |
| 107 | + $table->replace('password', 'test'); |
| 108 | + }); |
| 109 | + |
| 110 | + $this->artisan('db:dump', [ |
| 111 | + 'output' => $outputFile |
| 112 | + ]); |
| 113 | + |
| 114 | + $this->assertMatchesTextSnapshot(file_get_contents($outputFile)); |
| 115 | + } |
| 116 | + |
| 117 | + /** @test */ |
| 118 | + public function it_can_replace_columns_with_faker_values() |
| 119 | + { |
| 120 | + $this->loadLaravelMigrations(); |
| 121 | + |
| 122 | + DB::table('users') |
| 123 | + ->insert([ |
| 124 | + 'name' => 'Marcel', |
| 125 | + 'email' => 'marcel@beyondco.de', |
| 126 | + 'password' => 'test', |
| 127 | + 'created_at' => '2021-01-01 00:00:00', |
| 128 | + 'updated_at' => '2021-01-01 00:00:00', |
| 129 | + ]); |
| 130 | + |
| 131 | + $outputFile = base_path('test.sql'); |
| 132 | + |
| 133 | + $this->app['config']['masked-dump.default'] = DumpSchema::define() |
| 134 | + ->allTables() |
| 135 | + ->table('users', function (TableDefinition $table, Generator $faker) { |
| 136 | + $faker->seed(1); |
| 137 | + $table->replace('email', $faker->safeEmail()); |
| 138 | + }); |
| 139 | + |
| 140 | + $this->artisan('db:dump', [ |
| 141 | + 'output' => $outputFile |
| 142 | + ]); |
| 143 | + |
| 144 | + $this->assertMatchesTextSnapshot(file_get_contents($outputFile)); |
| 145 | + } |
| 146 | + |
| 147 | + /** @test */ |
| 148 | + public function it_can_dump_certain_tables_as_schema_only() |
| 149 | + { |
| 150 | + $this->loadLaravelMigrations(); |
| 151 | + |
| 152 | + DB::table('users') |
| 153 | + ->insert([ |
| 154 | + 'name' => 'Marcel', |
| 155 | + 'email' => 'marcel@beyondco.de', |
| 156 | + 'password' => 'test', |
| 157 | + 'created_at' => '2021-01-01 00:00:00', |
| 158 | + 'updated_at' => '2021-01-01 00:00:00', |
| 159 | + ]); |
| 160 | + |
| 161 | + $outputFile = base_path('test.sql'); |
| 162 | + |
| 163 | + $this->app['config']['masked-dump.default'] = DumpSchema::define() |
| 164 | + ->allTables() |
| 165 | + ->schemaOnly('migrations') |
| 166 | + ->schemaOnly('users'); |
| 167 | + |
| 168 | + $this->artisan('db:dump', [ |
| 169 | + 'output' => $outputFile |
| 170 | + ]); |
| 171 | + |
| 172 | + $this->assertMatchesTextSnapshot(file_get_contents($outputFile)); |
| 173 | + } |
| 174 | +} |
0 commit comments