Skip to content

Commit 39d9823

Browse files
author
Malte Riesch
committed
1 parent 1eb2bde commit 39d9823

File tree

6 files changed

+191
-87
lines changed

6 files changed

+191
-87
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
namespace TestDbAcle\Config;
3+
4+
class DefaultFactories implements \TestDbAcle\Config\FactoriesInterface
5+
{
6+
public function getFactories($pdoDriverName){
7+
8+
if($pdoDriverName=="sqlite"){
9+
return array_merge($this->getFactoriesAllDrivers(), $this->getFactoriesSqlite());
10+
}else{
11+
return array_merge($this->getFactoriesAllDrivers(), $this->getFactoriesMysql());
12+
}
13+
14+
}
15+
16+
public function getFactoriesAllDrivers(){
17+
18+
return array(
19+
20+
'dataInserterFilterQueue'=> function(\TestDbAcle\ServiceLocator $serviceLocator) {
21+
$filterQueue = $serviceLocator->get('filterQueue');
22+
$filterQueue->addRowFilter(new \TestDbAcle\Filter\AddDefaultValuesRowFilter($serviceLocator->get('tableList')));
23+
return $filterQueue;
24+
},
25+
'filterQueue'=> function(\TestDbAcle\ServiceLocator $serviceLocator) {
26+
$filterQueue = new \TestDbAcle\Filter\FilterQueue();
27+
return $filterQueue;
28+
},
29+
'upsertBuilderFactory' => function(\TestDbAcle\ServiceLocator $serviceLocator) {
30+
return new \TestDbAcle\Db\DataInserter\Factory\UpsertBuilderFactory($serviceLocator->get('pdoFacade'));
31+
},
32+
33+
'tableList' => '\TestDbAcle\Db\TableList',
34+
'parser' => '\TestDbAcle\Psv\PsvParser',
35+
36+
);
37+
}
38+
39+
public function getFactoriesMysql(){
40+
41+
return array(
42+
'dataInserter'=> function(\TestDbAcle\ServiceLocator $serviceLocator) {
43+
$dataInserter = new \TestDbAcle\Db\DataInserter\DataInserter($serviceLocator->get('pdoFacade'), $serviceLocator->get('upsertBuilderFactory'));
44+
$dataInserter->addUpsertListener(new \TestDbAcle\Db\DataInserter\Listeners\MysqlZeroPKListener($serviceLocator->get('pdoFacade'), $serviceLocator->get('tableList')));
45+
return $dataInserter;
46+
},
47+
'pdoFacade'=> function(\TestDbAcle\ServiceLocator $serviceLocator) {
48+
$pdoFacade = new \TestDbAcle\Db\Mysql\Pdo\PdoFacade($serviceLocator->get('pdo'));
49+
$pdoFacade->disableForeignKeyChecks();
50+
$pdoFacade->enableExceptions();
51+
return $pdoFacade;
52+
},
53+
'tableFactory' => function(\TestDbAcle\ServiceLocator $serviceLocator) {
54+
return new \TestDbAcle\Db\Mysql\TableFactory($serviceLocator->get('pdoFacade'));
55+
},
56+
57+
);
58+
}
59+
60+
public static function getFactoriesSqlite(){
61+
62+
return array(
63+
'dataInserter'=> function(\TestDbAcle\ServiceLocator $serviceLocator) {
64+
return new \TestDbAcle\Db\DataInserter\DataInserter($serviceLocator->get('pdoFacade'), $serviceLocator->get('upsertBuilderFactory'));
65+
},
66+
67+
'pdoFacade'=> function(\TestDbAcle\ServiceLocator $serviceLocator) {
68+
$pdoFacade = new \TestDbAcle\Db\Sqlite\Pdo\PdoFacade($serviceLocator->get('pdo'));
69+
$pdoFacade->disableForeignKeyChecks();
70+
$pdoFacade->enableExceptions();
71+
return $pdoFacade;
72+
},
73+
'tableFactory' => function(\TestDbAcle\ServiceLocator $serviceLocator) {
74+
return new \TestDbAcle\Db\Sqlite\TableFactory($serviceLocator->get('pdoFacade'));
75+
},
76+
77+
);
78+
}
79+
}
80+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace TestDbAcle\Config;
3+
4+
interface FactoriesInterface
5+
{
6+
/**
7+
* @param string $pdoDriverName mysql|sqlite
8+
* @return array FactoriesInterface
9+
*/
10+
public function getFactories($pdoDriverName);
11+
}
12+

