Skip to content

Commit 1ded9ac

Browse files
author
Alexander Obuhovich
committed
Make working directory sub-folder configurable (was ".svn-buddy" hardcoded before) via DIC
1 parent 7029339 commit 1ded9ac

File tree

5 files changed

+73
-19
lines changed

5 files changed

+73
-19
lines changed

phpunit.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
lowUpperBound="35" highLowerBound="70"/>
2828
</logging>-->
2929

30+
<php>
31+
<server name="working_directory" value=".svn-buddy"/>
32+
</php>
33+
3034
<filter>
3135
<whitelist>
3236
<directory>src/SVNBuddy</directory>

src/SVNBuddy/DIContainer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@ public function __construct(array $values = array())
3737
{
3838
parent::__construct($values);
3939

40-
$this['configFile'] = '{base}/config.json';
40+
$this['config_file'] = '{base}/config.json';
41+
$this['working_directory_sub_folder'] = '.svn-buddy';
4142

42-
$this['working_directory'] = function () {
43-
$working_directory = new WorkingDirectory();
43+
$this['working_directory'] = function ($c) {
44+
$working_directory = new WorkingDirectory($c['working_directory_sub_folder']);
4445

4546
return $working_directory->get();
4647
};
4748

4849
$this['config_editor'] = function ($c) {
49-
return new ConfigEditor(str_replace('{base}', $c['working_directory'], $c['configFile']));
50+
return new ConfigEditor(str_replace('{base}', $c['working_directory'], $c['config_file']));
5051
};
5152

5253
$this['input'] = function () {

src/SVNBuddy/WorkingDirectory.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,37 @@
1616
class WorkingDirectory
1717
{
1818

19+
/**
20+
* Name of sub folder placed in user's home directory.
21+
*
22+
* @var string
23+
*/
24+
private $_subFolder;
25+
26+
/**
27+
* WorkingDirectory constructor.
28+
*
29+
* @param string $sub_folder Sub folder.
30+
*
31+
* @throws ApplicationException When $sub_folder is a path or empty.
32+
*/
33+
public function __construct($sub_folder)
34+
{
35+
if ( !strlen($sub_folder) || strpos($sub_folder, DIRECTORY_SEPARATOR) !== false ) {
36+
throw new ApplicationException('The $sub_folder is a path or empty.');
37+
}
38+
39+
$this->_subFolder = $sub_folder;
40+
}
41+
1942
/**
2043
* Creates (if missing) working directory and returns full path to it.
2144
*
2245
* @return string
2346
*/
2447
public function get()
2548
{
26-
$working_directory = $this->getUserHomeDirectory() . '/.svn-buddy';
49+
$working_directory = $this->getUserHomeDirectory() . '/' . $this->_subFolder;
2750

2851
if ( !file_exists($working_directory) ) {
2952
mkdir($working_directory, 0777, true);

tests/SVNBuddy/WorkingDirectoryAwareTestCase.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,10 @@
1616
abstract class WorkingDirectoryAwareTestCase extends \PHPUnit_Framework_TestCase
1717
{
1818

19-
/**
20-
* Original value of "HOME" environment variable.
21-
*
22-
* @var string
23-
*/
24-
private $_homeDirectoryBackup;
25-
2619
protected function setUp()
2720
{
2821
parent::setUp();
2922

30-
$this->_homeDirectoryBackup = getenv('HOME');
31-
3223
if ( $this->requireWorkingDirectory() ) {
3324
$this->_createTempHomeDirectory();
3425
}
@@ -51,7 +42,13 @@ protected function requireWorkingDirectory()
5142
*/
5243
protected function getWorkingDirectory()
5344
{
54-
$working_directory = new WorkingDirectory();
45+
$sub_folder = array_key_exists('working_directory', $_SERVER) ? $_SERVER['working_directory'] : '';
46+
47+
if ( !strlen($sub_folder) ) {
48+
$this->fail('Please set "working_directory" environment variable before calling ' . __METHOD__ . '.');
49+
}
50+
51+
$working_directory = new WorkingDirectory($sub_folder);
5552

5653
return $working_directory->get();
5754
}
@@ -60,7 +57,7 @@ protected function tearDown()
6057
{
6158
parent::tearDown();
6259

63-
$this->_restoreHomeDirectory($this->_homeDirectoryBackup);
60+
$this->_restoreHomeDirectory();
6461
}
6562

6663
/**
@@ -80,10 +77,17 @@ private function _createTempHomeDirectory()
8077
/**
8178
* Restores original home directory and removes temporary one.
8279
*
83-
* @param string $original_home_directory Directory.
80+
* @return void
81+
* @throws \LogicException When original home directory is empty.
8482
*/
85-
private function _restoreHomeDirectory($original_home_directory)
83+
private function _restoreHomeDirectory()
8684
{
85+
$original_home_directory = $_SERVER['HOME'];
86+
87+
if ( empty($original_home_directory) ) {
88+
throw new \LogicException('Unable to restore empty home directory.');
89+
}
90+
8791
$current_home_directory = getenv('HOME');
8892

8993
if ( $current_home_directory && $current_home_directory != $original_home_directory ) {

tests/SVNBuddy/WorkingDirectoryTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,29 @@
1111
namespace Tests\aik099\SVNBuddy;
1212

1313

14+
use aik099\SVNBuddy\WorkingDirectory;
15+
1416
class WorkingDirectoryTest extends WorkingDirectoryAwareTestCase
1517
{
1618

19+
/**
20+
* @expectedException \aik099\SVNBuddy\Exception\ApplicationException
21+
* @expectedExceptionMessage The $sub_folder is a path or empty.
22+
* @dataProvider incorrectSubFolderDataProvider
23+
*/
24+
public function testCreationWithIncorrectSubFolder($sub_folder)
25+
{
26+
new WorkingDirectory($sub_folder);
27+
}
28+
29+
public function incorrectSubFolderDataProvider()
30+
{
31+
return array(
32+
'empty sub-folder' => array(''),
33+
'path sub-folder' => array('a/b'),
34+
);
35+
}
36+
1737
public function testWorkingDirectoryCreation()
1838
{
1939
$expected_working_directory = $this->getExpectedWorkingDirectory();
@@ -56,7 +76,9 @@ public function testBrokenWindowsEnvironment()
5676
*/
5777
protected function getExpectedWorkingDirectory()
5878
{
59-
return getenv('HOME') . '/.svn-buddy';
79+
$sub_folder = array_key_exists('working_directory', $_SERVER) ? $_SERVER['working_directory'] : '';
80+
81+
return getenv('HOME') . '/' . $sub_folder;
6082
}
6183

6284
}

0 commit comments

Comments
 (0)