JsonExporterTest::testExportToJson()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
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
Bug introduced by
The property name does not seem to exist on MathieuTu\JsonSyncer\Tests\Stubs\Bar. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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