Skip to content

Commit

Permalink
Version 2.20.12.
Browse files Browse the repository at this point in the history
- Replaced engine string values by "Engine" enumeration.
  • Loading branch information
laurentmuller committed Apr 22, 2024
1 parent f496049 commit 343117e
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

## [Unreleased]

## [2.20.12] - 2024-04-22

- Removed `orklah/psalm-*` plugins.
- Replaced engine string values by `Engine` enumeration.

## [2.20.11] - 2024-04-18

Expand Down Expand Up @@ -140,6 +143,7 @@ as possible. See [Security Advisory: ZF2014-01](https://framework.zend.com/secur
- Initial release

[Unreleased]: https://github.com/laurentmuller/HighchartsBundle/compare/1.7...HEAD
[2.20.12]: https://github.com/laurentmuller/HighchartsBundle/compare/2.20.11...2.20.12
[2.20.11]: https://github.com/laurentmuller/HighchartsBundle/compare/2.20.10...2.20.11
[2.20.10]: https://github.com/laurentmuller/HighchartsBundle/compare/2.20.9...2.20.10
[2.20.9]: https://github.com/laurentmuller/HighchartsBundle/compare/2.20.8...2.20.9
Expand Down
26 changes: 7 additions & 19 deletions src/Highcharts/AbstractChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ public static function createExpression(string $expression): Expr
return new Expr($expression);
}

/**
* @psalm-param ChartInterface::ENGINE_* $engine
*/
public function render(string $engine = self::ENGINE_JQUERY): string
public function render(Engine $engine = Engine::JQUERY): string
{
$chartJS = '';
$this->renderChartStart($chartJS, $engine);
Expand Down Expand Up @@ -174,13 +171,10 @@ protected function renderChartCommon(string &$chartJS): void
$this->renderAccessibility($chartJS);
}

/**
* @psalm-param ChartInterface::ENGINE_* $engine
*/
protected function renderChartEnd(string &$chartJS, string $engine): void
protected function renderChartEnd(string &$chartJS, Engine $engine): void
{
$chartJS = \rtrim($chartJS, self::END_LINE) . self::NEW_LINE . self::HALF_SPACE . '});' . self::NEW_LINE;
if (self::ENGINE_NONE !== $engine) {
if (Engine::NONE !== $engine) {
$chartJS .= '});' . self::NEW_LINE;
}
}
Expand All @@ -189,10 +183,7 @@ protected function renderChartOptions(string &$chartJS): void
{
}

/**
* @psalm-param ChartInterface::ENGINE_* $engine
*/
protected function renderChartStart(string &$chartJS, string $engine): void
protected function renderChartStart(string &$chartJS, Engine $engine): void
{
$this->renderEngine($chartJS, $engine);
$this->renderOptions($chartJS);
Expand All @@ -209,14 +200,11 @@ protected function renderCredits(string &$chartJS): void
$chartJS .= $this->jsonEncode($this->credits);
}

/**
* @psalm-param ChartInterface::ENGINE_* $engine
*/
protected function renderEngine(string &$chartJS, string $engine): void
protected function renderEngine(string &$chartJS, Engine $engine): void
{
$chartJS .= match ($engine) {
self::ENGINE_MOOTOOLS => 'window.addEvent(\'domready\', function () {',
self::ENGINE_JQUERY => '$(function () {',
Engine::MOOTOOLS => 'window.addEvent(\'domready\', function () {',
Engine::JQUERY => '$(function () {',
default => '',
};
}
Expand Down
10 changes: 1 addition & 9 deletions src/Highcharts/ChartInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,8 @@
*/
interface ChartInterface
{
public const ENGINE_JQUERY = 'jquery';

public const ENGINE_MOOTOOLS = 'mootools';

public const ENGINE_NONE = '';

/**
* Render this chart for the given engine.
*
* @psalm-param self::ENGINE_* $engine
*/
public function render(string $engine): string;
public function render(Engine $engine): string;
}
34 changes: 34 additions & 0 deletions src/Highcharts/Engine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/*
* This file is part of the HighchartsBundle package.
*
* (c) bibi.nu <bibi@bibi.nu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace HighchartsBundle\Highcharts;

/**
* The render engine enumeration.
*
* @see ChartInterface::render()
*/
enum Engine: string
{
/**
* The JQuery engine.
*/
case JQUERY = 'jquery';
/**
* The MooTools engine.
*/
case MOOTOOLS = 'mootools';
/**
* No engine.
*/
case NONE = '';
}
22 changes: 20 additions & 2 deletions src/Twig/HighchartsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
namespace HighchartsBundle\Twig;

use HighchartsBundle\Highcharts\ChartInterface;
use HighchartsBundle\Highcharts\Engine;
use Twig\Error\SyntaxError;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

Expand All @@ -24,10 +26,14 @@ class HighchartsExtension extends AbstractExtension
/**
* Render the given chart with the given engine.
*
* @psalm-param ChartInterface::ENGINE_* $engine
* @throws SyntaxError if the engine is a string and the corresponding enumeration cannot be found
*/
public function chart(ChartInterface $chart, string $engine = ChartInterface::ENGINE_JQUERY): string
public function chart(ChartInterface $chart, Engine|string $engine = Engine::JQUERY): string
{
if (\is_string($engine)) {
$engine = $this->parseEngine($engine);
}

return $chart->render($engine);
}

Expand All @@ -37,4 +43,16 @@ public function getFunctions(): array
new TwigFunction('chart', $this->chart(...), ['is_safe' => ['html']]),
];
}

