diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2007-02-15 10:19:58 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2007-02-15 10:19:58 +0000 |
commit | 9873a43cd9fa983041ae97ea56d7a50fc569102e (patch) | |
tree | 1b167d65e68241d7a2ac4d35ffdec587e5233f2d /tests | |
parent | 43c70ac8906a9096bde1dd1905695e3c1cc7b522 (diff) | |
download | zetacomponents-graph-9873a43cd9fa983041ae97ea56d7a50fc569102e.zip zetacomponents-graph-9873a43cd9fa983041ae97ea56d7a50fc569102e.tar.gz |
- Implemented #9405 (PDO data set)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/dataset_pdo_test.php | 355 | ||||
-rw-r--r-- | tests/suite.php | 2 |
2 files changed, 357 insertions, 0 deletions
diff --git a/tests/dataset_pdo_test.php b/tests/dataset_pdo_test.php new file mode 100644 index 0000000..8c42012 --- /dev/null +++ b/tests/dataset_pdo_test.php @@ -0,0 +1,355 @@ +<?php +/** + * ezcGraphPdoDataSetTest + * + * @package Graph + * @version //autogen// + * @subpackage Tests + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +require_once dirname( __FILE__ ) . '/test_case.php'; + +/** + * Tests for ezcGraph class. + * + * @package ImageAnalysis + * @subpackage Tests + */ +class ezcGraphPdoDataSetTest extends ezcGraphTestCase +{ + protected $basePath; + + protected $tempDir; + + public static function suite() + { + return new PHPUnit_Framework_TestSuite( "ezcGraphPdoDataSetTest" ); + } + + protected function setUp() + { + static $i = 0; + $this->tempDir = $this->createTempDir( __CLASS__ . sprintf( '_%03d_', ++$i ) ) . '/'; + $this->basePath = dirname( __FILE__ ) . '/data/'; + + // Try to build up database connection + try + { + $db = ezcDbInstance::get(); + } + catch ( Exception $e ) + { + $this->markTestSkipped( 'Database connection required for PDO statement tests.' ); + } + + $this->q = new ezcQueryInsert( $db ); + try + { + $db->exec( 'DROP TABLE graph_pdo_test' ); + } + catch ( Exception $e ) {} // eat + + // Create test table + $db->exec( 'CREATE TABLE graph_pdo_test ( id INT, browser VARCHAR(255), hits INT )' ); + + // Insert some data + $db->exec( "INSERT INTO graph_pdo_test VALUES ( '', 'Firefox', 2567 )" ); + $db->exec( "INSERT INTO graph_pdo_test VALUES ( '', 'Opera', 543 )" ); + $db->exec( "INSERT INTO graph_pdo_test VALUES ( '', 'Safari', 23 )" ); + $db->exec( "INSERT INTO graph_pdo_test VALUES ( '', 'Konquror', 812 )" ); + $db->exec( "INSERT INTO graph_pdo_test VALUES ( '', 'Lynx', 431 )" ); + $db->exec( "INSERT INTO graph_pdo_test VALUES ( '', 'wget', 912 )" ); + } + + protected function tearDown() + { + if ( !$this->hasFailed() ) + { + $this->removeTempDir(); + } + + $db = ezcDbInstance::get(); + $db->exec( 'DROP TABLE graph_pdo_test' ); + } + + public function testAutomaticDataSetUsage() + { + $db = ezcDbInstance::get(); + + $statement = $db->prepare( 'SELECT browser, hits FROM graph_pdo_test' ); + $statement->execute(); + + $dataset = new ezcGraphPdoDataSet( $statement ); + + $dataSetArray = array( + 'Firefox' => 2567, + 'Opera' => 543, + 'Safari' => 23, + 'Konquror' => 812, + 'Lynx' => 431, + 'wget' => 912, + ); + + $count = 0; + foreach ( $dataset as $key => $value ) + { + list( $compareKey, $compareValue ) = each( $dataSetArray ); + + $this->assertEquals( + $compareKey, + $key, + 'Unexpected key for dataset value.' + ); + + $this->assertEquals( + $compareValue, + $value, + 'Unexpected value for dataset.' + ); + + ++$count; + } + + $this->assertEquals( + $count, + count( $dataSetArray ), + 'Too few datasets found.' + ); + } + + public function testAutomaticDataSetUsageSingleColumn() + { + $db = ezcDbInstance::get(); + + $statement = $db->prepare( 'SELECT hits FROM graph_pdo_test' ); + $statement->execute(); + + $dataset = new ezcGraphPdoDataSet( $statement ); + + $dataSetArray = array( + 'Firefox' => 2567, + 'Opera' => 543, + 'Safari' => 23, + 'Konquror' => 812, + 'Lynx' => 431, + 'wget' => 912, + ); + + $count = 0; + foreach ( $dataset as $key => $value ) + { + list( $compareKey, $compareValue ) = each( $dataSetArray ); + + $this->assertEquals( + $count, + $key, + 'Unexpected key for dataset value.' + ); + + $this->assertEquals( + $compareValue, + $value, + 'Unexpected value for dataset.' + ); + + ++$count; + } + + $this->assertEquals( + $count, + count( $dataSetArray ), + 'Too few datasets found.' + ); + } + + public function testAutomaticDataSetUsageTooManyRows() + { + $db = ezcDbInstance::get(); + + $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' ); + $statement->execute(); + + try + { + $dataset = new ezcGraphPdoDataSet( $statement ); + } + catch ( ezcGraphPdoDataSetTooManyColumnsException $e ) + { + return true; + } + + $this->fail( 'Expected ezcGraphPdoDataSetTooManyColumnsException.' ); + } + + public function testSpecifiedDataSetUsage() + { + $db = ezcDbInstance::get(); + + $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' ); + $statement->execute(); + + $dataset = new ezcGraphPdoDataSet( + $statement, + array( + ezcGraph::KEY => 'browser', + ezcGraph::VALUE => 'hits', + ) + ); + + $dataSetArray = array( + 'Firefox' => 2567, + 'Opera' => 543, + 'Safari' => 23, + 'Konquror' => 812, + 'Lynx' => 431, + 'wget' => 912, + ); + + $count = 0; + foreach ( $dataset as $key => $value ) + { + list( $compareKey, $compareValue ) = each( $dataSetArray ); + + $this->assertEquals( + $compareKey, + $key, + 'Unexpected key for dataset value.' + ); + + $this->assertEquals( + $compareValue, + $value, + 'Unexpected value for dataset.' + ); + + ++$count; + } + + $this->assertEquals( + $count, + count( $dataSetArray ), + 'Too few datasets found.' + ); + } + + public function testSpecifiedDataSetUsageSingleColumn() + { + $db = ezcDbInstance::get(); + + $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' ); + $statement->execute(); + + $dataset = new ezcGraphPdoDataSet( + $statement, + array( + ezcGraph::VALUE => 'hits', + ) + ); + + $dataSetArray = array( + 'Firefox' => 2567, + 'Opera' => 543, + 'Safari' => 23, + 'Konquror' => 812, + 'Lynx' => 431, + 'wget' => 912, + ); + + $count = 0; + foreach ( $dataset as $key => $value ) + { + list( $compareKey, $compareValue ) = each( $dataSetArray ); + + $this->assertEquals( + $count, + $key, + 'Unexpected key for dataset value.' + ); + + $this->assertEquals( + $compareValue, + $value, + 'Unexpected value for dataset.' + ); + + ++$count; + } + + $this->assertEquals( + $count, + count( $dataSetArray ), + 'Too few datasets found.' + ); + } + + public function testSpecifiedDataSetUsageBrokenKey() + { + $db = ezcDbInstance::get(); + + $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' ); + $statement->execute(); + + try + { + $dataset = new ezcGraphPdoDataSet( + $statement, + array( + ezcGraph::KEY => 'nonexistant', + ezcGraph::VALUE => 'hits', + ) + ); + } + catch ( ezcGraphPdoDataSetMissingColumnException $e ) + { + return true; + } + + $this->fail( 'Expected ezcGraphPdoDataSetMissingColumnException.' ); + } + + public function testSpecifiedDataSetUsageBrokenValue() + { + $db = ezcDbInstance::get(); + + $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' ); + $statement->execute(); + + try + { + $dataset = new ezcGraphPdoDataSet( + $statement, + array( + ezcGraph::VALUE => 'nonexistant', + ) + ); + } + catch ( ezcGraphPdoDataSetMissingColumnException $e ) + { + return true; + } + + $this->fail( 'Expected ezcGraphPdoDataSetMissingColumnException.' ); + } + + public function testNonExceutedQuery() + { + $db = ezcDbInstance::get(); + + $statement = $db->prepare( 'SELECT browser, hits FROM graph_pdo_test' ); + + try + { + $dataset = new ezcGraphPdoDataSet( $statement ); + } + catch ( ezcGraphPdoDataSetStatementNotExecutedException $e ) + { + return true; + } + + $this->fail( 'Expected ezcGraphPdoDataSetStatementNotExecutedException.' ); + } +} + +?> diff --git a/tests/suite.php b/tests/suite.php index 6c75338..92ae202 100644 --- a/tests/suite.php +++ b/tests/suite.php @@ -19,6 +19,7 @@ require_once 'line_test.php'; require_once 'dataset_test.php'; require_once 'dataset_average_test.php'; require_once 'dataset_numeric_test.php'; +require_once 'dataset_pdo_test.php'; require_once 'element_options_test.php'; require_once 'legend_test.php'; require_once 'background_test.php'; @@ -64,6 +65,7 @@ class ezcGraphSuite extends PHPUnit_Framework_TestSuite $this->addTest( ezcGraphDataSetTest::suite() ); $this->addTest( ezcGraphDataSetAverageTest::suite() ); $this->addTest( ezcGraphNumericDataSetTest::suite() ); + $this->addTest( ezcGraphPdoDataSetTest::suite() ); $this->addTest( ezcGraphElementOptionsTest::suite() ); $this->addTest( ezcGraphLegendTest::suite() ); $this->addTest( ezcGraphBackgroundTest::suite() ); |