Skip to content

Commit c0ae80e

Browse files
author
Alexander Obuhovich
committed
Simplify "Connector/Connector" class test even more
1 parent 3058bd4 commit c0ae80e

File tree

2 files changed

+70
-140
lines changed

2 files changed

+70
-140
lines changed

src/SVNBuddy/Repository/Connector/Connector.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,29 @@ protected function prepareSvnCommand()
113113
}
114114

115115
/**
116-
* Builds a command
116+
* Builds a command.
117117
*
118-
* @param string $command Command.
118+
* @param string $sub_command Sub command.
119119
* @param string|null $param_string Parameter string.
120120
*
121121
* @return Command
122122
*/
123-
public function getCommand($command, $param_string = null)
123+
public function getCommand($sub_command, $param_string = null)
124124
{
125-
$final_command = $this->buildCommand($command, $param_string);
125+
$command_line = $this->buildCommand($sub_command, $param_string);
126126

127-
$repository_command = new Command(
128-
$this->_processFactory->createProcess($final_command, 1200),
127+
$command = new Command(
128+
$this->_processFactory->createProcess($command_line, 1200),
129129
$this->_io,
130130
$this->_cacheManager
131131
);
132132

133133
if ( isset($this->_nextCommandCacheDuration) ) {
134-
$repository_command->setCacheDuration($this->_nextCommandCacheDuration);
134+
$command->setCacheDuration($this->_nextCommandCacheDuration);
135135
$this->_nextCommandCacheDuration = null;
136136
}
137137

138-
return $repository_command;
138+
return $command;
139139
}
140140

141141
/**
@@ -150,24 +150,28 @@ public function getCommand($command, $param_string = null)
150150
protected function buildCommand($sub_command, $param_string = null)
151151
{
152152
if ( strpos($sub_command, ' ') !== false ) {
153-
throw new \InvalidArgumentException('The "' . $sub_command . '" sub-command contains spaces');
153+
throw new \InvalidArgumentException('The "' . $sub_command . '" sub-command contains spaces.');
154154
}
155155

156-
$final_command = $this->_svnCommand . ' ' . $sub_command;
156+
$command_line = $this->_svnCommand;
157+
158+
if ( !empty($sub_command) ) {
159+
$command_line .= ' ' . $sub_command;
160+
}
157161

158162
if ( !empty($param_string) ) {
159-
$final_command .= ' ' . $param_string;
163+
$command_line .= ' ' . $param_string;
160164
}
161165

162-
$final_command = preg_replace_callback(
166+
$command_line = preg_replace_callback(
163167
'/\{([^\}]*)\}/',
164168
function (array $matches) {
165169
return escapeshellarg($matches[1]);
166170
},
167-
$final_command
171+
$command_line
168172
);
169173

170-
return $final_command;
174+
return $command_line;
171175
}
172176

173177
/**

tests/SVNBuddy/Repository/Connector/ConnectorTest.php

Lines changed: 52 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -73,180 +73,106 @@ protected function setUp()
7373
// Called from "__destruct".
7474
$this->_process->stop();
7575

76-
$with_exceptions = array(
77-
'testCommandThatFails',
78-
'testGetPropertyNotFound',
79-
);
80-
81-
$this->_repositoryConnector = $this->_createRepositoryConnector(
82-
'',
83-
'',
84-
in_array($this->getName(false), $with_exceptions) ? null : false
85-
);
76+
$this->_repositoryConnector = $this->_createRepositoryConnector('', '');
8677
}
8778

88-
public function testConfigUsernameUsed()
79+
/**
80+
* @dataProvider baseCommandBuildingDataProvider
81+
*/
82+
public function testBaseCommandBuilding($username, $password, $expected_command)
8983
{
90-
$repository_connector = $this->_createRepositoryConnector('user', '');
84+
$repository_connector = $this->_createRepositoryConnector($username, $password);
9185

92-
$this->_expectCommand('svn --non-interactive --username user --version', 'OK');
93-
$this->assertEquals('OK', $repository_connector->getCommand('--version')->run());
86+
$this->_expectCommand($expected_command, 'OK');
87+
$this->assertEquals('OK', $repository_connector->getCommand('', '--version')->run());
9488
}
9589

96-
public function testConfigPasswordUsed()
90+
public function baseCommandBuildingDataProvider()
9791
{
98-
$repository_connector = $this->_createRepositoryConnector('', 'pass');
99-
100-
$this->_expectCommand('svn --non-interactive --password pass --version', 'OK');
101-
$this->assertEquals('OK', $repository_connector->getCommand('--version')->run());
92+
return array(
93+
'no username, no password' => array('', '', 'svn --non-interactive --version'),
94+
'username, no password' => array('user', '', 'svn --non-interactive --username user --version'),
95+
'no username, password' => array('', 'pass', 'svn --non-interactive --password pass --version'),
96+
'username, password' => array(
97+
'user',
98+
'pass',
99+
'svn --non-interactive --username user --password pass --version',
100+
),
101+
);
102102
}
103103

104-
public function testSimpleCommand()
104+
public function testCommandWithoutSubCommand()
105105
{
106106
$this->_expectCommand('svn --non-interactive --version', 'OK');
107-
$this->assertEquals('OK', $this->_repositoryConnector->getCommand('--version')->run());
108-
}
109-
110-
public function testCommandWithParams()
111-
{
112-
$this->_expectCommand('svn --non-interactive log -r 12', 'OK');
113-
$this->assertEquals('OK', $this->_repositoryConnector->getCommand('log', '-r 12')->run());
114-
}
115-
116-
public function testCommandWithPath()
117-
{
118-
$this->_expectCommand("svn --non-interactive log 'path/to/folder'", 'OK');
119-
$this->assertEquals('OK', $this->_repositoryConnector->getCommand('log', '{path/to/folder}')->run());
107+
$this->assertEquals('OK', $this->_repositoryConnector->getCommand('', '--version')->run());
120108
}
121109

122-
public function testCommandWithPathAndLeadingSlash()
110+
public function testCommandWithoutParams()
123111
{
124-
$this->_expectCommand("svn --non-interactive log '/path/to/folder'", 'OK');
125-
$this->assertEquals('OK', $this->_repositoryConnector->getCommand('log', '{/path/to/folder}')->run());
112+
$this->_expectCommand('svn --non-interactive log', 'OK');
113+
$this->assertEquals('OK', $this->_repositoryConnector->getCommand('log')->run());
126114
}
127115

128-
public function testCommandWithPathAndParams()
116+
/**
117+
* @dataProvider commandWithParamsDataProvider
118+
*/
119+
public function testCommandWithParams($params, $expected_command)
129120
{
130-
$this->_expectCommand("svn --non-interactive log -r 12 'path/to/folder'", 'OK');
131-
$this->assertEquals('OK', $this->_repositoryConnector->getCommand('log', '-r 12 {path/to/folder}')->run());
121+
$this->_expectCommand($expected_command, 'OK');
122+
$this->assertEquals('OK', $this->_repositoryConnector->getCommand('log', $params)->run());
132123
}
133124

134-
public function testCommandThatFails()
125+
public function commandWithParamsDataProvider()
135126
{
136-
$thrown_exception = null;
137-
$this->_expectCommand('svn --non-interactive any', '', false);
138-
139-
try {
140-
$this->_repositoryConnector->getCommand('any')->run();
141-
}
142-
catch ( \Exception $thrown_exception ) {
143-
$this->assertEquals(
144-
'ConsoleHelpers\\SVNBuddy\\Exception\\RepositoryCommandException',
145-
get_class($thrown_exception),
146-
'Exception of correct class was thrown'
147-
);
148-
}
149-
150-
$this->assertNotNull($thrown_exception, 'Exception was thrown when command execution failed');
151-
152-
$error_msg = <<<MSG
153-
Command:
154-
svn --non-interactive any
155-
Error #0:
156-
error output
157-
MSG;
158-
159-
$this->assertEquals($error_msg, $thrown_exception->getMessage());
127+
return array(
128+
'regular param' => array('-r 12', 'svn --non-interactive log -r 12'),
129+
'path param' => array('{path/to/folder}', "svn --non-interactive log 'path/to/folder'"),
130+
'regular and path param' => array(
131+
'-r 12 {path/to/folder}',
132+
"svn --non-interactive log -r 12 'path/to/folder'",
133+
),
134+
);
160135
}
161136

162-
public function testGetPropertyFound()
137+
public function testGetProperty()
163138
{
164-
$this->_expectCommand("svn --non-interactive propget test-p 'the/path'", 'OK');
139+
$this->_expectCommand("svn --non-interactive propget prop-name 'the/path'", 'OK');
165140

166141
$this->assertEquals(
167142
'OK',
168-
$this->_repositoryConnector->getProperty('test-p', 'the/path')
169-
);
170-
}
171-
172-
public function testGetPropertyNotFound()
173-
{
174-
$exception_msg = <<<MSG
175-
Command:
176-
svn --non-interactive propget test-p 'the/path'
177-
Error #0:
178-
error output
179-
MSG;
180-
181-
$this->setExpectedException(
182-
'ConsoleHelpers\\SVNBuddy\\Exception\\RepositoryCommandException',
183-
$exception_msg,
184-
0
143+
$this->_repositoryConnector->getProperty('prop-name', 'the/path')
185144
);
186-
187-
$this->_expectCommand("svn --non-interactive propget test-p 'the/path'", '', false);
188-
189-
$this->_repositoryConnector->getProperty('test-p', 'the/path');
190145
}
191146

192147
/**
193148
* Sets expectation for specific command.
194149
*
195-
* @param string $command Command.
196-
* @param string $output Output.
197-
* @param boolean $is_successful Should command be successful.
150+
* @param string $command Command.
151+
* @param string $output Output.
198152
*
199153
* @return void
200154
*/
201-
private function _expectCommand($command, $output, $is_successful = true)
155+
private function _expectCommand($command, $output)
202156
{
203157
$this->_process->getCommandLine()->willReturn($command)->shouldBeCalled();
204-
205-
$expectation = $this->_process->mustRun(null)->shouldBeCalled();
206-
207-
if ( $is_successful ) {
208-
$this->_process->getOutput()->willReturn($output)->shouldBeCalled();
209-
}
210-
else {
211-
$process = $this->_process;
212-
$expectation->will(function () use ($process) {
213-
$mock_definition = array(
214-
'isSuccessful' => false,
215-
'getExitCode' => 1,
216-
'getExitCodeText' => 'exit code text',
217-
'isOutputDisabled' => false,
218-
'getOutput' => 'normal output',
219-
'getErrorOutput' => 'error output',
220-
);
221-
222-
foreach ( $mock_definition as $method_name => $return_value ) {
223-
$process->{$method_name}()->willReturn($return_value)->shouldBeCalled();
224-
}
225-
226-
throw new ProcessFailedException($process->reveal());
227-
});
228-
}
158+
$this->_process->mustRun(null)->shouldBeCalled();
159+
$this->_process->getOutput()->willReturn($output)->shouldBeCalled();
229160

230161
$this->_processFactory->createProcess($command, 1200)->willReturn($this->_process)->shouldBeCalled();
231162
}
232163

233164
/**
234165
* Creates repository connector.
235166
*
236-
* @param string $svn_username Username.
237-
* @param string $svn_password Password.
238-
* @param boolean $is_verbose Is verbose.
167+
* @param string $username Username.
168+
* @param string $password Password.
239169
*
240170
* @return Connector
241171
*/
242-
private function _createRepositoryConnector($svn_username, $svn_password, $is_verbose = false)
172+
private function _createRepositoryConnector($username, $password)
243173
{
244-
$this->_configEditor->get('repository-connector.username')->willReturn($svn_username)->shouldBeCalled();
245-
$this->_configEditor->get('repository-connector.password')->willReturn($svn_password)->shouldBeCalled();
246-
247-
if ( isset($is_verbose) ) {
248-
$this->_io->isVerbose()->willReturn($is_verbose)->shouldBeCalled();
249-
}
174+
$this->_configEditor->get('repository-connector.username')->willReturn($username)->shouldBeCalled();
175+
$this->_configEditor->get('repository-connector.password')->willReturn($password)->shouldBeCalled();
250176

251177
return new Connector(
252178
$this->_configEditor->reveal(),

0 commit comments

Comments
 (0)