Skip to content

Commit

Permalink
First unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike authored and Mike committed May 7, 2010
1 parent 5eb61eb commit 68973e9
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -18,4 +18,4 @@ Roadmap
4. Implement mapping from form elements to object properties
5. Implement validators on form elements
6. Implement object association fields
x. ...
7. ...
5 changes: 3 additions & 2 deletions lib/FORM/FormManager.php
Expand Up @@ -32,8 +32,9 @@ class FormManager
/**
* @param Configuration $config
*/
public function __construct(\Doctrine\ORM\EntityManager $em, Configuration $config)
public function __construct(\Doctrine\ORM\EntityManager $em, Configuration $config = null)
{
$this->_em = $em;
$this->_config = $config;
}

Expand All @@ -44,7 +45,7 @@ public function __construct(\Doctrine\ORM\EntityManager $em, Configuration $conf
*/
public function getRepository($modelName)
{
if (isset($this->_repositories[$modelName])) {
if (!isset($this->_repositories[$modelName])) {
$this->_repositories[$modelName] = new FormRepository($this, $modelName);
$this->_repositories[$modelName]->setEntityManager($this->getEntityManager());
}
Expand Down
10 changes: 9 additions & 1 deletion lib/FORM/FormRepository.php
Expand Up @@ -53,9 +53,17 @@ public function getElement($propertyName){}
* @param Doctrine\ORM\EntityManager $em
* @return FormRepository
*/
public function setEntityRepository($em)
public function setEntityManager($em)
{
$this->_em = $em;
return $this;
}

/**
* @return Doctrine\ORM\EntityManager
*/
public function getEntityManager()
{
return $this->_em;
}
}
Empty file.
Empty file.
8 changes: 1 addition & 7 deletions nbproject/private/private.xml
@@ -1,11 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<coverage xmlns="http://www.netbeans.org/ns/code-coverage/1" enabled="true"/>
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/1">
<file>file:/Users/mike/NetBeansProjects/FORM/README</file>
<file>file:/Users/mike/NetBeansProjects/FORM/lib/FORM/FormManager.php</file>
<file>file:/Users/mike/NetBeansProjects/FORM/lib/FORM/FormRepository.php</file>
<file>file:/Users/mike/NetBeansProjects/FORM/lib/FORM/Mapping/MetadataFactory.php</file>
<file>file:/Users/mike/NetBeansProjects/FORM/lib/FORM/Configuration.php</file>
</open-files>
</project-private>
3 changes: 3 additions & 0 deletions nbproject/project.properties
@@ -1,5 +1,8 @@
include.path=${php.global.include.path}
php.version=PHP_53
phpunit.bootstrap=
phpunit.configuration=
phpunit.suite=
source.encoding=UTF-8
src.dir=.
tags.asp=false
Expand Down
28 changes: 28 additions & 0 deletions tests/Test/FORM/AllTests.php
@@ -0,0 +1,28 @@
<?php

namespace Test\FORM;

if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'FORM_AllTests::main');
}

require_once __DIR__ . '/../Init.php';

class AllTests
{
public static function main()
{
\PHPUnit_TextUI_TestRunner::run(self::suite());
}

public static function suite()
{
$suite = new \Test\FORMTestSuite('FORM Functional Tests');

return $suite;
}
}

if (PHPUnit_MAIN_METHOD == 'Functional_AllTests::main') {
AllTests::main();
}
37 changes: 37 additions & 0 deletions tests/Test/FORM/FormManagerTest.php
@@ -0,0 +1,37 @@
<?php

namespace Test\FORM;

require_once __DIR__ . '/../Init.php';

class FormManagerTest extends \Test\FORMTestCase
{
/**
* @var FormManager
*/
protected $_fm;

protected $_em;
protected $_config;

public function setUp()
{
parent::setUp();
$this->_em = $this->_getTestEntityManager();
$this->_config = new \FORM\Configuration();
$this->_fm = new \FORM\FormManager($this->_em, $this->_config);
}

public function testConstructor()
{
$this->assertEquals($this->_em, $this->_fm->getEntityManager());
$this->assertEquals($this->_config, $this->_fm->getConfiguration());
}

public function testGetRepository()
{
$repository = $this->_fm->getRepository('Test');
$this->assertEquals('FORM\FormRepository', get_class($repository));
$this->assertEquals($this->_em, $repository->getEntityManager());
}
}
59 changes: 59 additions & 0 deletions tests/Test/FORMTestCase.php
Expand Up @@ -4,4 +4,63 @@

abstract class FORMTestCase extends \PHPUnit_Framework_TestCase
{
/** The metadata cache that is shared between all ORM tests (except functional tests). */
private static $_metadataCacheImpl = null;
/** The query cache that is shared between all ORM tests (except functional tests). */
private static $_queryCacheImpl = null;

/**
* Creates an EntityManager for testing purposes.
*
* NOTE: The created EntityManager will have its dependant DBAL parts completely
* mocked out using a DriverMock, ConnectionMock, etc. These mocks can then
* be configured in the tests to simulate the DBAL behavior that is desired
* for a particular test,
*
* @return Doctrine\ORM\EntityManager
*/
protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null, $withSharedMetadata = true)
{
$config = new \Doctrine\ORM\Configuration();
if($withSharedMetadata) {
$config->setMetadataCacheImpl(self::getSharedMetadataCacheImpl());
} else {
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
}

$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());

$config->setQueryCacheImpl(self::getSharedQueryCacheImpl());
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
$eventManager = new \Doctrine\Common\EventManager();
if ($conn === null) {
$conn = array(
'driverClass' => 'Doctrine\Tests\Mocks\DriverMock',
'wrapperClass' => 'Doctrine\Tests\Mocks\ConnectionMock',
'user' => 'john',
'password' => 'wayne'
);
}
if (is_array($conn)) {
$conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
}
return \Doctrine\Tests\Mocks\EntityManagerMock::create($conn, $config, $eventManager);
}

private static function getSharedMetadataCacheImpl()
{
if (self::$_metadataCacheImpl === null) {
self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
}
return self::$_metadataCacheImpl;
}

private static function getSharedQueryCacheImpl()
{
if (self::$_queryCacheImpl === null) {
self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
}
return self::$_queryCacheImpl;
}
}
2 changes: 1 addition & 1 deletion tests/Test/Functional/AllTests.php
Expand Up @@ -3,7 +3,7 @@
namespace Test\Functional;

if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'Common_AllTests::main');
define('PHPUnit_MAIN_METHOD', 'Functional_AllTests::main');
}

require_once __DIR__ . '/../Init.php';
Expand Down
3 changes: 3 additions & 0 deletions tests/Test/Init.php
Expand Up @@ -16,6 +16,9 @@
$classLoader = new \Doctrine\Common\ClassLoader('FORM', __DIR__ . "/../../lib");
$classLoader->register();

$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\Tests', __DIR__ . "/../../vendor/Doctrine2/tests");
$classLoader->register();

$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', __DIR__ . "/../../vendor/Doctrine2/lib");
$classLoader->register();

Expand Down

0 comments on commit 68973e9

Please sign in to comment.