Commit 311cff87 authored by jwage's avatar jwage

[2.0] More test coverage

parent 26bdd89b
......@@ -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 */
/**
......
......@@ -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)
......
......@@ -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]);
}
}
......
......@@ -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;
}
/**
......
......@@ -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);
}
......
......@@ -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;
}
......
......@@ -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);
}
}
......
<?php
namespace Doctrine\Tests\Common\Cache;
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'Common_Cache_AllTests::main');
}
require_once __DIR__ . '/../../TestInit.php';
class AllTests
{
public static function main()
{
\PHPUnit_TextUI_TestRunner::run(self::suite());
}
public static function suite()
{
$suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Common Cache Tests');
$suite->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
<?php
namespace Doctrine\Tests\Common\Cache;
use Doctrine\Common\Cache\ApcCache;
require_once __DIR__ . '/../../TestInit.php';
class ApcCacheTest extends \Doctrine\Tests\DoctrineTestCase
{
public function setUp()
{
if ( ! extension_loaded('apc')) {
$this->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
<?php
namespace Doctrine\Tests\Common\Cache;
use Doctrine\Common\Cache\ArrayCache;
require_once __DIR__ . '/../../TestInit.php';
class ArrayCacheTest extends \Doctrine\Tests\DoctrineTestCase
{
public function testArrayCacheDriver()
{
$cache = new ArrayCache();
// 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
<?php
namespace Doctrine\Tests\Common\Cache;
use Doctrine\Common\Cache\MemcacheCache;
require_once __DIR__ . '/../../TestInit.php';
class MemcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase
{
public function setUp()
{
if ( ! extension_loaded('memcache')) {
$this->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
<?php
namespace Doctrine\Tests\Common\Cache;
use Doctrine\Common\Cache\XcacheCache;
require_once __DIR__ . '/../../TestInit.php';
class XcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase
{
public function setUp()
{
if ( ! extension_loaded('xcache')) {
$this->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
......@@ -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
<?php
class ClassLoaderTest_ClassC
{
}
\ No newline at end of file
......@@ -15,6 +15,26 @@ class CollectionTest extends \Doctrine\Tests\DoctrineTestCase
$this->_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");
......
......@@ -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
......@@ -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
......@@ -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;
......
<?php
namespace Doctrine\Tests\DBAL;
require_once __DIR__ . '/../TestInit.php';
class ConnectionTest extends \Doctrine\Tests\DbalTestCase
{
public function setUp()
{
$params = array(
'driver' => '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
<?php
namespace Doctrine\Tests\DBAL;
require_once __DIR__ . '/../TestInit.php';
class DriverManagerTest extends \Doctrine\Tests\DbalTestCase
{
/**
* @expectedException \Doctrine\Common\DoctrineException
*/
public function testCantInstantiateDriverManager()
{
$test = new \Doctrine\DBAL\DriverManager();
}
/**
* @expectedException \Doctrine\Common\DoctrineException
*/
public function testInvalidPdoInstance()
{
$options = array(
'pdo' => '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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment