Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit c421e74

Browse files
committed
Merge branch 'hotfix/config-writing' into develop
Close #155
2 parents 5238c02 + 981ee97 commit c421e74

File tree

4 files changed

+89
-55
lines changed

4 files changed

+89
-55
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"phpunit/phpunit": "^4.6 || ^5.2.10",
2323
"ocramius/proxy-manager": "^1.0 || ^2.0",
2424
"squizlabs/php_codesniffer": "^2.5.1",
25-
"phpbench/phpbench": "^0.10.0"
25+
"phpbench/phpbench": "^0.10.0",
26+
"mikey179/vfsStream": "^1.6"
2627
},
2728
"suggest": {
2829
"ocramius/proxy-manager": "ProxyManager 1.* to handle lazy initialization of services",

doc/book/console-tools.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,17 @@ Arguments:
1919
<className> Name of the class to reflect and for which to generate
2020
dependency configuration.
2121

22-
Generates to STDOUT a replacement configuration file containing dependency
23-
configuration for the named class with which to configure the
24-
ConfigAbstractFactory.
22+
23+
Reads the provided configuration file, and injects it with
24+
ConfigAbstractFactory dependency configuration for the provided class
25+
name, writing the changes back to the file.
2526
```
2627

2728
This utility will generate dependency configuration for the named class for use
2829
with the [ConfigAbstractFactory](config-abstract-factory.md). When doing so, it
2930
will read the named configuration file, and merge any configuration it generates
30-
with the return values of that file, emitting the updated version to STDOUT.
31-
This allows you to pipe it back to the original:
32-
33-
```bash
34-
$ ./vendor/bin/generate-deps-for-config-factory \
35-
> ./config/autoload/dependencies.local.php \
36-
> "Application\\Model\\AlbumModel" > ./config/autoload/dependencies.local.php
37-
```
38-
39-
Alternately, you can pipe them to a new file, so that you can diff the original
40-
to the generated file.
31+
with the return values of that file, writing the changes back to the original
32+
file.
4133

4234
## generate-factory-for-class
4335

src/Tool/ConfigDumperCommand.php

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class ConfigDumperCommand
3131
<info><className></info> Name of the class to reflect and for which to generate
3232
dependency configuration.
3333
34-
Generates to STDOUT a replacement configuration file containing dependency
35-
configuration for the named class with which to configure the
36-
ConfigAbstractFactory.
34+
Reads the provided configuration file, and injects it with
35+
ConfigAbstractFactory dependency configuration for the provided class
36+
name, writing the changes back to the file.
3737
EOH;
3838

3939
/**
@@ -90,7 +90,12 @@ public function __invoke(array $args)
9090
return 1;
9191
}
9292

93-
$this->helper->write($dumper->dumpConfigFile($config), false);
93+
file_put_contents($arguments->configFile, $dumper->dumpConfigFile($config));
94+
95+
$this->helper->writeLine(sprintf(
96+
'<info>[DONE]</info> Changes written to %s',
97+
$arguments->configFile
98+
));
9499
return 0;
95100
}
96101

@@ -101,45 +106,46 @@ public function __invoke(array $args)
101106
private function parseArgs(array $args)
102107
{
103108
if (! count($args)) {
104-
return $this->createArguments(self::COMMAND_HELP);
109+
return $this->createHelpArgument();
105110
}
106111

107112
$arg1 = array_shift($args);
108113

109114
if (in_array($arg1, ['-h', '--help', 'help'], true)) {
110-
return $this->createArguments(self::COMMAND_HELP);
115+
return $this->createHelpArgument();
111116
}
112117

113118
if (! count($args)) {
114-
return $this->createArguments(self::COMMAND_ERROR, null, null, 'Missing class name');
119+
return $this->createErrorArgument('Missing class name');
115120
}
116121

117122
if (! file_exists($arg1)) {
118-
return $this->createArguments(self::COMMAND_ERROR, null, null, sprintf(
123+
return $this->createErrorArgument(sprintf(
119124
'Cannot find configuration file at path "%s"',
120125
$arg1
121126
));
122127
}
123128

124-
$config = require $arg1;
129+
$configFile = $arg1;
130+
$config = require $configFile;
125131

126132
if (! is_array($config)) {
127-
return $this->createArguments(self::COMMAND_ERROR, null, null, sprintf(
133+
return $this->createErrorArgument(sprintf(
128134
'Configuration at path "%s" does not return an array.',
129-
$arg1
135+
$configFile
130136
));
131137
}
132138

133139
$class = array_shift($args);
134140

135141
if (! class_exists($class)) {
136-
return $this->createArguments(self::COMMAND_ERROR, null, null, sprintf(
142+
return $this->createErrorArgument(sprintf(
137143
'Class "%s" does not exist or could not be autoloaded.',
138144
$class
139145
));
140146
}
141147

142-
return $this->createArguments(self::COMMAND_DUMP, $config, $class);
148+
return $this->createArguments(self::COMMAND_DUMP, $configFile, $config, $class);
143149
}
144150

145151
/**
@@ -156,18 +162,41 @@ private function help($resource = STDOUT)
156162

157163
/**
158164
* @param string $command
159-
* @param array|null $config Parsed configuration.
160-
* @param string|null $class Name of class to reflect.
161-
* @param string|null $message Error message, if any.
165+
* @param string $configFile File from which config originates, and to
166+
* which it will be written.
167+
* @param array $config Parsed configuration.
168+
* @param string $class Name of class to reflect.
169+
* @return \stdClass
170+
*/
171+
private function createArguments($command, $configFile, $config, $class)
172+
{
173+
return (object) [
174+
'command' => $command,
175+
'configFile' => $configFile,
176+
'config' => $config,
177+
'class' => $class,
178+
];
179+
}
180+
181+
/**
182+
* @param string $message
183+
* @return \stdClass
184+
*/
185+
private function createErrorArgument($message)
186+
{
187+
return (object) [
188+
'command' => self::COMMAND_ERROR,
189+
'message' => $message,
190+
];
191+
}
192+
193+
/**
162194
* @return \stdClass
163195
*/
164-
private function createArguments($command, $config = null, $class = null, $error = null)
196+
private function createHelpArgument()
165197
{
166198
return (object) [
167-
'command' => $command,
168-
'config' => $config,
169-
'class' => $class,
170-
'message' => $error,
199+
'command' => self::COMMAND_HELP,
171200
];
172201
}
173202
}

test/Tool/ConfigDumperCommandTest.php

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88
namespace ZendTest\ServiceManager\Tool;
99

10+
use org\bovigo\vfs\vfsStream;
11+
use org\bovigo\vfs\vfsStreamDirectory;
1012
use PHPUnit_Framework_TestCase as TestCase;
1113
use Prophecy\Argument;
14+
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
1215
use Zend\ServiceManager\Tool\ConfigDumperCommand;
1316
use Zend\Stdlib\ConsoleHelper;
1417
use ZendTest\ServiceManager\TestAsset\InvokableObject;
@@ -19,6 +22,7 @@ class ConfigDumperCommandTest extends TestCase
1922
{
2023
public function setUp()
2124
{
25+
$this->configDir = vfsStream::setup('project');
2226
$this->helper = $this->prophesize(ConsoleHelper::class);
2327
$this->command = new ConfigDumperCommand(ConfigDumperCommand::class, $this->helper->reveal());
2428
}
@@ -84,7 +88,10 @@ public function testEmitsErrorWhenConfigurationFileNotFound()
8488
public function testEmitsErrorWhenConfigurationFileDoesNotReturnArray()
8589
{
8690
$command = $this->command;
87-
$config = realpath(__DIR__ . '/../TestAsset/config/invalid.config.php');
91+
vfsStream::newFile('config/invalid.config.php')
92+
->at($this->configDir)
93+
->setContent(file_get_contents(realpath(__DIR__ . '/../TestAsset/config/invalid.config.php')));
94+
$config = vfsStream::url('project/config/invalid.config.php');
8895
$this->assertErrorRaised('Configuration at path "' . $config . '" does not return an array.');
8996
$this->assertHelp(STDERR);
9097
$this->assertEquals(1, $command([$config, 'Not\A\Real\Class']));
@@ -93,7 +100,10 @@ public function testEmitsErrorWhenConfigurationFileDoesNotReturnArray()
93100
public function testEmitsErrorWhenClassDoesNotExist()
94101
{
95102
$command = $this->command;
96-
$config = realpath(__DIR__ . '/../TestAsset/config/test.config.php');
103+
vfsStream::newFile('config/test.config.php')
104+
->at($this->configDir)
105+
->setContent(file_get_contents(realpath(__DIR__ . '/../TestAsset/config/test.config.php')));
106+
$config = vfsStream::url('project/config/test.config.php');
97107
$this->assertErrorRaised('Class "Not\\A\\Real\\Class" does not exist or could not be autoloaded.');
98108
$this->assertHelp(STDERR);
99109
$this->assertEquals(1, $command([$config, 'Not\A\Real\Class']));
@@ -102,7 +112,10 @@ public function testEmitsErrorWhenClassDoesNotExist()
102112
public function testEmitsErrorWhenUnableToCreateConfiguration()
103113
{
104114
$command = $this->command;
105-
$config = realpath(__DIR__ . '/../TestAsset/config/test.config.php');
115+
vfsStream::newFile('config/test.config.php')
116+
->at($this->configDir)
117+
->setContent(file_get_contents(realpath(__DIR__ . '/../TestAsset/config/test.config.php')));
118+
$config = vfsStream::url('project/config/test.config.php');
106119
$this->assertErrorRaised('Unable to create config for "' . ObjectWithScalarDependency::class . '":');
107120
$this->assertHelp(STDERR);
108121
$this->assertEquals(1, $command([$config, ObjectWithScalarDependency::class]));
@@ -111,24 +124,23 @@ public function testEmitsErrorWhenUnableToCreateConfiguration()
111124
public function testEmitsConfigFileToStdoutWhenSuccessful()
112125
{
113126
$command = $this->command;
114-
$config = realpath(__DIR__ . '/../TestAsset/config/test.config.php');
127+
vfsStream::newFile('config/test.config.php')
128+
->at($this->configDir)
129+
->setContent(file_get_contents(realpath(__DIR__ . '/../TestAsset/config/test.config.php')));
130+
$config = vfsStream::url('project/config/test.config.php');
115131

116-
$this->helper->write(Argument::that(function ($config) {
117-
if (! strstr($config, 'return [')) {
118-
return false;
119-
}
120-
121-
if (! strstr($config, SimpleDependencyObject::class . '::class => [')) {
122-
return false;
123-
}
124-
125-
if (! strstr($config, InvokableObject::class . '::class => [')) {
126-
return false;
127-
}
128-
129-
return true;
130-
}), false)->shouldBeCalled();
132+
$this->helper->writeLine('<info>[DONE]</info> Changes written to ' . $config)->shouldBeCalled();
131133

132134
$this->assertEquals(0, $command([$config, SimpleDependencyObject::class]));
135+
136+
$generated = include $config;
137+
$this->assertInternalType('array', $generated);
138+
$this->assertArrayHasKey(ConfigAbstractFactory::class, $generated);
139+
$factoryConfig = $generated[ConfigAbstractFactory::class];
140+
$this->assertInternalType('array', $factoryConfig);
141+
$this->assertArrayHasKey(SimpleDependencyObject::class, $factoryConfig);
142+
$this->assertArrayHasKey(InvokableObject::class, $factoryConfig);
143+
$this->assertContains(InvokableObject::class, $factoryConfig[SimpleDependencyObject::class]);
144+
$this->assertEquals([], $factoryConfig[InvokableObject::class]);
133145
}
134146
}

0 commit comments

Comments
 (0)