Skip to content

Commit 13aa774

Browse files
committed
Add last required tests and suppress warnings for external code usages
1 parent fc9b6c1 commit 13aa774

File tree

8 files changed

+495
-14
lines changed

8 files changed

+495
-14
lines changed

src/Script/Helper/Filesystem.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tooly\Script\Helper;
44

55
use Composer\Util\Filesystem as ComposerFileSystem;
6+
use Composer\Util\Silencer;
67

78
/**
89
* @package Tooly\Script\Helper
@@ -37,15 +38,16 @@ public function isFileAlreadyExist($filename)
3738
* @param string $content
3839
*
3940
* @return bool
41+
* @SuppressWarnings(PHPMD.StaticAccess)
4042
*/
4143
public function createFile($filename, $content)
4244
{
4345
if (false === $this->createDirectory($filename)) {
4446
return false;
4547
}
4648

47-
file_put_contents($filename, $content);
48-
chmod($filename, 0755);
49+
Silencer::call('file_put_contents', $filename, $content);
50+
Silencer::call('chmod', $filename, 0755);
4951

5052
return true;
5153
}
@@ -79,10 +81,22 @@ public function removeDirectory($directory)
7981
return $this->filesystem->removeDirectoryPhp($directory);
8082
}
8183

84+
/**
85+
* @param string $file
86+
*
87+
* @return bool
88+
* @SuppressWarnings(PHPMD.StaticAccess)
89+
*/
90+
public function remove($file)
91+
{
92+
return Silencer::call('unlink', $file);
93+
}
94+
8295
/**
8396
* @param string $filename
8497
*
8598
* @return bool
99+
* @SuppressWarnings(PHPMD.StaticAccess)
86100
*/
87101
private function createDirectory($filename)
88102
{
@@ -92,6 +106,6 @@ private function createDirectory($filename)
92106
return true;
93107
}
94108

95-
return mkdir($directory, 0777, true);
109+
return Silencer::call('mkdir', $directory, 0777, true);
96110
}
97111
}

src/Script/Processor.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Tooly\Script;
44

55
use Composer\IO\IOInterface;
6-
use Composer\Util\Silencer;
76
use Tooly\Script\Decision\DecisionInterface;
87
use Tooly\Script\Decision\DoReplaceDecision;
98
use Tooly\Script\Decision\FileAlreadyExistDecision;
@@ -120,8 +119,6 @@ private function getDecisions()
120119
/**
121120
* @param string $dir
122121
* @param array $excludeToolNames
123-
*
124-
* @SuppressWarnings(PHPMD.StaticAccess)
125122
*/
126123
private function removeFromDir($dir, array $excludeToolNames = [])
127124
{
@@ -136,7 +133,7 @@ private function removeFromDir($dir, array $excludeToolNames = [])
136133
continue;
137134
}
138135

139-
Silencer::call('unlink', $path);
136+
$this->helper->getFilesystem()->remove($path);
140137
}
141138
}
142139
}

tests/Script/ConfigurationTest.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
1515
{
1616
public function testIfNoToolsSetEmptyToolSetIsGiven()
1717
{
18-
$configuration = new Configuration($this->getPreparedComposerInstance([], ''), new Mode(true, false));
18+
$composer = $this->getPreparedComposerInstance([], '');
19+
$configuration = new Configuration($composer, new Mode);
20+
1921
$this->assertCount(0, $configuration->getTools());
2022
}
2123

@@ -29,13 +31,17 @@ public function testCanGetCorrectToolSet()
2931
]
3032
];
3133

32-
$configuration = new Configuration($this->getPreparedComposerInstance($extra, ''), new Mode);
34+
$composer = $this->getPreparedComposerInstance($extra, '');
35+
$configuration = new Configuration($composer, new Mode);
36+
3337
$this->assertCount(1, $configuration->getTools());
3438
}
3539

