Skip to content
Browse files

Added unit tests.

  • Loading branch information...
1 parent 3fe6c08 commit c49c378861c7d7755a64bbd04116d5ef9322f34d John Mertic committed
Showing with 9,613 additions and 0 deletions.
  1. +41 −0 tests/ModuleInstall/Bug41829Test.php
  2. +80 −0 tests/ModuleInstall/ModuleScannerTest.php
  3. +101 −0 tests/ModuleInstall/PackageManager/Bug39980Test.php
  4. +206 −0 tests/PHPUnit/Extensions/Database/AbstractTester.php
  5. +132 −0 tests/PHPUnit/Extensions/Database/Constraint/DataSetIsEqual.php
  6. +133 −0 tests/PHPUnit/Extensions/Database/Constraint/TableIsEqual.php
  7. +174 −0 tests/PHPUnit/Extensions/Database/DB/DataSet.php
  8. +232 −0 tests/PHPUnit/Extensions/Database/DB/DefaultDatabaseConnection.php
  9. +95 −0 tests/PHPUnit/Extensions/Database/DB/FilteredDataSet.php
  10. +145 −0 tests/PHPUnit/Extensions/Database/DB/IDatabaseConnection.php
  11. +114 −0 tests/PHPUnit/Extensions/Database/DB/IMetaData.php
  12. +215 −0 tests/PHPUnit/Extensions/Database/DB/MetaData.php
  13. +184 −0 tests/PHPUnit/Extensions/Database/DB/MetaData/InformationSchema.php
  14. +137 −0 tests/PHPUnit/Extensions/Database/DB/MetaData/MySQL.php
  15. +164 −0 tests/PHPUnit/Extensions/Database/DB/MetaData/Oci.php
  16. +164 −0 tests/PHPUnit/Extensions/Database/DB/MetaData/PgSQL.php
  17. +152 −0 tests/PHPUnit/Extensions/Database/DB/MetaData/Sqlite.php
  18. +92 −0 tests/PHPUnit/Extensions/Database/DB/ResultSetTable.php
  19. +84 −0 tests/PHPUnit/Extensions/Database/DB/Table.php
  20. +176 −0 tests/PHPUnit/Extensions/Database/DB/TableIterator.php
  21. +75 −0 tests/PHPUnit/Extensions/Database/DB/TableMetaData.php
  22. +178 −0 tests/PHPUnit/Extensions/Database/DataSet/AbstractDataSet.php
  23. +218 −0 tests/PHPUnit/Extensions/Database/DataSet/AbstractTable.php
  24. +136 −0 tests/PHPUnit/Extensions/Database/DataSet/AbstractTableMetaData.php
  25. +150 −0 tests/PHPUnit/Extensions/Database/DataSet/AbstractXmlDataSet.php
  26. +130 −0 tests/PHPUnit/Extensions/Database/DataSet/CompositeDataSet.php
  27. +158 −0 tests/PHPUnit/Extensions/Database/DataSet/CsvDataSet.php
  28. +144 −0 tests/PHPUnit/Extensions/Database/DataSet/DataSetFilter.php
  29. +109 −0 tests/PHPUnit/Extensions/Database/DataSet/DefaultDataSet.php
  30. +124 −0 tests/PHPUnit/Extensions/Database/DataSet/DefaultTable.php
  31. +173 −0 tests/PHPUnit/Extensions/Database/DataSet/DefaultTableIterator.php
  32. +91 −0 tests/PHPUnit/Extensions/Database/DataSet/DefaultTableMetaData.php
  33. +111 −0 tests/PHPUnit/Extensions/Database/DataSet/FlatXmlDataSet.php
  34. +104 −0 tests/PHPUnit/Extensions/Database/DataSet/IDataSet.php
  35. +77 −0 tests/PHPUnit/Extensions/Database/DataSet/IPersistable.php
  36. +104 −0 tests/PHPUnit/Extensions/Database/DataSet/ITable.php
  37. +81 −0 tests/PHPUnit/Extensions/Database/DataSet/ITableIterator.php
  38. +95 −0 tests/PHPUnit/Extensions/Database/DataSet/ITableMetaData.php
  39. +137 −0 tests/PHPUnit/Extensions/Database/DataSet/Persistors/Abstract.php
  40. +157 −0 tests/PHPUnit/Extensions/Database/DataSet/Persistors/FlatXml.php
  41. +161 −0 tests/PHPUnit/Extensions/Database/DataSet/Persistors/Xml.php
  42. +142 −0 tests/PHPUnit/Extensions/Database/DataSet/QueryDataSet.php
  43. +172 −0 tests/PHPUnit/Extensions/Database/DataSet/QueryTable.php
  44. +140 −0 tests/PHPUnit/Extensions/Database/DataSet/ReplacementDataSet.php
  45. +250 −0 tests/PHPUnit/Extensions/Database/DataSet/ReplacementTable.php
  46. +197 −0 tests/PHPUnit/Extensions/Database/DataSet/ReplacementTableIterator.php
  47. +107 −0 tests/PHPUnit/Extensions/Database/DataSet/TableFilter.php
  48. +125 −0 tests/PHPUnit/Extensions/Database/DataSet/TableMetaDataFilter.php
  49. +141 −0 tests/PHPUnit/Extensions/Database/DataSet/XmlDataSet.php
  50. +95 −0 tests/PHPUnit/Extensions/Database/DefaultTester.php
  51. +128 −0 tests/PHPUnit/Extensions/Database/ITester.php
  52. +106 −0 tests/PHPUnit/Extensions/Database/Operation/Composite.php
  53. +98 −0 tests/PHPUnit/Extensions/Database/Operation/Delete.php
  54. +87 −0 tests/PHPUnit/Extensions/Database/Operation/DeleteAll.php
  55. +137 −0 tests/PHPUnit/Extensions/Database/Operation/Exception.php
  56. +157 −0 tests/PHPUnit/Extensions/Database/Operation/Factory.php
  57. +78 −0 tests/PHPUnit/Extensions/Database/Operation/IDatabaseOperation.php
  58. +108 −0 tests/PHPUnit/Extensions/Database/Operation/Insert.php
  59. +74 −0 tests/PHPUnit/Extensions/Database/Operation/Null.php
  60. +148 −0 tests/PHPUnit/Extensions/Database/Operation/Replace.php
  61. +125 −0 tests/PHPUnit/Extensions/Database/Operation/RowBased.php
  62. +96 −0 tests/PHPUnit/Extensions/Database/Operation/Truncate.php
  63. +103 −0 tests/PHPUnit/Extensions/Database/Operation/Update.php
  64. +254 −0 tests/PHPUnit/Extensions/Database/TestCase.php
  65. +110 −0 tests/PHPUnit/Extensions/GroupTestSuite.php
  66. +195 −0 tests/PHPUnit/Extensions/OutputTestCase.php
  67. +118 −0 tests/PHPUnit/Extensions/PerformanceTestCase.php
  68. +271 −0 tests/PHPUnit/Extensions/PhptTestCase.php
  69. +69 −0 tests/PHPUnit/Extensions/PhptTestCase/Logger.php
  70. +95 −0 tests/PHPUnit/Extensions/PhptTestSuite.php
  71. +153 −0 tests/PHPUnit/Extensions/RepeatedTest.php