lib/TestDbAcle/ServiceLocator.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
namespace TestDbAcle;
3+
34
class ServiceLocator
45
{
56
protected $factories = array();
@@ -15,6 +16,13 @@ function setFactories($factories)
1516
$this->factories = $factories;
1617
}
1718

19+
function addFactories($factories)
20+
{
21+
foreach($factories as $key=>$factory){
22+
$this->setFactory($key, $factory);
23+
}
24+
}
25+
1826
function get($name)
1927
{
2028
if (!isset($this->services[$name])){
@@ -36,11 +44,19 @@ function createNew($name)
3644
return $factory($this);
3745
}
3846
}
39-
function set($name, $service)
47+
48+
function setService($name, $service)
4049
{
41-
if (isset($this->services[$name])){
42-
$this->services["prototype.$name"] = $this->services[$name];
43-
}
4450
$this->services[$name] = $service;
4551
}
52+
53+
protected function setFactory($name, $factory)
54+
{
55+
if (isset($this->factories[$name])){
56+
$this->setFactory("prototype.$name", $this->factories[$name]);
57+
}
58+
$this->factories[$name] = $factory;
59+
}
60+
61+
4662
}

lib/TestDbAcle/TestDbAcle.php

Lines changed: 8 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -34,90 +34,22 @@ public function getServiceLocator()
3434
/**
3535
* @return TestDbAcle;
3636
*/
37-
public static function create(\Pdo $pdo, $factoryOverrides = array())
37+
public static function create(\Pdo $pdo, $factoryOverrides = array(), $factories = null)
3838
{
39+
if(is_null($factories)) {
40+
$factories = new Config\DefaultFactories();
41+
}
3942

4043
$testDbAcle = new TestDbAcle();
41-
$serviceLocator = new ServiceLocator(array_merge(static::getDefaultFactories($pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)),$factoryOverrides));
42-
$serviceLocator->set('pdo', $pdo);
44+
45+
$serviceLocator = new ServiceLocator($factories->getFactories($pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)));
46+
$serviceLocator->addFactories($factoryOverrides);
47+
$serviceLocator->setService('pdo', $pdo);
4348

4449
$testDbAcle->setServiceLocator($serviceLocator);
4550

4651
return $testDbAcle;
4752
}
4853

