1 | <?php namespace Nwidart\Modules\Tests; |
||
5 | class JsonTest extends BaseTestCase |
||
6 | { |
||
7 | /** |
||
8 | * @var Json |
||
9 | */ |
||
10 | private $json; |
||
11 | |||
12 | public function setUp() |
||
13 | { |
||
14 | parent::setUp(); |
||
15 | $path = __DIR__ . '/stubs/module.json'; |
||
16 | $this->json = new Json($path, $this->app['files']); |
||
17 | } |
||
18 | |||
19 | /** @test */ |
||
20 | public function it_gets_the_file_path() |
||
21 | { |
||
22 | $path = __DIR__ . '/stubs/module.json'; |
||
23 | |||
24 | $this->assertEquals($path, $this->json->getPath()); |
||
25 | } |
||
26 | |||
27 | /** @test */ |
||
28 | public function it_gets_attributes_from_json_file() |
||
29 | { |
||
30 | $this->assertEquals('Order', $this->json->get('name')); |
||
31 | $this->assertEquals('order', $this->json->get('alias')); |
||
32 | $this->assertEquals('My demo module', $this->json->get('description')); |
||
33 | $this->assertEquals('0.1', $this->json->get('version')); |
||
34 | $this->assertEquals(['my', 'stub', 'module'], $this->json->get('keywords')); |
||
35 | $this->assertEquals(1, $this->json->get('active')); |
||
36 | $this->assertEquals(1, $this->json->get('order')); |
||
37 | } |
||
38 | |||
39 | /** @test */ |
||
40 | public function it_reads_attributes_from_magic_get_method() |
||
41 | { |
||
42 | $this->assertEquals('Order', $this->json->name); |
||
43 | $this->assertEquals('order', $this->json->alias); |
||
44 | $this->assertEquals('My demo module', $this->json->description); |
||
45 | $this->assertEquals('0.1', $this->json->version); |
||
46 | $this->assertEquals(['my', 'stub', 'module'], $this->json->keywords); |
||
47 | $this->assertEquals(1, $this->json->active); |
||
48 | $this->assertEquals(1, $this->json->order); |
||
49 | } |
||
50 | |||
51 | /** @test */ |
||
52 | public function it_makes_json_class() |
||
53 | { |
||
54 | $path = __DIR__ . '/stubs/module.json'; |
||
55 | $json = Json::make($path, $this->app['files']); |
||
56 | |||
57 | $this->assertInstanceOf(Json::class, $json); |
||
58 | } |
||
59 | |||
60 | /** @test */ |
||
61 | public function it_sets_a_path() |
||
62 | { |
||
63 | $path = __DIR__ . '/stubs/module.json'; |
||
64 | $this->assertEquals($path, $this->json->getPath()); |
||
65 | |||
66 | $this->json->setPath('some/path.json'); |
||
67 | $this->assertEquals('some/path.json', $this->json->getPath()); |
||
68 | } |
||
69 | |||
70 | /** @test */ |
||
71 | public function it_decodes_json() |
||
72 | { |
||
73 | $expected = '{ |
||
74 | "name": "Order", |
||
75 | "alias": "order", |
||
76 | "description": "My demo module", |
||
77 | "version": "0.1", |
||
78 | "keywords": [ |
||
79 | "my", |
||
80 | "stub", |
||
81 | "module" |
||
82 | ], |
||
83 | "active": 1, |
||
84 | "order": 1, |
||
85 | "providers": [ |
||
86 | "Modules\\\Order\\\Providers\\\OrderServiceProvider", |
||
87 | "Modules\\\Order\\\Providers\\\EventServiceProvider", |
||
88 | "Modules\\\Order\\\Providers\\\RouteServiceProvider" |
||
89 | ], |
||
90 | "aliases": [], |
||
91 | "files": [] |
||
92 | }'; |
||
93 | $this->assertEquals($expected, $this->json->toJsonPretty()); |
||
94 | } |
||
95 | |||
96 | /** @test */ |
||
97 | public function it_sets_a_key_value() |
||
98 | { |
||
99 | $this->json->set('key', 'value'); |
||
100 | |||
101 | $this->assertEquals('value', $this->json->get('key')); |
||
102 | } |
||
103 | |||
104 | /** @test */ |
||
105 | public function it_can_be_casted_to_string() |
||
106 | { |
||
107 | $expected = '{ |
||
108 | "name": "Order", |
||
109 | "alias": "order", |
||
110 | "description": "My demo module", |
||
111 | "version": "0.1", |
||
112 | "keywords": [ |
||
113 | "my", |
||
114 | "stub", |
||
115 | "module" |
||
116 | ], |
||
117 | "active": 1, |
||
118 | "order": 1, |
||
119 | "providers": [ |
||
120 | "Modules\\\Order\\\Providers\\\OrderServiceProvider", |
||
121 | "Modules\\\Order\\\Providers\\\EventServiceProvider", |
||
122 | "Modules\\\Order\\\Providers\\\RouteServiceProvider" |
||
123 | ], |
||
124 | "aliases":{}, |
||
125 | "files": [ |
||
126 | ] |
||
127 | } |
||
128 | '; |
||
129 | $this->assertEquals($expected, (string)$this->json); |
||
130 | } |
||
131 | } |
||
132 |