diff --git a/lib/Doctrine/Common/Collections/Collection.php b/lib/Doctrine/Common/Collections/Collection.php index 4ee8dc25b..91321e1b6 100644 --- a/lib/Doctrine/Common/Collections/Collection.php +++ b/lib/Doctrine/Common/Collections/Collection.php @@ -127,22 +127,6 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess return null; } - /** - * @see containsKey() - */ - public function __isset($key) - { - return $this->containsKey($key); - } - - /** - * @see remove() - */ - public function __unset($key) - { - return $this->remove($key); - } - /* ArrayAccess implementation */ /** diff --git a/lib/Doctrine/Common/DoctrineException.php b/lib/Doctrine/Common/DoctrineException.php index e15121e3d..97a00ae75 100644 --- a/lib/Doctrine/Common/DoctrineException.php +++ b/lib/Doctrine/Common/DoctrineException.php @@ -29,8 +29,9 @@ class DoctrineException extends \Exception $messageKey = substr($class, strrpos($class, '\\') + 1) . "#$method"; $end = end($arguments); + $innerException = null; if ($end instanceof \Exception) { - $this->_innerException = $end; + $innerException = $end; unset($arguments[count($arguments) - 1]); } @@ -46,7 +47,7 @@ class DoctrineException extends \Exception $message .= ' (' . implode(', ', $args) . ')'; } - return new $class($message); + return new $class($message, $innerException); } public static function getExceptionMessage($messageKey) diff --git a/lib/Doctrine/Common/EventManager.php b/lib/Doctrine/Common/EventManager.php index 80dbbcd3c..7a13957c8 100644 --- a/lib/Doctrine/Common/EventManager.php +++ b/lib/Doctrine/Common/EventManager.php @@ -80,7 +80,7 @@ class EventManager */ public function hasListeners($event) { - return isset($this->_listeners[$event]); + return isset($this->_listeners[$event]) && ! empty($this->_listeners[$event]); } /** @@ -106,7 +106,7 @@ class EventManager public function removeEventListener($events, $listener) { foreach ((array)$events as $event) { - if ($key = array_search($listener, $this->_listeners[$event], true)) { + if (($key = array_search($listener, $this->_listeners[$event], true)) !== false) { unset($this->_listeners[$event][$key]); } } diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index 1d7208506..e3b01d438 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -202,7 +202,7 @@ class Connection */ public function getHost() { - return $this->_params['host']; + return isset($this->_params['host']) ? $this->_params['host'] : null; } /** @@ -212,7 +212,7 @@ class Connection */ public function getPort() { - return $this->_params['port']; + return isset($this->_params['port']) ? $this->_params['port'] : null; } /** @@ -222,7 +222,7 @@ class Connection */ public function getUsername() { - return $this->_params['user']; + return isset($this->_params['user']) ? $this->_params['user'] : null; } /** @@ -232,7 +232,7 @@ class Connection */ public function getPassword() { - return $this->_params['password']; + return isset($this->_params['password']) ? $this->_params['password'] : null; } /** diff --git a/lib/Doctrine/DBAL/DriverManager.php b/lib/Doctrine/DBAL/DriverManager.php index 105241ae0..fe07ba526 100644 --- a/lib/Doctrine/DBAL/DriverManager.php +++ b/lib/Doctrine/DBAL/DriverManager.php @@ -48,7 +48,10 @@ final class DriverManager ); /** Private constructor. This class cannot be instantiated. */ - private function __construct() {} + public function __construct() + { + throw \Doctrine\Common\DoctrineException::driverManagerCannotBeInstantiated(); + } /** * Creates a connection object based on the specified parameters. @@ -109,9 +112,9 @@ final class DriverManager // check for existing pdo object if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) { - throw DoctrineException::invalidPDOInstance(); + throw DoctrineException::invalidPdoInstance(); } else if (isset($params['pdo'])) { - $params['driver'] = $params['pdo']->getAttribute(\PDO::ATTR_DRIVER_NAME); + $params['driver'] = 'pdo_' . $params['pdo']->getAttribute(\PDO::ATTR_DRIVER_NAME); } else { self::_checkParams($params); } diff --git a/tests/Doctrine/Tests/Common/AllTests.php b/tests/Doctrine/Tests/Common/AllTests.php index 27b3a3b75..e78183d71 100644 --- a/tests/Doctrine/Tests/Common/AllTests.php +++ b/tests/Doctrine/Tests/Common/AllTests.php @@ -4,6 +4,7 @@ namespace Doctrine\Tests\Common; use Doctrine\Tests\Common\Collections; use Doctrine\Tests\Common\Annotations; +use Doctrine\Tests\Common\Cache; if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'Common_AllTests::main'); @@ -28,6 +29,7 @@ class AllTests $suite->addTest(Collections\AllTests::suite()); $suite->addTest(Annotations\AllTests::suite()); + $suite->addTest(Cache\AllTests::suite()); return $suite; } diff --git a/tests/Doctrine/Tests/Common/Annotations/AnnotationReaderTest.php b/tests/Doctrine/Tests/Common/Annotations/AnnotationReaderTest.php index 078394b08..8973f9183 100644 --- a/tests/Doctrine/Tests/Common/Annotations/AnnotationReaderTest.php +++ b/tests/Doctrine/Tests/Common/Annotations/AnnotationReaderTest.php @@ -46,6 +46,16 @@ class AnnotationReaderTest extends \Doctrine\Tests\DoctrineTestCase $this->assertEquals('col2', $joinTableAnnot->joinColumns[0]->referencedColumnName); $this->assertEquals('col3', $joinTableAnnot->inverseJoinColumns[0]->name); $this->assertEquals('col4', $joinTableAnnot->inverseJoinColumns[0]->referencedColumnName); + + $dummyAnnot = $reader->getMethodAnnotation($class->getMethod('getField1'), 'Doctrine\Tests\Common\Annotations\DummyAnnotation'); + $this->assertEquals('', $dummyAnnot->dummyValue); + $this->assertEquals(array(1, 2, 'three'), $dummyAnnot->value); + + $dummyAnnot = $reader->getPropertyAnnotation($class->getProperty('field1'), 'Doctrine\Tests\Common\Annotations\DummyAnnotation'); + $this->assertEquals('fieldHello', $dummyAnnot->dummyValue); + + $classAnnot = $reader->getClassAnnotation($class, 'Doctrine\Tests\Common\Annotations\DummyAnnotation'); + $this->assertEquals('hello', $classAnnot->dummyValue); } } diff --git a/tests/Doctrine/Tests/Common/Cache/AllTests.php b/tests/Doctrine/Tests/Common/Cache/AllTests.php new file mode 100644 index 000000000..374ba9b06 --- /dev/null +++ b/tests/Doctrine/Tests/Common/Cache/AllTests.php @@ -0,0 +1,33 @@ +addTestSuite('Doctrine\Tests\Common\Cache\ApcCacheTest'); + $suite->addTestSuite('Doctrine\Tests\Common\Cache\ArrayCacheTest'); + $suite->addTestSuite('Doctrine\Tests\Common\Cache\MemcacheCacheTest'); + $suite->addTestSuite('Doctrine\Tests\Common\Cache\XcacheCacheTest'); + + return $suite; + } +} + +if (PHPUnit_MAIN_METHOD == 'Common_Cache_AllTests::main') { + AllTests::main(); +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Common/Cache/ApcCacheTest.php b/tests/Doctrine/Tests/Common/Cache/ApcCacheTest.php new file mode 100644 index 000000000..efd8470bc --- /dev/null +++ b/tests/Doctrine/Tests/Common/Cache/ApcCacheTest.php @@ -0,0 +1,43 @@ +markTestSkipped('The ' . __CLASS__ .' requires the use of APC'); + } + } + + public function testApcCacheDriver() + { + $cache = new ApcCache(); + + // Test save + $cache->save('test_key', 'testing this out'); + + // Test contains to test that save() worked + $this->assertTrue($cache->contains('test_key')); + + // Test fetch + $this->assertEquals('testing this out', $cache->fetch('test_key')); + + // Test count + $this->assertEquals(1, $cache->count()); + + // Test delete + $cache->save('test_key2', 'test2'); + $cache->delete('test_key2'); + $this->assertFalse($cache->contains('test_key2')); + + // Test delete all + $cache->deleteAll(); + $this->assertEquals(0, $cache->count()); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Common/Cache/ArrayCacheTest.php b/tests/Doctrine/Tests/Common/Cache/ArrayCacheTest.php new file mode 100644 index 000000000..af84e160f --- /dev/null +++ b/tests/Doctrine/Tests/Common/Cache/ArrayCacheTest.php @@ -0,0 +1,36 @@ +save('test_key', 'testing this out'); + + // Test contains to test that save() worked + $this->assertTrue($cache->contains('test_key')); + + // Test fetch + $this->assertEquals('testing this out', $cache->fetch('test_key')); + + // Test count + $this->assertEquals(1, $cache->count()); + + // Test delete + $cache->save('test_key2', 'test2'); + $cache->delete('test_key2'); + $this->assertFalse($cache->contains('test_key2')); + + // Test delete all + $cache->deleteAll(); + $this->assertEquals(0, $cache->count()); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Common/Cache/MemcacheCacheTest.php b/tests/Doctrine/Tests/Common/Cache/MemcacheCacheTest.php new file mode 100644 index 000000000..c1e78ea36 --- /dev/null +++ b/tests/Doctrine/Tests/Common/Cache/MemcacheCacheTest.php @@ -0,0 +1,43 @@ +markTestSkipped('The ' . __CLASS__ .' requires the use of memcache'); + } + } + + public function testMemcacheCacheDriver() + { + $cache = new MemcacheCache(); + + // Test save + $cache->save('test_key', 'testing this out'); + + // Test contains to test that save() worked + $this->assertTrue($cache->contains('test_key')); + + // Test fetch + $this->assertEquals('testing this out', $cache->fetch('test_key')); + + // Test count + $this->assertEquals(1, $cache->count()); + + // Test delete + $cache->save('test_key2', 'test2'); + $cache->delete('test_key2'); + $this->assertFalse($cache->contains('test_key2')); + + // Test delete all + $cache->deleteAll(); + $this->assertEquals(0, $cache->count()); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Common/Cache/XcacheCacheTest.php b/tests/Doctrine/Tests/Common/Cache/XcacheCacheTest.php new file mode 100644 index 000000000..1a0b983cb --- /dev/null +++ b/tests/Doctrine/Tests/Common/Cache/XcacheCacheTest.php @@ -0,0 +1,43 @@ +markTestSkipped('The ' . __CLASS__ .' requires the use of xcache'); + } + } + + public function testXcacheCacheDriver() + { + $cache = new XcacheCache(); + + // Test save + $cache->save('test_key', 'testing this out'); + + // Test contains to test that save() worked + $this->assertTrue($cache->contains('test_key')); + + // Test fetch + $this->assertEquals('testing this out', $cache->fetch('test_key')); + + // Test count + $this->assertEquals(1, $cache->count()); + + // Test delete + $cache->save('test_key2', 'test2'); + $cache->delete('test_key2'); + $this->assertFalse($cache->contains('test_key2')); + + // Test delete all + $cache->deleteAll(); + $this->assertEquals(0, $cache->count()); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Common/ClassLoaderTest.php b/tests/Doctrine/Tests/Common/ClassLoaderTest.php index 7ac1d4182..2b659d99c 100644 --- a/tests/Doctrine/Tests/Common/ClassLoaderTest.php +++ b/tests/Doctrine/Tests/Common/ClassLoaderTest.php @@ -28,4 +28,16 @@ class ClassLoaderTest extends \Doctrine\Tests\DoctrineTestCase // This would return a fatal error without check file exists true $this->assertEquals($classLoader->loadClass('SomeInvalidClass'), false); } + + public function testAlreadyLoadedClassReturnsFalse() + { + $classLoader = new \Doctrine\Common\ClassLoader(); + $classLoader->setBasePath('ClassLoaderTest', __DIR__); + $classLoader->setClassFileExtension('.class.php'); + $classLoader->setNamespaceSeparator('_'); + $classLoader->setCheckFileExists(true); + + $this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassA'), false); + $this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassC'), true); + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/Common/ClassLoaderTest/ClassC.class.php b/tests/Doctrine/Tests/Common/ClassLoaderTest/ClassC.class.php new file mode 100644 index 000000000..d2e33efe4 --- /dev/null +++ b/tests/Doctrine/Tests/Common/ClassLoaderTest/ClassC.class.php @@ -0,0 +1,6 @@ +_coll = new \Doctrine\Common\Collections\Collection; } + public function testIssetAndUnset() + { + $this->assertFalse(isset($this->_coll[0])); + $this->_coll->add('testing'); + $this->assertTrue(isset($this->_coll[0])); + unset($this->_coll[0]); + $this->assertFalse(isset($this->_coll[0])); + } + + public function testToString() + { + $this->_coll->add('testing'); + $this->assertTrue(is_string((string) $this->_coll)); + } + + public function testRemovingNonExistentEntryReturnsNull() + { + $this->assertEquals(null, $this->_coll->remove('testing_does_not_exist')); + } + public function testExists() { $this->_coll->add("one"); diff --git a/tests/Doctrine/Tests/Common/DoctrineExceptionTest.php b/tests/Doctrine/Tests/Common/DoctrineExceptionTest.php index bfaaab550..225b91ed4 100644 --- a/tests/Doctrine/Tests/Common/DoctrineExceptionTest.php +++ b/tests/Doctrine/Tests/Common/DoctrineExceptionTest.php @@ -12,4 +12,28 @@ class DoctrineExceptionTest extends \Doctrine\Tests\DoctrineTestCase $this->assertEquals($e->getMessage(), "Testing static call builds error message with params ('param1', 'param2')"); } + + public function testInnerException() + { + $e1 = \Doctrine\Common\DoctrineException::testException(); + $e2 = \Doctrine\Common\DoctrineException::testException2('param1', $e1); + $this->assertEquals($e1, $e2->getInnerException()); + } + + public function testNotImplemented() + { + $e = \Doctrine\Common\DoctrineException::notImplemented('testMethod', 'SomeClass'); + $this->assertEquals("The method 'testMethod' is not implemented in class 'SomeClass'.", $e->getMessage()); + } + + public function testGetExceptionMessage() + { + $this->assertEquals('The query contains more than one result.', \Doctrine\Common\DoctrineException::getExceptionMessage('QueryException#nonUniqueResult')); + } + + public function testUseGetExceptionMessage() + { + $q = \Doctrine\ORM\Query\QueryException::nonUniqueResult(); + $this->assertEquals('The query contains more than one result.', $q->getMessage()); + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/Common/EventManagerTest.php b/tests/Doctrine/Tests/Common/EventManagerTest.php index 38e492dfb..1106eeee5 100644 --- a/tests/Doctrine/Tests/Common/EventManagerTest.php +++ b/tests/Doctrine/Tests/Common/EventManagerTest.php @@ -12,6 +12,8 @@ class EventManagerTest extends \Doctrine\Tests\DoctrineTestCase /* Some pseudo events */ const preFoo = 'preFoo'; const postFoo = 'postFoo'; + const preBar = 'preBar'; + const postBar = 'postBar'; private $_preFooInvoked = false; private $_postFooInvoked = false; @@ -50,6 +52,22 @@ class EventManagerTest extends \Doctrine\Tests\DoctrineTestCase $this->assertFalse($this->_postFooInvoked); } + public function testRemoveEventListener() + { + $this->_eventManager->addEventListener(array('preBar'), $this); + $this->assertTrue($this->_eventManager->hasListeners(self::preBar)); + $this->_eventManager->removeEventListener(array('preBar'), $this); + $this->assertFalse($this->_eventManager->hasListeners(self::preBar)); + } + + public function testAddEventSubscriber() + { + $eventSubscriber = new TestEventSubscriber(); + $this->_eventManager->addEventSubscriber($eventSubscriber); + $this->assertTrue($this->_eventManager->hasListeners(self::preFoo)); + $this->assertTrue($this->_eventManager->hasListeners(self::postFoo)); + } + /* Listener methods */ public function preFoo(EventArgs $e) @@ -61,4 +79,12 @@ class EventManagerTest extends \Doctrine\Tests\DoctrineTestCase { $this->_postFooInvoked = true; } +} + +class TestEventSubscriber implements \Doctrine\Common\EventSubscriber +{ + public function getSubscribedEvents() + { + return array('preFoo', 'postFoo'); + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/AllTests.php b/tests/Doctrine/Tests/DBAL/AllTests.php index 6192e651b..a9af256f3 100644 --- a/tests/Doctrine/Tests/DBAL/AllTests.php +++ b/tests/Doctrine/Tests/DBAL/AllTests.php @@ -42,6 +42,13 @@ class AllTests $suite->addTestSuite('Doctrine\Tests\DBAL\Types\SmallIntTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Types\StringTest'); + // Driver manager test + $suite->addTestSuite('Doctrine\Tests\DBAL\DriverManagerTest'); + + // Connection test + $suite->addTestSuite('Doctrine\Tests\DBAL\Connectiontest'); + + // All Functional DBAL tests $suite->addTest(Functional\AllTests::suite()); return $suite; diff --git a/tests/Doctrine/Tests/DBAL/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/ConnectionTest.php new file mode 100644 index 000000000..0daac0eeb --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/ConnectionTest.php @@ -0,0 +1,50 @@ + 'pdo_mysql', + 'host' => 'localhost', + 'user' => 'root', + 'password' => 'password', + 'port' => '1234' + ); + $this->_conn = \Doctrine\DBAL\DriverManager::getConnection($params); + } + + public function testGetHost() + { + $this->assertEquals('localhost', $this->_conn->getHost()); + } + + public function testGetPort() + { + $this->assertEquals('1234', $this->_conn->getPort()); + } + + public function testGetUsername() + { + $this->assertEquals('root', $this->_conn->getUsername()); + } + + public function testGetPassword() + { + $this->assertEquals('password', $this->_conn->getPassword()); + } + + public function testGetDriver() + { + $this->assertEquals('Doctrine\DBAL\Driver\PDOMySql\Driver', get_class($this->_conn->getDriver())); + } + + public function testGetEventManager() + { + $this->assertEquals('Doctrine\Common\EventManager', get_class($this->_conn->getEventManager())); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/DriverManagerTest.php b/tests/Doctrine/Tests/DBAL/DriverManagerTest.php new file mode 100644 index 000000000..de7a150df --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/DriverManagerTest.php @@ -0,0 +1,52 @@ + 'test' + ); + $test = \Doctrine\DBAL\DriverManager::getConnection($options); + } + + public function testValidPdoInstance() + { + $options = array( + 'pdo' => new \PDO('sqlite::memory:') + ); + $conn = \Doctrine\DBAL\DriverManager::getConnection($options); + $this->assertEquals('sqlite', $conn->getDatabasePlatform()->getName()); + } + + /** + * @expectedException \Doctrine\Common\DoctrineException + */ + public function testCheckParams() + { + $conn = \Doctrine\DBAL\DriverManager::getConnection(array()); + } + + /** + * @expectedException \Doctrine\Common\DoctrineException + */ + public function testInvalidDriver() + { + $conn = \Doctrine\DBAL\DriverManager::getConnection(array('driver' => 'invalid_driver')); + } +} \ No newline at end of file