Skip to content

Commit 5911841

Browse files
authored
Merge pull request #8 from stevebauman/master
Fix `Formatter` trait `toJson` and `toVue` methods
2 parents 64a466e + d6d98af commit 5911841

File tree

4 files changed

+73
-43
lines changed

4 files changed

+73
-43
lines changed

src/Config/apexcharts.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
/*
66
|--------------------------------------------------------------------------
7-
| Font Options
7+
| ApexCharts Default Options
88
|--------------------------------------------------------------------------
99
|
10-
| Here you may specify font family and font color.
10+
| Here you may define the default options that will be applied to all
11+
| ApexCharts rendered using this package. To learn more about each
12+
| available option, check the official ApexCharts documentation.
13+
|
14+
| https://apexcharts.com/docs/options/
1115
|
1216
*/
1317

src/Traits/Formatter.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22

33
namespace Akaunting\Apexcharts\Traits;
44

5+
use Illuminate\Http\JsonResponse;
6+
7+
/** @mixin \Akaunting\Apexcharts\Chart */
58
trait Formatter
69
{
7-
public function toJson()
10+
public function toJson(): JsonResponse
811
{
9-
return response()->json([
10-
'id' => $this->id(),
11-
'options' => $this->getOptions(),
12-
]);
12+
return response()->json($this->toVue());
1313
}
1414

1515
public function toVue(): array
1616
{
1717
return [
18+
'id' => $this->getId(),
1819
'height' => $this->getHeight(),
1920
'width' => $this->getWidth(),
2021
'type' => $this->getType(),
21-
'options' => $this->getOptions(),
22-
'series' => json_decode($this->getSeries()),
22+
'options' => json_decode($this->getOptions(), true),
23+
'series' => $this->getSeries(),
2324
];
2425
}
2526
}

tests/Feature/ChartsTest.php

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
class ChartsTest extends TestCase
99
{
10-
/** @test */
1110
public function testDefaultChart()
1211
{
1312
$chart = (new Chart)->setTitle('Users Test Chart');
@@ -16,7 +15,6 @@ public function testDefaultChart()
1615
$this->assertEquals('line', $chart->getType());
1716
}
1817

19-
/** @test */
2018
public function testPieChart()
2119
{
2220
$chart = (new Chart)->setType('pie')
@@ -30,7 +28,6 @@ public function testPieChart()
3028
$this->assertEquals('pie', $chart->getType());
3129
}
3230

33-
/** @test */
3431
public function testDonutChart()
3532
{
3633
$chart = (new Chart)->setType('donut')
@@ -42,7 +39,6 @@ public function testDonutChart()
4239
$this->assertEquals('donut', $chart->getType());
4340
}
4441

45-
/** @test */
4642
public function testRadialChart()
4743
{
4844
$chart = (new Chart)->setType('radial')
@@ -54,7 +50,6 @@ public function testRadialChart()
5450
$this->assertEquals('radial', $chart->getType());
5551
}
5652

57-
/** @test */
5853
public function testPolarChart()
5954
{
6055
$chart = (new Chart)->setType('polarArea')
@@ -66,7 +61,6 @@ public function testPolarChart()
6661
$this->assertEquals('polarArea', $chart->getType());
6762
}
6863

69-
/** @test */
7064
public function testLineChart()
7165
{
7266
$chart = (new Chart)->setType('line')
@@ -90,7 +84,6 @@ public function testLineChart()
9084
$this->assertEquals('line', $chart->getType());
9185
}
9286

93-
/** @test */
9487
public function testAreaChart()
9588
{
9689
$chart = (new Chart)->setType('area')
@@ -115,7 +108,6 @@ public function testAreaChart()
115108
$this->assertEquals('area', $chart->getType());
116109
}
117110

118-
/** @test */
119111
public function testBarChart()
120112
{
121113
$chart = (new Chart)->setType('bar')
@@ -153,7 +145,6 @@ public function testBarChart()
153145
$this->assertEquals('bar', $chart->getType());
154146
}
155147

156-
/** @test */
157148
public function testHorizontalBarChart()
158149
{
159150
$chart = (new Chart)->setType('bar')
@@ -181,7 +172,6 @@ public function testHorizontalBarChart()
181172
$this->assertTrue($chart->getHorizontal());
182173
}
183174

184-
/** @test */
185175
public function testHeatmapChart()
186176
{
187177
$chart = (new Chart)->setType('heatmap')
@@ -205,7 +195,6 @@ public function testHeatmapChart()
205195
$this->assertEquals('heatmap', $chart->getType());
206196
}
207197

208-
/** @test */
209198
public function testRadarChart()
210199
{
211200
$chart = (new Chart)->setType('radar')
@@ -228,4 +217,62 @@ public function testRadarChart()
228217
$this->assertEquals($chart, $chart->script()['chart']);
229218
$this->assertEquals('radar', $chart->getType());
230219
}
220+
221+
public function testToVue()
222+
{
223+
$chart = (new Chart)->setType('line')
224+
->setTitle('Total Users Monthly')
225+
->setSubtitle('From January to March')
226+
->setSeries([
227+
'Jan', 'Feb', 'Mar'
228+
])
229+
->setDataset('Users', 'line', [
230+
[
231+
'name' => 'Active Users',
232+
'data' => [250, 700, 1200]
233+
]
234+
])
235+
->setHeight(250)
236+
->setGridShow(true)
237+
->setStrokeShow(true);
238+
239+
$this->assertEquals([
240+
'id',
241+
'height',
242+
'width',
243+
'type',
244+
'options',
245+
'series',
246+
], array_keys($chart->toVue()));
247+
}
248+
249+
public function testToJson()
250+
{
251+
$chart = (new Chart)->setType('line')
252+
->setTitle('Total Users Monthly')
253+
->setSubtitle('From January to March')
254+
->setSeries([
255+
'Jan', 'Feb', 'Mar'
256+
])
257+
->setDataset('Users', 'line', [
258+
[
259+
'name' => 'Active Users',
260+
'data' => [250, 700, 1200]
261+
]
262+
])
263+
->setHeight(250)
264+
->setGridShow(true)
265+
->setStrokeShow(true);
266+
267+
$response = $chart->toJson();
268+
269+
$this->assertEquals([
270+
'id',
271+
'height',
272+
'width',
273+
'type',
274+
'options',
275+
'series',
276+
], array_keys(json_decode($response->content(), true)));
277+
}
231278
}

tests/TestCase.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,12 @@
99
class TestCase extends TestBenchTestCase
1010
{
1111
/**
12-
* Sets the env data to interact as env file values
13-
*
14-
* @param [type] $app
15-
* @return void
12+
* Load the package service provider.
1613
*/
17-
protected function getEnvironmentSetUp($app)
18-
{
19-
$app['config']->set('database.default', 'testing');
20-
21-
$app['config']->set('database.connection.testing', [
22-
'driver' => 'sqlite',
23-
'database' => ':memory:'
24-
]);
25-
}
26-
27-
// set providers to test the class
2814
protected function getPackageProviders($app): array
2915
{
3016
return [
3117
Provider::class,
3218
];
3319
}
34-
35-
// With this method I can use the facade instead of all class namespace
36-
protected function getPackageAliases($app): array
37-
{
38-
return [
39-
'FirstPackage' => Facade::class
40-
];
41-
}
4220
}

0 commit comments

Comments
 (0)