From 04fda52a0b79e122919cf66ec4fd9937068be995 Mon Sep 17 00:00:00 2001 From: Kevin Hill Date: Mon, 13 Apr 2015 18:54:27 -0700 Subject: [PATCH] Added Lavacharts#exists() method to check for existing charts by type and label --- src/Lavacharts.php | 13 +++++++++++++ src/Volcano.php | 12 +++++++++--- tests/LavachartsTest.php | 41 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/Lavacharts.php b/src/Lavacharts.php index f6a35ff7..1f3be310 100755 --- a/src/Lavacharts.php +++ b/src/Lavacharts.php @@ -254,6 +254,19 @@ public function jsapi() return $this->jsFactory->getCoreJs(); } + /** + * Checks to see if the given chart type and title exists in the volcano storage. + * + * @access public + * @since v2.4.2 + * + * @return string + */ + public function exists($type, $label) + { + return $this->volcano->checkChart($type, $label); + } + /** * Builds a div html element for the chart to be rendered into. * diff --git a/src/Volcano.php b/src/Volcano.php index 00d708ea..729f9834 100755 --- a/src/Volcano.php +++ b/src/Volcano.php @@ -15,6 +15,7 @@ * @license http://opensource.org/licenses/MIT MIT */ +use Khill\Lavacharts\Utils; use Khill\Lavacharts\Charts\Chart; use Khill\Lavacharts\Exceptions\InvalidLabel; use Khill\Lavacharts\Exceptions\ChartNotFound; @@ -64,13 +65,18 @@ public function getChart($type, $label) * * @param string $type Type of chart to store. * @param string $label Identifying label of a chart to check. + * * @return bool */ public function checkChart($type, $label) { - if (array_key_exists($type, $this->charts)) { - if (array_key_exists($label, $this->charts[$type])) { - return true; + if (Utils::nonEmptyString($type) && Utils::nonEmptyString($label)) { + if (array_key_exists($type, $this->charts)) { + if (array_key_exists($label, $this->charts[$type])) { + return true; + } else { + return false; + } } else { return false; } diff --git a/tests/LavachartsTest.php b/tests/LavachartsTest.php index 737a57c2..e1ee9b3c 100755 --- a/tests/LavachartsTest.php +++ b/tests/LavachartsTest.php @@ -38,6 +38,47 @@ public function testCreateDataTableViaAliasWithTimezone() $this->assertInstanceOf('\Khill\Lavacharts\Configs\DataTable', $this->lava->DataTable('America/Los_Angeles')); } + public function testExistsWithExistingChartInVolcano() + { + $this->lava->LineChart('TestChart'); + + $this->assertTrue($this->lava->exists('LineChart', 'TestChart')); + } + + public function testExistsWithNonExistantChartTypeInVolcano() + { + $this->lava->LineChart('TestChart'); + + $this->assertFalse($this->lava->exists('SheepChart', 'TestChart')); + } + + public function testExistsWithNonExistantChartLabelInVolcano() + { + $this->lava->LineChart('WhaaaaatChart?'); + + $this->assertFalse($this->lava->exists('LineChart', 'TestChart')); + } + + /** + * @dataProvider nonStringProvider + */ + public function testExistsWithNonStringInputForType($badTypes) + { + $this->lava->LineChart('TestChart'); + + $this->assertFalse($this->lava->exists($badTypes, 'TestChart')); + } + + /** + * @dataProvider nonStringProvider + */ + public function testExistsWithNonStringInputForLabel($badTypes) + { + $this->lava->LineChart('TestChart'); + + $this->assertFalse($this->lava->exists('LineChart', $badTypes)); + } + /** * @dataProvider chartTypesProvider */