3640
public function testCanCheckDevMode()
3741
{
38-
$configuration = new Configuration($this->getPreparedComposerInstance([], ''), new Mode);
42+
$composer = $this->getPreparedComposerInstance([], '');
43+
$configuration = new Configuration($composer, new Mode);
44+
3945
$this->assertTrue($configuration->isDevMode());
4046
}
4147

@@ -44,13 +50,17 @@ public function testCanSetDevMode()
4450
$mode = new Mode;
4551
$mode->setNoDev();
4652

47-
$configuration = new Configuration($this->getPreparedComposerInstance([], ''), $mode);
53+
$composer = $this->getPreparedComposerInstance([], '');
54+
$configuration = new Configuration($composer, $mode);
55+
4856
$this->assertFalse($configuration->isDevMode());
4957
}
5058

5159
public function testCanCheckInteractiveMode()
5260
{
53-
$configuration = new Configuration($this->getPreparedComposerInstance([], ''), new Mode);
61+
$composer = $this->getPreparedComposerInstance([], '');
62+
$configuration = new Configuration($composer, new Mode);
63+
5464
$this->assertTrue($configuration->isInteractiveMode());
5565
}
5666

@@ -59,15 +69,35 @@ public function testCanSetInteractiveMode()
5969
$mode = new Mode;
6070
$mode->setNonInteractive();
6171

62-
$configuration = new Configuration($this->getPreparedComposerInstance([], ''), $mode);
72+
$composer = $this->getPreparedComposerInstance([], '');
73+
$configuration = new Configuration($composer, $mode);
74+
6375
$this->assertFalse($configuration->isInteractiveMode());
6476
}
6577