/**
* @throws SyntaxError
*/
private function parseEngine(string $engine): Engine
{
try {
return Engine::from($engine);
} catch (\ValueError $e) {
throw new SyntaxError("Invalid chart engine: \"$engine\".", previous: $e);
}
}
}
11 changes: 6 additions & 5 deletions tests/AbstractChartTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
namespace HighchartsBundle\Tests;

use HighchartsBundle\Highcharts\ChartInterface;
use HighchartsBundle\Highcharts\Engine;
use PHPUnit\Framework\TestCase;

abstract class AbstractChartTestCase extends TestCase
{
/**
* @psalm-param ChartInterface::ENGINE_* $engine
*/
protected static function assertChartMatchesRegularExpression(ChartInterface $chart, string $regex, string $engine = 'jquery'): void
{
protected static function assertChartMatchesRegularExpression(
ChartInterface $chart,
string $regex,
Engine $engine = Engine::JQUERY
): void {
$result = $chart->render($engine);
self::assertMatchesRegularExpression($regex, $result);
}
Expand Down
60 changes: 60 additions & 0 deletions tests/Highcharts/EngineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/*
* This file is part of the HighchartsBundle package.
*
* (c) bibi.nu <bibi@bibi.nu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace HighchartsBundle\Tests\Highcharts;

use HighchartsBundle\Highcharts\Engine;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class EngineTest extends TestCase
{
public static function getInvalidValues(): \Generator
{
yield ['fake'];
}

public static function getValues(): \Generator
{
yield [Engine::JQUERY, 'jquery'];
yield [Engine::MOOTOOLS, 'mootools'];
yield [Engine::NONE, ''];
}

public function testCount(): void
{
$values = Engine::cases();
self::assertCount(3, $values);
}

#[DataProvider('getValues')]
public function testFrom(Engine $engine, string $value): void
{
$actual = Engine::from($value);
self::assertSame($actual, $engine);
}

#[DataProvider('getInvalidValues')]
public function testInvalidValue(string $value): void
{
self::expectException(\ValueError::class);
Engine::from($value);
self::fail('A value error must be raised.');
}

#[DataProvider('getValues')]
public function testValue(Engine $engine, string $expected): void
{
$actual = $engine->value;
self::assertSame($expected, $actual);
}
}
7 changes: 4 additions & 3 deletions tests/Highcharts/HighchartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace HighchartsBundle\Tests\Highcharts;

use HighchartsBundle\Highcharts\Engine;
use HighchartsBundle\Highcharts\Highchart;
use HighchartsBundle\Tests\AbstractChartTestCase;

Expand All @@ -37,17 +38,17 @@ public function testMooTools(): void
{
$chart = new Highchart();
$regex = '/window.addEvent\(\'domready\', function\s?\(\)\s?\{\r?\n?\s*const chart = new Highcharts.Chart\(\{\n?\r?\s*\}\);\n?\r?\s*\}\);/';
self::assertChartMatchesRegularExpression($chart, $regex, 'mootools');
self::assertChartMatchesRegularExpression($chart, $regex, Engine::MOOTOOLS);
}

/**
* Render chart without library wrapper.
* Render chart without a library wrapper.
*/
public function testNoEngine(): void
{
$chart = new Highchart();
$regex = '/const chart = new Highcharts.Chart\(\{\n?\r?\s*\}\);/';
self::assertChartMatchesRegularExpression($chart, $regex, '');
self::assertChartMatchesRegularExpression($chart, $regex, Engine::NONE);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/Twig/TwigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,32 @@

namespace HighchartsBundle\Tests\Twig;

use HighchartsBundle\Highcharts\Engine;
use HighchartsBundle\Highcharts\Highchart;
use HighchartsBundle\Twig\HighchartsExtension;
use PHPUnit\Framework\TestCase;
use Twig\Error\SyntaxError;

/**
* This class hold Unit Tests for the Twig extension.
*/
class TwigTest extends TestCase
{
/**
* @throws SyntaxError
*/
public function testInvalidEngine(): void
{
$this->expectException(SyntaxError::class);
$chart = new Highchart();
$extension = new HighchartsExtension();
$extension->chart($chart, 'fake');
}

/**
* Chart rendering using the twig extension.
*
* @throws SyntaxError
*/
public function testTwigExtension(): void
{
Expand All @@ -36,13 +51,23 @@ public function testTwigExtension(): void
);

// render with jquery explicitly
$actual = $extension->chart($chart, Engine::JQUERY);
self::assertMatchesRegularExpression(
'/\$\(function\s?\(\)\s?\{\n?\r?\s*const chart = new Highcharts.Chart\(\{\n?\r?\s*\}\);\n?\r?\s*\}\);/',
$actual
);
$actual = $extension->chart($chart, 'jquery');
self::assertMatchesRegularExpression(
'/\$\(function\s?\(\)\s?\{\n?\r?\s*const chart = new Highcharts.Chart\(\{\n?\r?\s*\}\);\n?\r?\s*\}\);/',
$actual
);

// render with mootools
$actual = $extension->chart($chart, Engine::MOOTOOLS);
self::assertMatchesRegularExpression(
'/window.addEvent\(\'domready\', function\s?\(\)\s?\{\r?\n?\s*const chart = new Highcharts.Chart\(\{\n?\r?\s*\}\);\n?\r?\s*\}\);/',
$actual
);
$actual = $extension->chart($chart, 'mootools');
self::assertMatchesRegularExpression(
'/window.addEvent\(\'domready\', function\s?\(\)\s?\{\r?\n?\s*const chart = new Highcharts.Chart\(\{\n?\r?\s*\}\);\n?\r?\s*\}\);/',
Expand Down

0 comments on commit 343117e

Please sign in to comment.