Sorry, we could not display the entire diff because too many files (697) changed.
View
41 tests/ModuleInstall/Bug41829Test.php
@@ -0,0 +1,41 @@
+<?php
+
+require_once('ModuleInstall/ModuleInstaller.php');
+
+class Bug41829Test extends Sugar_PHPUnit_Framework_TestCase
+{
+ protected $module_installer;
+ protected $log;
+
+ public function setUp()
+ {
+ $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
+ $this->module_installer = new ModuleInstaller();
+ $this->module_installer->silent = true;
+ $this->module_installer->base_dir = '';
+ $this->module_installer->id_name = 'Bug41829Test';
+ $this->module_installer->installdefs['dcaction'] = array(
+ array(
+ 'from' => '<basepath>/dcaction_file.php',
+ ),
+ );
+ $this->log = $GLOBALS['log'];
+ $GLOBALS['log'] = new SugarMockLogger();
+ }
+
+ public function tearDown()
+ {
+ SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
+ unset($GLOBALS['current_user']);
+ $GLOBALS['log'] = $this->log;
+ }
+
+ public function testWarningOnUninstallDCActions()
+ {
+ $this->module_installer->uninstall_dcactions();
+
+ $this->assertTrue(in_array('DEBUG: Uninstalling DCActions ...' . str_replace('<basepath>', $this->module_installer->base_dir, $this->module_installer->installdefs['dcaction'][0]['from']), $GLOBALS['log']->messages));
+ }
+
+
+}
View
80 tests/ModuleInstall/ModuleScannerTest.php
@@ -0,0 +1,80 @@
+<?php
+require_once 'ModuleInstall/ModuleScanner.php';
+
+class ModuleScannerTest extends Sugar_PHPUnit_Framework_TestCase
+{
+ var $fileLoc;
+
+ public function setUp()
+ {
+ $this->fileLoc = "cache/moduleScannerTemp.php";
+ }
+
+ public function tearDown()
+ {
+ if (is_file($this->fileLoc))
+ unlink($this->fileLoc);
+ }
+
+ public function testFileTemplatePass()
+ {
+
+ $fileModContents = <<<EOQ
+<?PHP
+require_once('include/SugarObjects/templates/file/File.php');
+
+class testFile_sugar extends File {
+ function fileT_testFiles_sugar(){
+ parent::File();
+ \$this->file = new File();
+ \$file = "file";
+ }
+}
+?>
+EOQ;
+ file_put_contents($this->fileLoc, $fileModContents);
+ $ms = new ModuleScanner();
+ $errors = $ms->scanFile($this->fileLoc);
+ $this->assertTrue(empty($errors));
+ }
+
+ public function testFileFunctionFail()
+ {
+
+ $fileModContents = <<<EOQ
+<?PHP
+require_once('include/SugarObjects/templates/file/File.php');
+
+class testFile_sugar extends File {
+ function fileT_testFiles_sugar(){
+ parent::File();
+ \$this->file = new File();
+ \$file = file('test.php');
+
+ }
+}
+?>
+EOQ;
+ file_put_contents($this->fileLoc, $fileModContents);
+ $ms = new ModuleScanner();
+ $errors = $ms->scanFile($this->fileLoc);
+ $this->assertTrue(!empty($errors));
+ }
+
+ public function testCallUserFunctionFail()
+ {
+
+ $fileModContents = <<<EOQ
+<?PHP
+ call_user_func("sugar_file_put_contents", "test2.php", "test");
+?>
+EOQ;
+ file_put_contents($this->fileLoc, $fileModContents);
+ $ms = new ModuleScanner();
+ $errors = $ms->scanFile($this->fileLoc);
+ $this->assertTrue(!empty($errors));
+ }
+
+
+
+}
View
101 tests/ModuleInstall/PackageManager/Bug39980Test.php
@@ -0,0 +1,101 @@
+<?php
+require_once 'ModuleInstall/PackageManager/PackageManager.php';
+
+class Bug39980Test extends Sugar_PHPUnit_Framework_TestCase
+{
+
+ public function tearDown()
+ {
+ if (is_file(Bug39980PackageManger::$manifest_location))
+ unlink(Bug39980PackageManger::$manifest_location);
+ }
+
+ public function testGetinstalledPackagesUninstallable()
+ {
+ $pm = new Bug39980PackageManger();
+ $pm->extractManifest(0, 0);
+ $packs = $pm->getinstalledPackages();
+ //Its confusing, but "UNINSTALLABLE" in file_install means the package is NOT uninstallable
+ $this->assertEquals("UNINSTALLABLE", $packs[0]['file_install']);
+ }
+
+}
+
+class Bug39980PackageManger extends PackageManager {
+ static $manifest_location = "cache/Bug39980manifest.php";
+
+ public function __construct() {
+ parent::__construct();
+ $this->manifest_content = <<<EOQ
+<?php
+\$manifest = array (
+ 'acceptable_sugar_versions' =>
+ array (
+ '6.1.0'
+ ),
+ 'acceptable_sugar_flavors' =>
+ array(
+ 'ENT'
+ ),
+ 'readme'=>'',
+ 'key'=>'tf1',
+ 'author' => '',
+ 'description' => '',
+ 'icon' => '',
+ 'is_uninstallable' => false,
+ 'name' => 'test_file_1',
+ 'published_date' => '2010-10-20 22:10:01',
+ 'type' => 'module',
+ 'version' => '1287612601',
+ 'remove_tables' => 'prompt',
+ );
+\$installdefs = array (
+ 'id' => 'asdfqq',
+ 'copy' =>
+ array (
+ 0 => array (
+ 'from' => '<basepath>/Extension/modules/Cases/Ext/Vardefs/dummy_extension2.php',
+ 'to' => 'custom/Extension/modules/Cases/Ext/Vardefs/dummy_extension2.php',
+ ),
+ ),
+);
+
+EOQ;
+ }
+
+ public function getInstalled($types)
+ {
+ include($this->extractManifest(0,0));
+ $sm = array(
+ 'manifest' => (isset($manifest) ? $manifest : ''),
+ 'installdefs' => (isset($installdefs) ? $installdefs : ''),
+ 'upgrade_manifest' => (isset($upgrade_manifest) ? $upgrade_manifest : '')
+ );
+ return array (
+ (object) array(
+ 'filename' => Bug39980PackageManger::$manifest_location,
+ 'manifest' => base64_encode(serialize($sm)),
+ 'date_entered' => '1/1/2010',
+ 'new_schema' => '1',
+ 'module_dir' => 'Administration' ,
+ 'id' => 'b4d22740-4e96-65b3-b712-4ca230d95987' ,
+ 'md5sum' => 'fe221d731d8c624f15712878300aa907' ,
+ 'type' => 'module' ,
+ 'version' => '1285697780' ,
+ 'status' => 'installed' ,
+ 'name' => 'test_file_1' ,
+ 'description' => '' ,
+ 'id_name' => 'tf1' ,
+ 'enabled' => true ,
+ )
+ );
+ }
+
+ public function extractManifest($filename, $base_tmp_upgrade_dir)
+ {
+ if (!is_file(Bug39980PackageManger::$manifest_location))
+ file_put_contents(Bug39980PackageManger::$manifest_location, $this->manifest_content);
+
+ return Bug39980PackageManger::$manifest_location;
+ }
+}
View
206 tests/PHPUnit/Extensions/Database/AbstractTester.php
@@ -0,0 +1,206 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.2.0
+ */
+
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/Util/Filter.php';
+
+require_once 'PHPUnit/Extensions/Database/ITester.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ * Can be used as a foundation for new DatabaseTesters.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2009 Mike Lively <m@digitalsandwich.com>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: 3.3.17
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.2.0
+ */
+abstract class PHPUnit_Extensions_Database_AbstractTester implements PHPUnit_Extensions_Database_ITester
+{
+
+ /**
+ * @var PHPUnit_Extensions_Database_Operation_IDatabaseOperation
+ */
+ protected $setUpOperation;
+
+ /**
+ * @var PHPUnit_Extensions_Database_Operation_IDatabaseOperation
+ */
+ protected $tearDownOperation;
+
+ /**
+ * @var PHPUnit_Extensions_Database_DataSet_IDataSet
+ */
+ protected $dataSet;
+
+ /**
+ * @var string
+ */
+ protected $schema;
+
+ /**
+ * Creates a new database tester.
+ *
+ * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $databaseConnection
+ */
+ public function __construct()
+ {
+ $this->setUpOperation = PHPUnit_Extensions_Database_Operation_Factory::CLEAN_INSERT();
+ $this->tearDownOperation = PHPUnit_Extensions_Database_Operation_Factory::NONE();
+ }
+
+ /**
+ * Closes the specified connection.
+ *
+ * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
+ */
+ public function closeConnection(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection)
+ {
+ $connection->close();
+ }
+
+ /**
+ * Returns the test dataset.
+ *
+ * @return PHPUnit_Extensions_Database_DataSet_IDataSet
+ */
+ public function getDataSet()
+ {
+ return $this->dataSet;
+ }
+
+ /**
+ * TestCases must call this method inside setUp().
+ */
+ public function onSetUp()
+ {
+ $this->getSetUpOperation()->execute($this->getConnection(), $this->getDataSet());
+ }
+
+ /**
+ * TestCases must call this method inside tearDown().
+ */
+ public function onTearDown()
+ {
+ $this->getTearDownOperation()->execute($this->getConnection(), $this->getDataSet());
+ }
+
+ /**
+ * Sets the test dataset to use.
+ *
+ * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
+ */
+ public function setDataSet(PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
+ {
+ $this->dataSet = $dataSet;
+ }
+
+ /**
+ * Sets the schema value.
+ *
+ * @param string $schema
+ */
+ public function setSchema($schema)
+ {
+ $this->schema = $schema;
+ }
+
+ /**
+ * Sets the DatabaseOperation to call when starting the test.
+ *
+ * @param PHPUnit_Extensions_Database_Operation_DatabaseOperation $setUpOperation
+ */
+ public function setSetUpOperation(PHPUnit_Extensions_Database_Operation_IDatabaseOperation $setUpOperation)
+ {
+ $this->setUpOperation = $setUpOperation;
+ }
+
+ /**
+ * Sets the DatabaseOperation to call when ending the test.
+ *
+ * @param PHPUnit_Extensions_Database_Operation_DatabaseOperation $tearDownOperation
+ */
+ public function setTearDownOperation(PHPUnit_Extensions_Database_Operation_IDatabaseOperation $tearDownOperation)
+ {
+ $this->tearDownOperation = $tearDownOperation;
+ }
+
+ /**
+ * Returns the schema value
+ *
+ * @return string
+ */
+ protected function getSchema()
+ {
+ return $this->schema;
+ }
+
+ /**
+ * Returns the database operation that will be called when starting the test.
+ *
+ * @return PHPUnit_Extensions_Database_Operation_DatabaseOperation
+ */
+ protected function getSetUpOperation()
+ {
+ return $this->setUpOperation;
+ }
+
+ /**
+ * Returns the database operation that will be called when ending the test.
+ *
+ * @return PHPUnit_Extensions_Database_Operation_DatabaseOperation
+ */
+ protected function getTearDownOperation()
+ {
+ return $this->tearDownOperation;
+ }
+}
+?>
View
132 tests/PHPUnit/Extensions/Database/Constraint/DataSetIsEqual.php
@@ -0,0 +1,132 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.2.0
+ */
+
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/Util/Filter.php';
+require_once 'PHPUnit/Framework/Constraint.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ * Asserts whether or not two dbunit datasets are equal.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2009 Mike Lively <m@digitalsandwich.com>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: 3.3.17
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.2.0
+ */
+class PHPUnit_Extensions_Database_Constraint_DataSetIsEqual extends PHPUnit_Framework_Constraint
+{
+
+ /**
+ * @var PHPUnit_Extensions_Database_DataSet_IDataSet
+ */
+ protected $value;
+
+ /**
+ * @var string
+ */
+ protected $failure_reason;
+
+ /**
+ * Creates a new constraint.
+ *
+ * @param PHPUnit_Extensions_Database_DataSet_IDataSet $value
+ */
+ public function __construct(PHPUnit_Extensions_Database_DataSet_IDataSet $value)
+ {
+ $this->value = $value;
+ }
+
+ /**
+ * Determines whether or not the given dataset matches the dataset used to
+ * create this constraint.
+ *
+ * @param PHPUnit_Extensions_Database_DataSet_IDataSet $other
+ * @return bool
+ */
+ public function evaluate($other)
+ {
+ if ($other instanceof PHPUnit_Extensions_Database_DataSet_IDataSet) {
+ try {
+ $this->value->assertEquals($other);
+ return TRUE;
+ } catch (Exception $e) {
+ $this->failure_reason = $e->getMessage();
+ return FALSE;
+ }
+ } else {
+ throw new InvalidArgumentException("PHPUnit_Extensions_Database_DataSet_IDataSet expected");
+ }
+ }
+
+ protected function customFailureDescription($other, $description, $not)
+ {
+ return sprintf(
+ 'Failed asserting that actual %s %s Reason: %s',
+
+ $other->__toString(),
+ $this->toString(),
+ $this->failure_reason
+ );
+ }
+
+ /**
+ * Returns a string representation of the constraint.
+ *
+ * @return string
+ */
+ public function toString()
+ {
+ return sprintf('is equal to expected %s',
+ $this->value->__toString());
+ }
+}
+?>
View
133 tests/PHPUnit/Extensions/Database/Constraint/TableIsEqual.php
@@ -0,0 +1,133 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.2.0
+ */
+
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/Util/Filter.php';
+require_once 'PHPUnit/Framework/Constraint.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ * Asserts whether or not two dbunit tables are equal.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2009 Mike Lively <m@digitalsandwich.com>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: 3.3.17
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.2.0
+ */
+class PHPUnit_Extensions_Database_Constraint_TableIsEqual extends PHPUnit_Framework_Constraint
+{
+
+ /**
+ * @var PHPUnit_Extensions_Database_DataSet_ITable
+ */
+ protected $value;
+
+ /**
+ * @var string
+ */
+ protected $failure_reason;
+
+ /**
+ * Creates a new constraint.
+ *
+ * @param PHPUnit_Extensions_Database_DataSet_ITable $value
+ */
+ public function __construct(PHPUnit_Extensions_Database_DataSet_ITable $value)
+ {
+ $this->value = $value;
+ }
+
+ /**
+ * Determines whether or not the given table matches the table used to
+ * create this constraint.
+ *
+ * @param PHPUnit_Extensions_Database_DataSet_ITable $other
+ * @return bool
+ */
+ public function evaluate($other)
+ {
+ if ($other instanceof PHPUnit_Extensions_Database_DataSet_ITable) {
+ try {
+ $this->value->assertEquals($other);
+ return TRUE;
+ } catch (Exception $e) {
+ $this->failure_reason = $e->getMessage();
+ return FALSE;
+ }
+ } else {
+ throw new InvalidArgumentException("PHPUnit_Extensions_Database_DataSet_ITable expected");
+ }
+ }
+
+ protected function customFailureDescription($other, $description, $not)
+ {
+ return sprintf(
+ 'Failed asserting that actual %s %s Reason: %s',
+
+ $other->__toString(),
+ $this->toString(),
+ $this->failure_reason
+ );
+ }
+
+ /**
+ * Returns a string representation of the constraint.
+ *
+ * @return string
+ */
+ public function toString()
+ {
+ return sprintf('is equal to expected %s',
+
+ PHPUnit_Util_Type::toString($this->value));
+ }
+}
+?>
View
174 tests/PHPUnit/Extensions/Database/DB/DataSet.php
@@ -0,0 +1,174 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.2.0
+ */
+
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/Util/Filter.php';
+
+require_once 'PHPUnit/Extensions/Database/DataSet/AbstractDataSet.php';
+require_once 'PHPUnit/Extensions/Database/DataSet/DefaultTableMetaData.php';
+require_once 'PHPUnit/Extensions/Database/DB/TableIterator.php';
+require_once 'PHPUnit/Extensions/Database/DB/Table.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ * Provides access to a database instance as a data set.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2009 Mike Lively <m@digitalsandwich.com>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: 3.3.17
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.2.0
+ */
+class PHPUnit_Extensions_Database_DB_DataSet extends PHPUnit_Extensions_Database_DataSet_AbstractDataSet
+{
+
+ /**
+ * An array of ITable objects.
+ *
+ * @var array
+ */
+ protected $tables = array();
+
+ /**
+ * The database connection this dataset is using.
+ *
+ * @var PHPUnit_Extensions_Database_DB_IDatabaseConnection
+ */
+ protected $databaseConnection;
+
+ /**
+ * Creates a new dataset using the given database connection.
+ *
+ * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $databaseConnection
+ */
+ public function __construct(PHPUnit_Extensions_Database_DB_IDatabaseConnection $databaseConnection)
+ {
+ $this->databaseConnection = $databaseConnection;
+ }
+
+ /**
+ * Creates the query necessary to pull all of the data from a table.
+ *
+ * @param PHPUnit_Extensions_Database_DataSet_ITableMetaData $tableMetaData
+ * @return unknown
+ */
+ public static function buildTableSelect(PHPUnit_Extensions_Database_DataSet_ITableMetaData $tableMetaData)
+ {
+ if ($tableMetaData->getTableName() == '') {
+ $e = new Exception("Empty Table Name");
+ echo $e->getTraceAsString();
+ throw $e;
+ }
+
+ $columnList = implode(', ', $tableMetaData->getColumns());
+
+ $primaryKeys = $tableMetaData->getPrimaryKeys();
+ if (count($primaryKeys)) {
+ $orderBy = 'ORDER BY ' . implode(' ASC, ', $primaryKeys) . ' ASC';
+ } else {
+ $orderBy = '';
+ }
+
+ return "SELECT {$columnList} FROM {$tableMetaData->getTableName()} {$orderBy}";
+ }
+
+ /**
+ * Creates an iterator over the tables in the data set. If $reverse is
+ * true a reverse iterator will be returned.
+ *
+ * @param bool $reverse
+ * @return PHPUnit_Extensions_Database_DB_TableIterator
+ */
+ protected function createIterator($reverse = FALSE)
+ {
+ return new PHPUnit_Extensions_Database_DB_TableIterator($this->getTableNames(), $this, $reverse);
+ }
+
+ /**
+ * Returns a table object for the given table.
+ *
+ * @param string $tableName
+ * @return PHPUnit_Extensions_Database_DB_Table
+ */
+ public function getTable($tableName)
+ {
+ if (!in_array($tableName, $this->getTableNames())) {
+ throw new InvalidArgumentException("$tableName is not a table in the current database.");
+ }
+
+ if (empty($this->tables[$tableName])) {
+ $this->tables[$tableName] = new PHPUnit_Extensions_Database_DB_Table($this->getTableMetaData($tableName), $this->databaseConnection);
+ }
+
+ return $this->tables[$tableName];
+ }
+
+ /**
+ * Returns a table meta data object for the given table.
+ *
+ * @param string $tableName
+ * @return PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData
+ */
+ public function getTableMetaData($tableName)
+ {
+ return new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData($tableName, $this->databaseConnection->getMetaData()->getTableColumns($tableName), $this->databaseConnection->getMetaData()->getTablePrimaryKeys($tableName));
+ }
+
+ /**
+ * Returns a list of table names for the database
+ *
+ * @return Array
+ */
+ public function getTableNames()
+ {
+ return $this->databaseConnection->getMetaData()->getTableNames();
+ }
+}
+?>
View
232 tests/PHPUnit/Extensions/Database/DB/DefaultDatabaseConnection.php
@@ -0,0 +1,232 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.2.0
+ */
+
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/Util/Filter.php';
+
+require_once 'PHPUnit/Extensions/Database/DataSet/QueryTable.php';
+require_once 'PHPUnit/Extensions/Database/DB/IDatabaseConnection.php';
+require_once 'PHPUnit/Extensions/Database/DB/MetaData.php';
+require_once 'PHPUnit/Extensions/Database/DB/ResultSetTable.php';
+require_once 'PHPUnit/Extensions/Database/DB/DataSet.php';
+require_once 'PHPUnit/Extensions/Database/DB/FilteredDataSet.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ * Provides a basic interface for communicating with a database.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2009 Mike Lively <m@digitalsandwich.com>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: 3.3.17
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.2.0
+ */
+class PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection implements PHPUnit_Extensions_Database_DB_IDatabaseConnection
+{
+ /**
+ * @var PDO
+ */
+ protected $connection;
+
+ /**
+ * @var string
+ */
+ protected $schema;
+
+ /**
+ * The metadata object used to retrieve table meta data from the database.
+ *
+ * @var PHPUnit_Extensions_Database_DB_IMetaData
+ */
+ protected $metaData;
+
+ /**
+ * Creates a new database connection
+ *
+ * @param PDO $connection
+ * @param string $schema - The name of the database schema you will be testing against.
+ */
+ public function __construct(PDO $connection, $schema)
+ {
+ $this->connection = $connection;
+ $this->metaData = PHPUnit_Extensions_Database_DB_MetaData::createMetaData($connection, $schema);
+ $this->schema = $schema;
+
+ $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ }
+
+ /**
+ * Close this connection.
+ */
+ public function close()
+ {
+ unset($this->connection);
+ }
+
+ /**
+ * Returns a database metadata object that can be used to retrieve table
+ * meta data from the database.
+ *
+ * @return PHPUnit_Extensions_Database_DB_IMetaData
+ */
+ public function getMetaData()
+ {
+ return $this->metaData;
+ }
+
+ /**
+ * Returns the schema for the connection.
+ *
+ * @return string
+ */
+ public function getSchema()
+ {
+ return $this->schema;
+ }
+
+ /**
+ * Creates a dataset containing the specified table names. If no table
+ * names are specified then it will created a dataset over the entire
+ * database.
+ *
+ * @param array $tableNames
+ * @return PHPUnit_Extensions_Database_DataSet_IDataSet
+ * @todo Implement the filtered data set.
+ */
+ public function createDataSet(Array $tableNames = NULL)
+ {
+ if (empty($tableNames)) {
+ return new PHPUnit_Extensions_Database_DB_DataSet($this);
+ } else {
+ return new PHPUnit_Extensions_Database_DB_FilteredDataSet($this, $tableNames);
+ }
+ }
+
+ /**
+ * Creates a table with the result of the specified SQL statement.
+ *
+ * @param string $resultName
+ * @param string $sql
+ * @return PHPUnit_Extensions_Database_DB_Table
+ */
+ public function createQueryTable($resultName, $sql)
+ {
+ return new PHPUnit_Extensions_Database_DataSet_QueryTable($resultName, $sql, $this);
+ }
+
+ /**
+ * Returns this connection database configuration
+ *
+ * @return PHPUnit_Extensions_Database_Database_DatabaseConfig
+ */
+ public function getConfig()
+ {
+
+ }
+
+ /**
+ * Returns a PDO Connection
+ *
+ * @return PDO
+ */
+ public function getConnection()
+ {
+ return $this->connection;
+ }
+
+ /**
+ * Returns the number of rows in the given table. You can specify an
+ * optional where clause to return a subset of the table.
+ *
+ * @param string $tableName
+ * @param string $whereClause
+ * @param int
+ */
+ public function getRowCount($tableName, $whereClause = NULL)
+ {
+ $query = "SELECT COUNT(*) FROM {$tableName}";
+
+ if (isset($whereClause)) {
+ $query .= " WHERE {$whereClause}";
+ }
+ }
+
+ /**
+ * Returns a quoted schema object. (table name, column name, etc)
+ *
+ * @param string $object
+ * @return string
+ */
+ public function quoteSchemaObject($object)
+ {
+ return $this->getMetaData()->quoteSchemaObject($object);
+ }
+
+ /**
+ * Returns the command used to truncate a table.
+ *
+ * @return string
+ */
+ public function getTruncateCommand()
+ {
+ return $this->getMetaData()->getTruncateCommand();
+ }
+
+ /**
+ * Returns true if the connection allows cascading
+ *
+ * @return bool
+ */
+ public function allowsCascading()
+ {
+ return $this->getMetaData()->allowsCascading();
+ }
+}
+?>
View
95 tests/PHPUnit/Extensions/Database/DB/FilteredDataSet.php
@@ -0,0 +1,95 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.2.0
+ */
+
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/Util/Filter.php';
+
+require_once 'PHPUnit/Extensions/Database/DB/DataSet.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ * Provides access to a database instance as a data set.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2009 Mike Lively <m@digitalsandwich.com>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: 3.3.17
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.2.0
+ */
+class PHPUnit_Extensions_Database_DB_FilteredDataSet extends PHPUnit_Extensions_Database_DB_DataSet
+{
+
+ /**
+ * @var Array
+ */
+ protected $tableNames;
+
+ /**
+ * Creates a new dataset using the given database connection.
+ *
+ * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $databaseConnection
+ */
+ public function __construct(PHPUnit_Extensions_Database_DB_IDatabaseConnection $databaseConnection, Array $tableNames)
+ {
+ parent::__construct($databaseConnection);
+ $this->tableNames = $tableNames;
+ }
+
+ /**
+ * Returns a list of table names for the database
+ *
+ * @return Array
+ */
+ public function getTableNames()
+ {
+ return $this->tableNames;
+ }
+}
+?>
View
145 tests/PHPUnit/Extensions/Database/DB/IDatabaseConnection.php
@@ -0,0 +1,145 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.2.0
+ */
+
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/Util/Filter.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ * Provides a basic interface for communicating with a database.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2009 Mike Lively <m@digitalsandwich.com>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: 3.3.17
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.2.0
+ */
+interface PHPUnit_Extensions_Database_DB_IDatabaseConnection
+{
+
+ /**
+ * Close this connection.
+ */
+ public function close();
+
+ /**
+ * Creates a dataset containing the specified table names. If no table
+ * names are specified then it will created a dataset over the entire
+ * database.
+ *
+ * @param array $tableNames
+ * @return PHPUnit_Extensions_Database_DataSet_IDataSet
+ */
+ public function createDataSet(Array $tableNames = NULL);
+
+ /**
+ * Creates a table with the result of the specified SQL statement.
+ *
+ * @param string $resultName
+ * @param string $sql
+ * @return PHPUnit_Extensions_Database_DataSet_ITable
+ */
+ public function createQueryTable($resultName, $sql);
+
+ /**
+ * Returns a PDO Connection
+ *
+ * @return PDO
+ */
+ public function getConnection();
+
+ /**
+ * Returns a database metadata object that can be used to retrieve table
+ * meta data from the database.
+ *
+ * @return PHPUnit_Extensions_Database_DB_IMetaData
+ */
+ public function getMetaData();
+
+ /**
+ * Returns the number of rows in the given table. You can specify an
+ * optional where clause to return a subset of the table.
+ *
+ * @param string $tableName
+ * @param string $whereClause
+ * @param int
+ */
+ public function getRowCount($tableName, $whereClause = NULL);
+
+ /**
+ * Returns the schema for the connection.
+ *
+ * @return string
+ */
+ public function getSchema();
+
+ /**
+ * Returns a quoted schema object. (table name, column name, etc)
+ *
+ * @param string $object
+ * @return string
+ */
+ public function quoteSchemaObject($object);
+
+ /**
+ * Returns the command used to truncate a table.
+ *
+ * @return string
+ */
+ public function getTruncateCommand();
+
+ /**
+ * Returns true if the connection allows cascading
+ *
+ * @return bool
+ */
+ public function allowsCascading();
+}
+?>
View
114 tests/PHPUnit/Extensions/Database/DB/IMetaData.php
@@ -0,0 +1,114 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.2.0
+ */
+
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/Util/Filter.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ * Provides a basic interface for retreiving metadata from a database.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2009 Mike Lively <m@digitalsandwich.com>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: 3.3.17
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.2.0
+ */
+interface PHPUnit_Extensions_Database_DB_IMetaData
+{
+
+ /**
+ * Returns an array containing the names of all the tables in the database.
+ *
+ * @return array
+ */
+ public function getTableNames();
+
+ /**
+ * Returns an array containing the names of all the columns in the
+ * $tableName table,
+ *
+ * @param string $tableName
+ * @return array
+ */
+ public function getTableColumns($tableName);
+
+ /**
+ * Returns an array containing the names of all the primary key columns in
+ * the $tableName table.
+ *
+ * @param string $tableName
+ * @return array
+ */
+ public function getTablePrimaryKeys($tableName);
+
+ /**
+ * Returns the name of the default schema.
+ *
+ * @return string
+ */
+ public function getSchema();
+
+ /**
+ * Returns a quoted schema object. (table name, column name, etc)
+ *
+ * @param string $object
+ * @return string
+ */
+ public function quoteSchemaObject($object);
+
+ /**
+ * Returns true if the rdbms allows cascading
+ *
+ * @return bool
+ */
+ public function allowsCascading();
+}
+?>
View
215 tests/PHPUnit/Extensions/Database/DB/MetaData.php
@@ -0,0 +1,215 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.2.0
+ */
+
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/Util/Filter.php';
+require_once 'PHPUnit/Extensions/Database/DB/IMetaData.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ * Provides a basic constructor for all meta data classes and a factory for
+ * generating the appropriate meta data class.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2009 Mike Lively <m@digitalsandwich.com>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: 3.3.17
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.2.0
+ */
+abstract class PHPUnit_Extensions_Database_DB_MetaData implements PHPUnit_Extensions_Database_DB_IMetaData
+{
+ protected static $metaDataClassMap = array(
+ 'pgsql' => 'PHPUnit_Extensions_Database_DB_MetaData_PgSQL',
+ 'mysql' => 'PHPUnit_Extensions_Database_DB_MetaData_MySQL',
+ 'oci' => 'PHPUnit_Extensions_Database_DB_MetaData_Oci',
+ 'sqlite' => 'PHPUnit_Extensions_Database_DB_MetaData_Sqlite'
+ );
+
+ /**
+ * The PDO connection used to retreive database meta data
+ *
+ * @var PDO
+ */
+ protected $pdo;
+
+ /**
+ * The default schema name for the meta data object.
+ *
+ * @var string
+ */
+ protected $schema;
+
+ /**
+ * The character used to quote schema objects.
+ */
+ protected $schemaObjectQuoteChar = '"';
+
+ /**
+ * The command used to perform a TRUNCATE operation.
+ */
+ protected $truncateCommand = 'TRUNCATE';
+
+ /**
+ * Creates a new database meta data object using the given pdo connection
+ * and schema name.
+ *
+ * @param PDO $pdo
+ * @param string $schema
+ */
+ public final function __construct(PDO $pdo, $schema)
+ {
+ $this->pdo = $pdo;
+ $this->schema = $schema;
+ }
+
+ /**
+ * Creates a meta data object based on the driver of given $pdo object and
+ * $schema name.
+ *
+ * @param PDO $pdo
+ * @param string $schema
+ * @return PHPUnit_Extensions_Database_DB_MetaData
+ */
+ public static function createMetaData(PDO $pdo, $schema)
+ {
+ $driverName = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
+ if (isset(self::$metaDataClassMap[$driverName])) {
+ $className = self::$metaDataClassMap[$driverName];
+
+ if ($className instanceof ReflectionClass) {
+ return $className->newInstance($pdo, $schema);
+ } else {
+ return self::registerClassWithDriver($className, $driverName)->newInstance($pdo, $schema);
+ }
+ } else {
+ throw new Exception("Could not find a meta data driver for {$driverName} pdo driver.");
+ }
+ }
+
+ /**
+ * Validates and registers the given $className with the given $pdoDriver.
+ * It should be noted that this function will not attempt to include /
+ * require the file. The $pdoDriver can be determined by the value of the
+ * PDO::ATTR_DRIVER_NAME attribute for a pdo object.
+ *
+ * A reflection of the $className is returned.
+ *
+ * @param string $className
+ * @param string $pdoDriver
+ * @return ReflectionClass
+ */
+ public static function registerClassWithDriver($className, $pdoDriver)
+ {
+ if (!class_exists($className)) {
+ throw new Exception("Specified class for {$pdoDriver} driver ({$className}) does not exist.");
+ }
+
+ $reflection = new ReflectionClass($className);
+ if ($reflection->isSubclassOf('PHPUnit_Extensions_Database_DB_MetaData')) {
+ return self::$metaDataClassMap[$pdoDriver] = $reflection;
+ } else {
+ throw new Exception("Specified class for {$pdoDriver} driver ({$className}) does not extend PHPUnit_Extensions_Database_DB_MetaData.");
+ }
+ }
+
+ /**
+ * Returns the schema for the connection.
+ *
+ * @return string
+ */
+ public function getSchema()
+ {
+ return $this->schema;
+ }
+
+ /**
+ * Returns a quoted schema object. (table name, column name, etc)
+ *
+ * @param string $object
+ * @return string
+ */
+ public function quoteSchemaObject($object)
+ {
+ return $this->schemaObjectQuoteChar.
+ str_replace($this->schemaObjectQuoteChar, $this->schemaObjectQuoteChar.$this->schemaObjectQuoteChar, $object).
+ $this->schemaObjectQuoteChar;
+ }
+
+ /**
+ * Returns the command for the database to truncate a table.
+ *
+ * @return string
+ */
+ public function getTruncateCommand()
+ {
+ return $this->truncateCommand;
+ }
+
+ /**
+ * Returns true if the rdbms allows cascading
+ *
+ * @return bool
+ */
+ public function allowsCascading()
+ {
+ return FALSE;
+ }
+}
+
+/**
+ * I am not sure why these requires can't go above the class, but when they do
+ * the classes can't find the PHPUnit_Extensions_Database_DB_MetaData
+ * class.
+ */
+require_once 'PHPUnit/Extensions/Database/DB/MetaData/Sqlite.php';
+require_once 'PHPUnit/Extensions/Database/DB/MetaData/InformationSchema.php';
+require_once 'PHPUnit/Extensions/Database/DB/MetaData/MySQL.php';
+require_once 'PHPUnit/Extensions/Database/DB/MetaData/PgSQL.php';
+?>
View
184 tests/PHPUnit/Extensions/Database/DB/MetaData/InformationSchema.php
@@ -0,0 +1,184 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.2.0
+ */
+
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/Util/Filter.php';
+require_once 'PHPUnit/Extensions/Database/DB/MetaData.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ * Provides functionality to retrieve meta data from a database with information_schema support.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: 3.3.17
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.2.0
+ */
+class PHPUnit_Extensions_Database_DB_MetaData_InformationSchema extends PHPUnit_Extensions_Database_DB_MetaData
+{
+
+ protected $columns = array();
+
+ protected $keys = array();
+
+ /**
+ * Returns an array containing the names of all the tables in the database.
+ *
+ * @return array
+ */
+ public function getTableNames()
+ {
+ $query = "
+ SELECT DISTINCT
+ TABLE_NAME
+ FROM INFORMATION_SCHEMA.TABLES
+ WHERE
+ TABLE_TYPE='BASE TABLE' AND
+ TABLE_SCHEMA = ?
+ ORDER BY TABLE_NAME
+ ";
+
+ $statement = $this->pdo->prepare($query);
+ $statement->execute(array($this->getSchema()));
+
+ $tableNames = array();
+ while ($tableName = $statement->fetchColumn(0)) {
+ $tableNames[] = $tableName;
+ }
+
+ return $tableNames;
+ }
+
+ /**
+ * Returns an array containing the names of all the columns in the
+ * $tableName table,
+ *
+ * @param string $tableName
+ * @return array
+ */
+ public function getTableColumns($tableName)
+ {
+ if (!isset($this->columns[$tableName])) {
+ $this->loadColumnInfo($tableName);
+ }
+
+ return $this->columns[$tableName];
+ }
+
+ /**
+ * Returns an array containing the names of all the primary key columns in
+ * the $tableName table.
+ *
+ * @param string $tableName
+ * @return array
+ */
+ public function getTablePrimaryKeys($tableName)
+ {
+ if (!isset($this->keys[$tableName])) {
+ $this->loadColumnInfo($tableName);
+ }
+
+ return $this->keys[$tableName];
+ }
+
+ /**
+ * Loads column info from a sqlite database.
+ *
+ * @param string $tableName
+ */
+ protected function loadColumnInfo($tableName)
+ {
+ $this->columns[$tableName] = array();
+ $this->keys[$tableName] = array();
+
+ $columnQuery = "
+ SELECT DISTINCT
+ COLUMN_NAME
+ FROM INFORMATION_SCHEMA.COLUMNS
+ WHERE
+ TABLE_NAME = ? AND
+ TABLE_SCHEMA = ?
+ ORDER BY ORDINAL_POSITION
+ ";
+
+ $columnStatement = $this->pdo->prepare($columnQuery);
+ $columnStatement->execute(array($tableName, $this->getSchema()));
+
+ while ($columName = $columnStatement->fetchColumn(0)) {
+ $this->columns[$tableName][] = $columName;
+ }
+
+ $keyQuery = "
+ SELECT
+ KCU.COLUMN_NAME
+ FROM
+ INFORMATION_SCHEMA.TABLE_CONSTRAINTS as TC,
+ INFORMATION_SCHEMA.KEY_COLUMN_USAGE as KCU
+ WHERE
+ TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND
+ TC.TABLE_NAME = KCU.TABLE_NAME AND
+ TC.TABLE_SCHEMA = KCU.TABLE_SCHEMA AND
+ TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND
+ TC.TABLE_NAME = ? AND
+ TC.TABLE_SCHEMA = ?
+ ORDER BY
+ KCU.ORDINAL_POSITION ASC
+ ";
+
+ $keyStatement = $this->pdo->prepare($keyQuery);
+ $keyStatement->execute(array($tableName, $this->getSchema()));
+
+ while ($columName = $keyStatement->fetchColumn(0)) {
+ $this->keys[$tableName][] = $columName;
+ }
+ }
+}
+?>
View
137 tests/PHPUnit/Extensions/Database/DB/MetaData/MySQL.php
@@ -0,0 +1,137 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.2.0
+ */
+
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/Util/Filter.php';
+require_once 'PHPUnit/Extensions/Database/DB/MetaData/InformationSchema.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ * Provides functionality to retrieve meta data from a database with information_schema support.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Mike Lively <m@digitalsandwich.com>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: 3.3.17
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.2.0
+ */
+class PHPUnit_Extensions_Database_DB_MetaData_MySQL extends PHPUnit_Extensions_Database_DB_MetaData
+{
+ protected $schemaObjectQuoteChar = '`';
+
+ protected $columns = array();
+
+ protected $keys = array();
+
+ /**
+ * Returns an array containing the names of all the tables in the database.
+ *
+ * @return array
+ */
+ public function getTableNames()
+ {
+ $query = 'SHOW TABLES';
+ $statement = $this->pdo->prepare($query);
+ $statement->execute();
+
+ $tableNames = array();
+ while (($tableName = $statement->fetchColumn(0))) {
+ $tableNames[] = $tableName;
+ }
+
+ return $tableNames;
+ }
+
+ /**
+ * Returns an array containing the names of all the columns in the
+ * $tableName table,
+ *
+ * @param string $tableName
+ * @return array
+ */
+ public function getTableColumns($tableName)
+ {
+ $query = 'SHOW COLUMNS FROM `' . $tableName . '`';
+ $statement = $this->pdo->prepare($query);
+ $statement->execute();
+
+ $columnNames = array();
+ while (($columnName = $statement->fetchColumn(0))) {
+ $columnNames[] = $columnName;
+ }
+
+ return $columnNames;
+ }
+
+ /**
+ * Returns an array containing the names of all the primary key columns in
+ * the $tableName table.
+ *
+ * @param string $tableName
+ * @return array
+ */
+ public function getTablePrimaryKeys($tableName)
+ {
+ $query = 'SHOW INDEX FROM `' . $tableName . '`';
+ $statement = $this->pdo->prepare($query);
+ $statement->execute();
+ $statement->setFetchMode(PDO::FETCH_ASSOC);
+
+ $columnNames = array();
+ while (($column = $statement->fetch())) {
+ if ($column['Key_name'] == 'PRIMARY') {
+ $columnNames[] = $column['Column_name'];
+ }
+ }
+
+ return $columnNames;
+ }
+}
+?>
View
164 tests/PHPUnit/Extensions/Database/DB/MetaData/Oci.php
@@ -0,0 +1,164 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Trond Hansen <trond@xait.no>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.2.3
+ */
+
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/Util/Filter.php';
+require_once 'PHPUnit/Extensions/Database/DB/MetaData.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ * Provides functionality to retrieve meta data from an Oracle database.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Trond Hansen <trond@xait.no>
+ * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: 3.3.17
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.2.3
+ */
+class PHPUnit_Extensions_Database_DB_MetaData_Oci extends PHPUnit_Extensions_Database_DB_MetaData
+{
+ /**
+ * No character used to quote schema objects.
+ */
+ protected $schemaObjectQuoteChar = '';
+
+ protected $columns = array();
+ protected $keys = array();
+
+ /**
+ * Returns an array containing the names of all the tables in the database.
+ *
+ * @return array
+ */
+ public function getTableNames()
+ {
+ $tableNames = array();
+
+ $query = "SELECT table_name
+ FROM cat
+ WHERE table_type='TABLE'
+ ORDER BY table_name";
+
+ $result = $this->pdo->query($query);
+
+ while ($tableName = $result->fetchColumn(0)) {
+ $tableNames[] = $tableName;
+ }
+
+ return $tableNames;
+ }
+
+ /**
+ * Returns an array containing the names of all the columns in the
+ * $tableName table,
+ *
+ * @param string $tableName
+ * @return array
+ */
+ public function getTableColumns($tableName)
+ {
+ if (!isset($this->columns[$tableName])) {
+ $this->loadColumnInfo($tableName);
+ }
+
+ return $this->columns[$tableName];
+ }
+
+ /**
+ * Returns an array containing the names of all the primary key columns in
+ * the $tableName table.
+ *
+ * @param string $tableName
+ * @return array
+ */
+ public function getTablePrimaryKeys($tableName)
+ {
+ if (!isset($this->keys[$tableName])) {
+ $this->loadColumnInfo($tableName);
+ }
+
+ return $this->keys[$tableName];
+ }
+
+ /**
+ * Loads column info from a oracle database.
+ *
+ * @param string $tableName
+ */
+ protected function loadColumnInfo($tableName)
+ {
+ $this->columns[$tableName] = array();
+ $this->keys[$tableName] = array();
+
+ $query = "SELECT DISTINCT COLUMN_NAME
+ FROM USER_TAB_COLUMNS
+ WHERE TABLE_NAME='".$tableName."'
+ ORDER BY COLUMN_NAME";
+
+ $result = $this->pdo->query($query);
+
+ while ($columnName = $result->fetchColumn(0)) {
+ $this->columns[$tableName][] = $columnName;
+ }
+
+ $keyQuery = "SELECT b.column_name
+ FROM user_constraints a, user_cons_columns b
+