Skip to content

Commit 82cb038

Browse files
committed
Merge pull request #13 from symfony-cmf/orm_support
[WIP] Added Orm support
2 parents 0a5180b + 142e53c commit 82cb038

File tree

6 files changed

+152
-11
lines changed

6 files changed

+152
-11
lines changed

bin/console

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
#!/usr/bin/env php
22
<?php
33

4-
$kernel = include __DIR__.'/../bootstrap/kernel_bootstrap.php';
4+
$rootDir = realpath(__DIR__.'/../../../..');
5+
$vendorDir = realpath($rootDir.'/vendor');
6+
require_once $vendorDir.'/symfony-cmf/testing/bootstrap/bootstrap.php';
57

68
use Symfony\Bundle\FrameworkBundle\Console\Application;
79
use Symfony\Component\Console\Input\ArgvInput;
810

11+
$input = new ArgvInput();
12+
$env = $input->getParameterOption(array('--env', '-e'), 'phpcr');
13+
14+
// must be placed after setting $env, because it's used in bootstrapping the
15+
// kernel
16+
$kernel = include __DIR__.'/../bootstrap/kernel_bootstrap.php';
17+
918
$application = new Application($kernel);
10-
$application->run();
19+
$application->run($input);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
DIR_NAME=`dirname $0`
4+
CONSOLE_DIR=$DIR_NAME"/.."
5+
6+
# composer install --dev
7+
php $CONSOLE_DIR"/console" doctrine:database:create --env=orm
8+
php $CONSOLE_DIR"/console" doctrine:schema:create --env=orm
9+
php $CONSOLE_DIR"/console" doctrine:phpcr:repository:init

bootstrap/kernel_bootstrap.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
2-
$rootDir = realpath(__DIR__.'/../../../..');
3-
$vendorDir = realpath($rootDir.'/vendor');
2+
43
$phpUnitFile = $rootDir.'/phpunit.xml.dist';
54

65
if (!file_exists($phpUnitFile)) {
@@ -22,7 +21,6 @@
2221
));
2322
}
2423

25-
require_once $vendorDir.'/symfony-cmf/testing/bootstrap/bootstrap.php';
2624
require_once $kernelFile;
2725

28-
return new AppKernel('test', true);
26+
return new AppKernel($env, true);

resources/config/dist/doctrine.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ doctrine:
33
driver: %database_driver%
44
path: %database_path%
55
charset: UTF8
6+
orm:
7+
auto_mapping: true

src/Symfony/Cmf/Component/Testing/Functional/BaseTestCase.php

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,73 @@
44

55
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
66

7+
/**
8+
* The base class for Functional and Web tests.
9+
*
10+
* @author Daniel Leech <[email protected]>
11+
* @author Wouter J <[email protected]>
12+
*/
713
abstract class BaseTestCase extends WebTestCase
814
{
15+
/**
16+
* Use this property to save the DbManager.
17+
*/
918
protected $db;
19+
1020
protected $dbManagers = array();
11-
protected $container;
21+
protected $settings = array();
22+
protected $containers = array();
1223

24+
/**
25+
* Configure the testcase.
26+
*
27+
* Currently, this is only used for creating a new kernel. This accepts
28+
* 2 settings:
29+
*
30+
* * environment - The environment to use (defaults to 'phpcr')
31+
* * debug - If debug should be enabled/disabled (defaults to true)
32+
*
33+
* @param array $options
34+
*/
35+
protected function configure(array $options)
36+
{
37+
$this->settings = $options;
38+
}
39+
40+
/**
41+
* Gets the container.
42+
*
43+
* @return \Symfony\Component\DependencyInjection\Container
44+
*/
1345
public function getContainer()
1446
{
15-
if (null === $this->container) {
16-
$client = $this->createClient();
17-
$this->container = $client->getContainer();
47+
$hash = md5(serialize($this->settings));
48+
49+
if (!isset($this->containers[$hash])) {
50+
$client = $this->createClient($this->settings);
51+
$this->containers[$hash] = $client->getContainer();
1852
}
1953

20-
return $this->container;
54+
return $this->containers[$hash];
2155
}
2256

57+
/**
58+
* Gets the DbManager.
59+
*
60+
* @see self::getDbManager
61+
*/
2362
public function db($type)
2463
{
2564
return $this->getDbManager($type);
2665
}
2766

67+
/**
68+
* Gets the DbManager.
69+
*
70+
* @param string $type The Db type
71+
*
72+
* @return object
73+
*/
2874
public function getDbManager($type)
2975
{
3076
if (isset($this->dbManagers[$type])) {
@@ -49,4 +95,19 @@ public function getDbManager($type)
4995

5096
return $this->getDbManager($type);
5197
}
98+
99+
/**
100+
* {@inheritDoc}
101+
*
102+
* This is overriden to set the default environment to 'phpcr'
103+
*/
104+
protected static function createKernel(array $options = array())
105+
{
106+
// default environment is 'phpcr'
107+
if (!isset($options['environment'])) {
108+
$options['environment'] = 'phpcr';
109+
}
110+
111+
parent::createKernel($options);
112+
}
52113
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Component\Testing\Functional\DbManager;
4+
5+
use Symfony\Component\DependencyInjection\ContainerInterface;
6+
7+
use Doctrine\Common\Persistence\ManagerRegistry;
8+
use Doctrine\Common\Persistence\ObjectManager;
9+
10+
/**
11+
* The DbManager for the Doctrine2 ORM.
12+
*
13+
* This manager needs the DoctrineBundle to work.
14+
*
15+
* @author Wouter J <[email protected]>
16+
*/
17+
class ORM
18+
{
19+
/**
20+
* @var ContainerInterface
21+
*/
22+
protected $container;
23+
24+
/**
25+
* @var ObjectManager
26+
*/
27+
protected $om;
28+
29+
/**
30+
* Constructor.
31+
*
32+
* @param ContainerInterface $container
33+
*/
34+
public function __construct(ContainerInterface $container)
35+
{
36+
$this->container = $container;
37+
}
38+
39+
/**
40+
* Gets the Doctrine ManagerRegistry
41+
*
42+
* @return ManagerRegistry
43+
*/
44+
public function getRegistry()
45+
{
46+
return $this->container->get('doctrine');
47+
}
48+
49+
/**
50+
* Gets the Doctrine ObjectManager
51+
*
52+
* @return ObjectManager
53+
*/
54+
public function getOm()
55+
{
56+
if (!$this->om) {
57+
$this->om = $this->getRegistry()->getManager();
58+
}
59+
60+
return $this->om;
61+
}
62+
}

0 commit comments

Comments
 (0)