Skip to content

Commit

Permalink
Added Lavacharts#exists() method to check for existing charts by type…
Browse files Browse the repository at this point in the history
… and label
  • Loading branch information
kevinkhill committed Apr 14, 2015
1 parent fcdd189 commit 04fda52
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/Lavacharts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
12 changes: 9 additions & 3 deletions src/Volcano.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
41 changes: 41 additions & 0 deletions tests/LavachartsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 04fda52

Please sign in to comment.