49-
public static function getDefaultFactories($pdoDriverName){
50-
51-
if($pdoDriverName=="sqlite"){
52-
return array_merge(static::getDefaultFactoriesAllDrivers(), static::getDefaultFactoriesSqlite());
53-
}else{
54-
return array_merge(static::getDefaultFactoriesAllDrivers(), static::getDefaultFactoriesMysql());
55-
}
56-
57-
}
58-
59-
public static function getDefaultFactoriesAllDrivers(){
60-
61-
return array(
62-
63-
'dataInserterFilterQueue'=> function(\TestDbAcle\ServiceLocator $serviceLocator) {
64-
$filterQueue = $serviceLocator->get('filterQueue');
65-
$filterQueue->addRowFilter(new Filter\AddDefaultValuesRowFilter($serviceLocator->get('tableList')));
66-
return $filterQueue;
67-
},
68-
'filterQueue'=> function(\TestDbAcle\ServiceLocator $serviceLocator) {
69-
$filterQueue = new Filter\FilterQueue();
70-
return $filterQueue;
71-
},
72-
'upsertBuilderFactory' => function(\TestDbAcle\ServiceLocator $serviceLocator) {
73-
return new \TestDbAcle\Db\DataInserter\Factory\UpsertBuilderFactory($serviceLocator->get('pdoFacade'));
74-
},
75-
76-
'tableList' => '\TestDbAcle\Db\TableList',
77-
'parser' => '\TestDbAcle\Psv\PsvParser',
78-
79-
);
80-
}
81-
82-
public static function getDefaultFactoriesMysql(){
83-
84-
return array(
85-
'dataInserter'=> function(\TestDbAcle\ServiceLocator $serviceLocator) {
86-
$dataInserter = new Db\DataInserter\DataInserter($serviceLocator->get('pdoFacade'), $serviceLocator->get('upsertBuilderFactory'));
87-
$dataInserter->addUpsertListener(new Db\DataInserter\Listeners\MysqlZeroPKListener($serviceLocator->get('pdoFacade'), $serviceLocator->get('tableList')));
88-
return $dataInserter;
89-
},
90-
'pdoFacade'=> function(\TestDbAcle\ServiceLocator $serviceLocator) {
91-
$pdoFacade = new Db\Mysql\Pdo\PdoFacade($serviceLocator->get('pdo'));
92-
$pdoFacade->disableForeignKeyChecks();
93-
$pdoFacade->enableExceptions();
94-
return $pdoFacade;
95-
},
96-
'tableFactory' => function(\TestDbAcle\ServiceLocator $serviceLocator) {
97-
return new \TestDbAcle\Db\Mysql\TableFactory($serviceLocator->get('pdoFacade'));
98-
},
99-
100-
);
101-
}
102-
103-
public static function getDefaultFactoriesSqlite(){
104-
105-
return array(
106-
'dataInserter'=> function(\TestDbAcle\ServiceLocator $serviceLocator) {
107-
return new Db\DataInserter\DataInserter($serviceLocator->get('pdoFacade'), $serviceLocator->get('upsertBuilderFactory'));
108-
},
109-
110-
'pdoFacade'=> function(\TestDbAcle\ServiceLocator $serviceLocator) {
111-
$pdoFacade = new \TestDbAcle\Db\Sqlite\Pdo\PdoFacade($serviceLocator->get('pdo'));
112-
$pdoFacade->disableForeignKeyChecks();
113-
$pdoFacade->enableExceptions();
114-
return $pdoFacade;
115-
},
116-
'tableFactory' => function(\TestDbAcle\ServiceLocator $serviceLocator) {
117-
return new \TestDbAcle\Db\Sqlite\TableFactory($serviceLocator->get('pdoFacade'));
118-
},
119-
120-
);
121-
}
12254

12355
}

tests/TestDbAcle/ServiceLocatorTest.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function setup()
2020
));
2121
}
2222

