1+ <?php
2+
3+ namespace distinctm \LaravelDataSync \Tests ;
4+
5+ use distinctm \LaravelDataSync \Tests \Fakes \UpdaterFake ;
6+ use Exception ;
7+
8+ class UpdaterTest extends TestCase
9+ {
10+ /** @test */
11+ public function it_adds_roles_to_the_database ()
12+ {
13+ $ updater = new UpdaterFake (__DIR__ . '/../test-data ' , 'roles ' );
14+
15+ $ updater ->run ();
16+
17+ $ this ->assertDatabaseHas ('roles ' , ['slug ' => 'update-student-records ' ]);
18+ $ this ->assertDatabaseHas ('roles ' , ['slug ' => 'borrow-ferrari ' ]);
19+ $ this ->assertDatabaseHas ('roles ' , ['slug ' => 'destroy-ferrari ' ]);
20+ }
21+
22+ /** @test */
23+ public function it_can_default_to_configuration ()
24+ {
25+ config ()->set ('data-sync.path ' , __DIR__ . '/../test-data ' );
26+
27+ $ updater = new UpdaterFake ();
28+
29+ $ updater ->run ();
30+
31+ $ this ->assertDatabaseHas ('roles ' , ['slug ' => 'update-student-records ' ]);
32+ $ this ->assertDatabaseHas ('roles ' , ['slug ' => 'borrow-ferrari ' ]);
33+ $ this ->assertDatabaseHas ('roles ' , ['slug ' => 'destroy-ferrari ' ]);
34+ }
35+
36+ /** @test */
37+ public function it_can_update_an_existing_record ()
38+ {
39+ config ()->set ('data-sync.path ' , __DIR__ . '/../test-data ' );
40+ (new UpdaterFake ())->run ();
41+
42+ config ()->set ('data-sync.path ' , __DIR__ . '/../test-data/valid ' );
43+ (new UpdaterFake ())->run ();
44+
45+ $ this ->assertDatabaseHas ('roles ' , ['category ' => 'changed ' ]);
46+ $ this ->assertDatabaseHas ('roles ' , ['category ' => 'changed ' ]);
47+ $ this ->assertDatabaseHas ('roles ' , ['category ' => 'changed ' ]);
48+ }
49+
50+ /** @test */
51+ public function it_can_update_the_relationship ()
52+ {
53+ $ supervisor = Supervisor::create ([
54+ 'name ' => 'CEO ' ,
55+ ]);
56+
57+ config ()->set ('data-sync.path ' , __DIR__ . '/../test-data/relationship ' , 'roles ' );
58+ (new UpdaterFake ())->run ();
59+
60+ $ this ->assertEquals ($ supervisor ->id , Roles::first ()->supervisor_id );
61+ $ this ->assertTrue ($ supervisor ->is (Roles::first ()->supervisor ));
62+ }
63+
64+ /** @test */
65+ public function exception_is_thrown_if_the_directory_does_not_exists ()
66+ {
67+ try {
68+ new UpdaterFake ();
69+
70+ $ this ->fail ('exception was thrown ' );
71+
72+ } catch (Exception $ e ) {
73+ $ this ->assertEquals ('Specified sync file directory does not exist ' , $ e ->getMessage ());
74+ }
75+ }
76+
77+ /** @test */
78+ public function invalid_json_throws_an_exception ()
79+ {
80+ try {
81+ $ updater = new UpdaterFake (__DIR__ . '/../test-data/invalid-json ' );
82+ $ updater ->run ();
83+
84+ $ this ->fail ('exception was thrown ' );
85+
86+ } catch (Exception $ e ) {
87+ $ this ->assertContains ('No records or invalid JSON for ' , $ e ->getMessage ());
88+ }
89+
90+ }
91+
92+ /** @test */
93+ public function the_json_must_contain_a_key_with_an_underscore ()
94+ {
95+ try {
96+ $ updater = new UpdaterFake (__DIR__ . '/../test-data/no-criteria ' );
97+ $ updater ->run ();
98+
99+ $ this ->fail ('exception was thrown ' );
100+
101+ } catch (Exception $ e ) {
102+ $ this ->assertEquals ('No criteria/attributes detected ' , $ e ->getMessage ());
103+ }
104+
105+ }
106+ }
0 commit comments