Skip to content

Commit

Permalink
Merge pull request #8 from stevebauman/master
Browse files Browse the repository at this point in the history
Fix `Formatter` trait `toJson` and `toVue` methods
  • Loading branch information
denisdulici authored Apr 1, 2024
2 parents 64a466e + d6d98af commit 5911841
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 43 deletions.
8 changes: 6 additions & 2 deletions src/Config/apexcharts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

/*
|--------------------------------------------------------------------------
| Font Options
| ApexCharts Default Options
|--------------------------------------------------------------------------
|
| Here you may specify font family and font color.
| Here you may define the default options that will be applied to all
| ApexCharts rendered using this package. To learn more about each
| available option, check the official ApexCharts documentation.
|
| https://apexcharts.com/docs/options/
|
*/

Expand Down
15 changes: 8 additions & 7 deletions src/Traits/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

namespace Akaunting\Apexcharts\Traits;

use Illuminate\Http\JsonResponse;

/** @mixin \Akaunting\Apexcharts\Chart */
trait Formatter
{
public function toJson()
public function toJson(): JsonResponse
{
return response()->json([
'id' => $this->id(),
'options' => $this->getOptions(),
]);
return response()->json($this->toVue());
}

public function toVue(): array
{
return [
'id' => $this->getId(),
'height' => $this->getHeight(),
'width' => $this->getWidth(),
'type' => $this->getType(),
'options' => $this->getOptions(),
'series' => json_decode($this->getSeries()),
'options' => json_decode($this->getOptions(), true),
'series' => $this->getSeries(),
];
}
}
69 changes: 58 additions & 11 deletions tests/Feature/ChartsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class ChartsTest extends TestCase
{
/** @test */
public function testDefaultChart()
{
$chart = (new Chart)->setTitle('Users Test Chart');
Expand All @@ -16,7 +15,6 @@ public function testDefaultChart()
$this->assertEquals('line', $chart->getType());
}

/** @test */
public function testPieChart()
{
$chart = (new Chart)->setType('pie')
Expand All @@ -30,7 +28,6 @@ public function testPieChart()
$this->assertEquals('pie', $chart->getType());
}

/** @test */
public function testDonutChart()
{
$chart = (new Chart)->setType('donut')
Expand All @@ -42,7 +39,6 @@ public function testDonutChart()
$this->assertEquals('donut', $chart->getType());
}

/** @test */
public function testRadialChart()
{
$chart = (new Chart)->setType('radial')
Expand All @@ -54,7 +50,6 @@ public function testRadialChart()
$this->assertEquals('radial', $chart->getType());
}

/** @test */
public function testPolarChart()
{
$chart = (new Chart)->setType('polarArea')
Expand All @@ -66,7 +61,6 @@ public function testPolarChart()
$this->assertEquals('polarArea', $chart->getType());
}

/** @test */
public function testLineChart()
{
$chart = (new Chart)->setType('line')
Expand All @@ -90,7 +84,6 @@ public function testLineChart()
$this->assertEquals('line', $chart->getType());
}

/** @test */
public function testAreaChart()
{
$chart = (new Chart)->setType('area')
Expand All @@ -115,7 +108,6 @@ public function testAreaChart()
$this->assertEquals('area', $chart->getType());
}

/** @test */
public function testBarChart()
{
$chart = (new Chart)->setType('bar')
Expand Down Expand Up @@ -153,7 +145,6 @@ public function testBarChart()
$this->assertEquals('bar', $chart->getType());
}

/** @test */
public function testHorizontalBarChart()
{
$chart = (new Chart)->setType('bar')
Expand Down Expand Up @@ -181,7 +172,6 @@ public function testHorizontalBarChart()
$this->assertTrue($chart->getHorizontal());
}

/** @test */
public function testHeatmapChart()
{
$chart = (new Chart)->setType('heatmap')
Expand All @@ -205,7 +195,6 @@ public function testHeatmapChart()
$this->assertEquals('heatmap', $chart->getType());
}

/** @test */
public function testRadarChart()
{
$chart = (new Chart)->setType('radar')
Expand All @@ -228,4 +217,62 @@ public function testRadarChart()
$this->assertEquals($chart, $chart->script()['chart']);
$this->assertEquals('radar', $chart->getType());
}

public function testToVue()
{
$chart = (new Chart)->setType('line')
->setTitle('Total Users Monthly')
->setSubtitle('From January to March')
->setSeries([
'Jan', 'Feb', 'Mar'
])
->setDataset('Users', 'line', [
[
'name' => 'Active Users',
'data' => [250, 700, 1200]
]
])
->setHeight(250)
->setGridShow(true)
->setStrokeShow(true);

$this->assertEquals([
'id',
'height',
'width',
'type',
'options',
'series',
], array_keys($chart->toVue()));
}

public function testToJson()
{
$chart = (new Chart)->setType('line')
->setTitle('Total Users Monthly')
->setSubtitle('From January to March')
->setSeries([
'Jan', 'Feb', 'Mar'
])
->setDataset('Users', 'line', [
[
'name' => 'Active Users',
'data' => [250, 700, 1200]
]
])
->setHeight(250)
->setGridShow(true)
->setStrokeShow(true);

$response = $chart->toJson();

$this->assertEquals([
'id',
'height',
'width',
'type',
'options',
'series',
], array_keys(json_decode($response->content(), true)));
}
}
24 changes: 1 addition & 23 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,12 @@
class TestCase extends TestBenchTestCase
{
/**
* Sets the env data to interact as env file values
*
* @param [type] $app
* @return void
* Load the package service provider.
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('database.default', 'testing');

$app['config']->set('database.connection.testing', [
'driver' => 'sqlite',
'database' => ':memory:'
]);
}

// set providers to test the class
protected function getPackageProviders($app): array
{
return [
Provider::class,
];
}

// With this method I can use the facade instead of all class namespace
protected function getPackageAliases($app): array
{
return [
'FirstPackage' => Facade::class
];
}
}

0 comments on commit 5911841

Please sign in to comment.