23-
function test_SmokeTest()
23+
function xtest_SmokeTest()
2424
{
2525

2626
$foo1 = $this->serviceLocator->get('foo');
@@ -45,11 +45,46 @@ function test_SmokeTest()
4545
$this->assertSame($cow1, $field->cow);
4646

4747
$override= new \StdClass();
48-
$this->serviceLocator->set('moo', $override);
48+
$this->serviceLocator->setService('moo', $override);
4949
$this->assertSame($override, $this->serviceLocator->get('moo'), 'services can be overridden');
50-
$this->assertSame($cow1, $this->serviceLocator->get('prototype.moo'), 'overides are shadowed by parent');
50+
51+
5152
$this->assertNull($this->serviceLocator->get('baz'), 'unreckognised services return null');
5253
}
54+
55+
function test_addFactories_areMerged_andExistingSavedAsPrototypes()
56+
{
57+
$this->serviceLocator->addFactories(
58+
array(
59+
'foo' => function(\TestDbAcle\ServiceLocator $serviceLocator){
60+
return 'foo overridden';
61+
},
62+
));
63+
$this->assertEquals('foo overridden', $this->serviceLocator->get('foo'));
64+
$this->assertEquals(new TestClass(), $this->serviceLocator->get('prototype.foo'));
65+
$this->assertEquals(new \TestDbAcleTests\TestDbAcle\Cow(), $this->serviceLocator->get('moo'));
66+
67+
}
68+
69+
function test_addFactories_areMergedTwice_andExistingSavedAsPrototypes()
70+
{
71+
$this->serviceLocator->addFactories(
72+
array(
73+
'foo' => function(\TestDbAcle\ServiceLocator $serviceLocator){
74+
return 'foo overridden';
75+
},
76+
));
77+
$this->serviceLocator->addFactories(
78+
array(
79+
'foo' => function(\TestDbAcle\ServiceLocator $serviceLocator){
80+
return 'foo overridden again';
81+
},
82+
));
83+
84+
$this->assertEquals('foo overridden again', $this->serviceLocator->get('foo'));
85+
$this->assertEquals('foo overridden', $this->serviceLocator->get('prototype.foo'));
86+
$this->assertEquals(new TestClass(), $this->serviceLocator->get('prototype.prototype.foo'));
87+
}
5388
}
5489

5590

tests/TestDbAcle/TestDbAcleTest.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function test_runCommand(){
6060

6161
}
6262

63-
function test_create(){
63+
function test_create_default(){
6464

6565
$expectedTestDbAcle = new \TestDbAcle\TestDbAcle();
6666
$mockPdo = \TestDbAcle\PhpUnit\Mocks\MockablePdo::createMock($this, array('getAttribute'));
@@ -70,8 +70,10 @@ function test_create(){
7070
->with(\PDO::ATTR_DRIVER_NAME)
7171
->will($this->returnValue("mysql"));
7272

73-
$serviceLocator = new \TestDbAcle\ServiceLocator(\TestDbAcle\TestDbAcle::getDefaultFactories("mysql"));
74-
$serviceLocator->set('pdo', $mockPdo);
73+
$defaultFactories = new \TestDbAcle\Config\DefaultFactories();
74+
75+
$serviceLocator = new \TestDbAcle\ServiceLocator($defaultFactories->getFactories("mysql"));
76+
$serviceLocator->setService('pdo', $mockPdo);
7577

7678
$expectedTestDbAcle->setServiceLocator($serviceLocator);
7779
$testDbAcle = \TestDbAcle\TestDbAcle::create($mockPdo);
@@ -95,7 +97,34 @@ function test_create_with_defaultOverriden(){
9597

9698
}
9799

98-
100+
function test_create_customFactoryContainer(){
101+
102+
$expectedTestDbAcle = new \TestDbAcle\TestDbAcle();
103+
$mockPdo = \TestDbAcle\PhpUnit\Mocks\MockablePdo::createMock($this, array('getAttribute'));
104+
105+
$mockPdo->expects($this->once())
106+
->method('getAttribute')
107+
->with(\PDO::ATTR_DRIVER_NAME)
108+
->will($this->returnValue("mysql"));
109+
110+
$expectedConfig = array(
111+
'foo' => function(){
112+
return "moo";
113+
}
114+
);
115+
116+
$factories = \Mockery::mock('\TestDbAcle\Config\FactoriesInterface');
117+
$factories->shouldReceive("getFactories")->with("mysql")->andReturn($expectedConfig);
118+
119+
$serviceLocator = new \TestDbAcle\ServiceLocator($expectedConfig);
120+
$serviceLocator->setService('pdo', $mockPdo);
121+
122+
$expectedTestDbAcle->setServiceLocator($serviceLocator);
123+
$testDbAcle = \TestDbAcle\TestDbAcle::create($mockPdo, array(), $factories);
124+
125+
$this->assertEquals($expectedTestDbAcle, $testDbAcle);
126+
127+
}
99128

100129

101130
}

0 commit comments

Comments
 (0)