78+
public function testCanGetCorrectComposerBinDirectory()
79+
{
80+
$binDir = __DIR__ . '/../../vendor/bin';
81+
82+
$composer = $this->getPreparedComposerInstance([], $binDir);
83+
$configuration = new Configuration($composer, new Mode);
84+
85+
$this->assertEquals($binDir, $configuration->getComposerBinDirectory());
86+
}
87+
88+
public function testCanGetCorrectBinDir()
89+
{
90+
$composer = $this->getPreparedComposerInstance([], '');
91+
$configuration = new Configuration($composer, new Mode);
92+
93+
$this->assertEquals(realpath(__DIR__ . '/../../bin'), $configuration->getBinDirectory());
94+
}
95+
6696
/**
6797
* @param mixed $extra
6898
* @param mixed $binDir
6999
*
70-
* @return \PHPUnit_Framework_MockObject_MockObject
100+
* @return Composer
71101
*/
72102
private function getPreparedComposerInstance($extra, $binDir)
73103
{
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace Tooly\Tests\Script\Decision;
4+
5+
use Composer\IO\ConsoleIO;
6+
use Symfony\Component\Console\Helper\HelperSet;
7+
use Symfony\Component\Console\Helper\QuestionHelper;
8+
use Symfony\Component\Console\Input\ArrayInput;
9+
use Symfony\Component\Console\Output\StreamOutput;
10+
use Tooly\Factory\ToolFactory;
11+
use Tooly\Script\Decision\DoReplaceDecision;
12+
use Tooly\Script\Helper\Filesystem;
13+
14+
/**
15+
* @package Tooly\Tests\Script\Decision
16+
*/
17+
class DoReplaceDecisionTest extends DecisionTestCase
18+
{
19+
private $io;
20+
21+
private $input;
22+
23+
private $output;
24+
25+
/**
26+
* @var HelperSet
27+
*/
28+
private $helperSet;
29+
30+
public function setUp()
31+
{
32+
parent::setUp();
33+
34+
$this->input = new ArrayInput([]);
35+
$this->output = new StreamOutput(fopen('php://memory', 'w', false));
36+
$this->helperSet = new HelperSet;
37+
38+
$this->io = new ConsoleIO($this->input, $this->output, $this->helperSet);
39+
}
40+
41+
public function testIfFileNotExistReturnsTrue()
42+
{
43+
$filesystem = $this
44+
->getMockBuilder(Filesystem::class)
45+
->disableOriginalConstructor()
46+
->getMock();
47+
48+
$filesystem
49+
->expects($this->once())
50+
->method('isFileAlreadyExist')
51+
->willReturn(false);
52+
53+
$this->helper
54+
->expects($this->once())
55+
->method('getFilesystem')
56+
->willReturn($filesystem);
57+
58+
$decision = new DoReplaceDecision($this->configuration, $this->helper, $this->io);
59+
$tool = ToolFactory::createTool('tool', __DIR__, []);
60+
61+
$this->assertTrue($decision->canProceed($tool));
62+
}
63+
64+
public function testIfFileExistReturnsForceReplaceValue()
65+
{
66+
$filesystem = $this
67+
->getMockBuilder(Filesystem::class)
68+
->disableOriginalConstructor()
69+
->getMock();
70+
71+
$filesystem
72+
->expects($this->exactly(2))
73+
->method('isFileAlreadyExist')
74+
->willReturn(true);
75+
76+
$this->helper
77+
->expects($this->exactly(2))
78+
->method('getFilesystem')
79+
->willReturn($filesystem);
80+
81+
$decision = new DoReplaceDecision($this->configuration, $this->helper, $this->io);
82+
83+
$tool = ToolFactory::createTool('tool', __DIR__, []);
84+
$this->assertFalse($decision->canProceed($tool));
85+
86+
$tool->activateForceReplace();
87+
$this->assertTrue($decision->canProceed($tool));
88+
}
89+
90+
public function testInteractiveModeWithAnswerReturnsThatValue()
91+
{
92+
$sayNo = fopen('php://memory', 'r+', false);
93+
fputs($sayNo, 'no');
94+
rewind($sayNo);
95+
96+
$this->helperSet->set(new QuestionHelper);
97+
$this->helperSet
98+
->get('question')
99+
->setInputStream($sayNo);
100+
101+
$filesystem = $this
102+
->getMockBuilder(Filesystem::class)
103+
->disableOriginalConstructor()
104+
->getMock();
105+
106+
$filesystem
107+
->expects($this->exactly(2))
108+
->method('isFileAlreadyExist')
109+
->willReturn(true);
110+
111+
$this->helper
112+
->expects($this->exactly(2))
113+
->method('getFilesystem')
114+
->willReturn($filesystem);
115+
116+
$this->configuration
117+
->method('isInteractiveMode')
118+
->willReturn(true);
119+
120+
$decision = new DoReplaceDecision($this->configuration, $this->helper, $this->io);
121+
$tool = ToolFactory::createTool('tool', __DIR__, []);
122+
123+
$this->assertFalse($decision->canProceed($tool));
124+
125+
$sayYes = fopen('php://memory', 'r+', false);
126+
fputs($sayYes, 'yes');
127+
rewind($sayYes);
128+
129+
$this->helperSet
130+
->get('question')
131+
->setInputStream($sayYes);
132+
133+
$this->assertTrue($decision->canProceed($tool));
134+
}
135+
136+
public function testCanGetReason()
137+
{
138+
$decision = new DoReplaceDecision($this->configuration, $this->helper, $this->io);
139+
$this->assertRegExp('/info/', $decision->getReason());
140+
}
141+
}

tests/Script/HelperTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,16 @@ public function testCanSymlinkAFile()
9898
$helper = new Helper($filesystem, new Downloader, new Verifier);
9999
$this->assertTrue($helper->getFilesystem()->symlinkFile('foo', 'bar'));
100100
}
101+
102+
public function testCanGetDownloader()
103+
{
104+
$helper = new Helper(new Filesystem, new Downloader, new Verifier);
105+
$this->assertInstanceOf(Downloader::class, $helper->getDownloader());
106+
}
107+
108+
public function testCanGetVerifier()
109+
{
110+
$helper = new Helper(new Filesystem, new Downloader, new Verifier);
111+
$this->assertInstanceOf(Verifier::class, $helper->getVerifier());
112+
}
101113
}

0 commit comments

Comments
 (0)