mathieutu /
laravel-json-syncer
| 1 | <?php |
||
| 2 | |||
| 3 | namespace MathieuTu\JsonSyncer\Tests; |
||
| 4 | |||
| 5 | use MathieuTu\JsonSyncer\Tests\Stubs\Bar; |
||
| 6 | use MathieuTu\JsonSyncer\Tests\Stubs\Foo; |
||
| 7 | |||
| 8 | class JsonExporterTest extends TestCase |
||
| 9 | { |
||
| 10 | public function testExportToJson() |
||
| 11 | { |
||
| 12 | $this->setDatabase(); |
||
| 13 | |||
| 14 | $this->assertEquals( |
||
| 15 | json_decode(file_get_contents(__DIR__ . '/Stubs/import.json')), |
||
| 16 | json_decode(Foo::query()->first()->exportToJson()) |
||
| 17 | ); |
||
| 18 | } |
||
| 19 | |||
| 20 | public function testExportToCollection() |
||
| 21 | { |
||
| 22 | $this->setDatabase(); |
||
| 23 | |||
| 24 | $this->assertEquals( |
||
| 25 | json_decode(file_get_contents(__DIR__ . '/Stubs/import.json'), true), |
||
| 26 | Foo::query()->first()->exportToCollection()->toArray() |
||
| 27 | ); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function testIsExportingMethod() |
||
| 31 | { |
||
| 32 | $this->setDatabase(); |
||
| 33 | |||
| 34 | $foo = $this->fooModelForTest()->firstOrFail(); |
||
| 35 | |||
| 36 | $this->assertEquals('Mathieu TUDISCO', $foo->author); |
||
| 37 | |||
| 38 | $this->expectExceptionMessage('Is exporting ok !'); |
||
| 39 | $foo->exportToJson(); |
||
| 40 | } |
||
| 41 | |||
| 42 | protected function setDatabase() |
||
| 43 | { |
||
| 44 | (new Foo)->create(['author' => 'Mathieu TUDISCO', 'username' => '@mathieutu']) |
||
| 45 | ->bars()->createMany([ |
||
| 46 | ['name' => 'bar1'], |
||
| 47 | ['name' => 'bar2'], |
||
| 48 | ])->each(function (Bar $bar) { |
||
| 49 | $bar->baz()->create(['name' => $bar->name . '_baz']) |
||
|
0 ignored issues
–
show
|
|||
| 50 | ->doNots()->createMany([ |
||
| 51 | ['name' => 'do not 1'], |
||
| 52 | ['name' => 'do not 2'], |
||
| 53 | ['name' => 'do not 3'], |
||
| 54 | ]); |
||
| 55 | }); |
||
| 56 | } |
||
| 57 | |||
| 58 | protected function fooModelForTest() |
||
| 59 | { |
||
| 60 | return new class extends Foo { |
||
| 61 | protected $fillable = ['author']; |
||
| 62 | protected $table = 'foos'; |
||
| 63 | |||
| 64 | public function getAuthorAttribute($value) |
||
| 65 | { |
||
| 66 | if ($this->isExporting()) { |
||
| 67 | throw new \Exception('Is exporting ok !'); |
||
| 68 | } |
||
| 69 | |||
| 70 | return $value; |
||
| 71 | } |
||
| 72 | }; |
||
| 73 | } |
||
| 74 | } |
||
| 75 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.