Skip to content

Commit dd6b089

Browse files
author
Jan-Marten de Boer
committed
Merge branch 'master' of bitbucket.org:mediactbv/mediact-composer-file-installer
* 'master' of bitbucket.org:mediactbv/mediact-composer-file-installer: Extract file installer from deployer package
2 parents c2b8b4a + 07f3d68 commit dd6b089

File tree

3 files changed

+234
-2
lines changed

3 files changed

+234
-2
lines changed

composer.json

+14-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,25 @@
2323
],
2424
"require": {
2525
"php": "^7.0",
26-
"composer-plugin-api": "^1.1"
26+
"composer-plugin-api": "^1.1",
27+
"mediact/file-mapping": "^1.0"
2728
},
2829
"require-dev": {
2930
"phpunit/phpunit": "@stable",
3031
"kint-php/kint": "@stable",
3132
"mediact/testing-suite": "@stable",
32-
"composer/composer": "@stable"
33+
"composer/composer": "@stable",
34+
"mikey179/vfsstream": "@stable"
35+
},
36+
"autoload": {
37+
"psr-4": {
38+
"Mediact\\Composer\\": "src"
39+
}
40+
},
41+
"autoload-dev": {
42+
"psr-4": {
43+
"Mediact\\Composer\\Tests\\": "tests"
44+
}
3345
},
3446
"archive": {
3547
"exclude": [

src/FileInstaller.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
namespace Mediact\Composer;
3+
4+
use Composer\IO\IOInterface;
5+
use Mediact\FileMapping\FileMappingInterface;
6+
use Mediact\FileMapping\FileMappingReaderInterface;
7+
use SplFileObject;
8+
9+
class FileInstaller
10+
{
11+
/** @var FileMappingReaderInterface */
12+
private $mappingReader;
13+
14+
/**
15+
* Constructor.
16+
*
17+
* @param FileMappingReaderInterface $mappingReader
18+
*/
19+
public function __construct(FileMappingReaderInterface $mappingReader)
20+
{
21+
$this->mappingReader = $mappingReader;
22+
}
23+
24+
/**
25+
* Install the deployer files.
26+
*
27+
* @param IOInterface $io
28+
*
29+
* @return void
30+
*
31+
* @SuppressWarnings(PHPMD.ShortVariable)
32+
*/
33+
public function install(IOInterface $io)
34+
{
35+
foreach ($this->mappingReader as $mapping) {
36+
if (file_exists($mapping->getDestination())) {
37+
continue;
38+
}
39+
40+
$this->installFile($mapping);
41+
42+
$io->write(
43+
sprintf(
44+
'<info>Installed:</info> %s',
45+
$mapping->getRelativeDestination()
46+
)
47+
);
48+
}
49+
}
50+
51+
/**
52+
* Install the given file if it does not exist.
53+
*
54+
* @param FileMappingInterface $mapping
55+
*
56+
* @return void
57+
*
58+
* @SuppressWarnings(PHPMD.ShortVariable)
59+
*/
60+
public function installFile(FileMappingInterface $mapping)
61+
{
62+
$inputFile = new SplFileObject($mapping->getSource(), 'r');
63+
$targetFile = new SplFileObject($mapping->getDestination(), 'w+');
64+
65+
foreach ($inputFile as $input) {
66+
$targetFile->fwrite($input);
67+
}
68+
}
69+
}

tests/FileInstallerTest.php

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
/**
3+
* Copyright MediaCT. All rights reserved.
4+
* https://www.mediact.nl
5+
*/
6+
7+
namespace Mediact\Composer\Tests;
8+
9+
use Composer\IO\IOInterface;
10+
use Mediact\FileMapping\FileMappingInterface;
11+
use Mediact\FileMapping\FileMappingReaderInterface;
12+
use org\bovigo\vfs\vfsStream;
13+
use PHPUnit\Framework\TestCase;
14+
use PHPUnit_Framework_MockObject_MockObject;
15+
use Mediact\Composer\FileInstaller;
16+
17+
/**
18+
* @coversDefaultClass \MediaCT\Composer\FileInstaller
19+
*/
20+
class FileInstallerTest extends TestCase
21+
{
22+
/**
23+
* @return void
24+
*
25+
* @covers ::__construct
26+
*/
27+
public function testConstructor()
28+
{
29+
/** @noinspection PhpParamsInspection */
30+
$this->assertInstanceOf(
31+
FileInstaller::class,
32+
new FileInstaller(
33+
$this->createMock(FileMappingReaderInterface::class)
34+
)
35+
);
36+
}
37+
38+
/**
39+
* @return void
40+
* @covers ::installFile
41+
*/
42+
public function testInstallFile()
43+
{
44+
/** @var FileMappingReaderInterface $reader */
45+
$reader = $this->createMock(FileMappingReaderInterface::class);
46+
$installer = new FileInstaller($reader);
47+
48+
$fs = vfsStream::setup(
49+
sha1(__METHOD__),
50+
null,
51+
[
52+
'source' => [
53+
'foo.php' => 'Foo'
54+
],
55+
'destination' => []
56+
]
57+
);
58+
59+
/** @var FileMappingInterface|PHPUnit_Framework_MockObject_MockObject $mapping */
60+
$mapping = $this->createMock(FileMappingInterface::class);
61+
$mapping
62+
->expects($this->once())
63+
->method('getSource')
64+
->willReturn(
65+
$fs->getChild('source/foo.php')->url()
66+
);
67+
68+
$mapping
69+
->expects($this->once())
70+
->method('getDestination')
71+
->willReturn(
72+
$fs->getChild('destination')->url() . '/foo.php'
73+
);
74+
75+
$installer->installFile($mapping);
76+
77+
$this->assertStringEqualsFile(
78+
$fs->getChild('destination/foo.php')->url(),
79+
'Foo'
80+
);
81+
}
82+
83+
/**
84+
* @return void
85+
* @covers ::install
86+
*/
87+
public function testInstall()
88+
{
89+
/** @var FileMappingReaderInterface|PHPUnit_Framework_MockObject_MockObject $reader */
90+
$reader = $this->createMock(FileMappingReaderInterface::class);
91+
$installer = new FileInstaller($reader);
92+
93+
$fs = vfsStream::setup(
94+
sha1(__METHOD__),
95+
null,
96+
[
97+
'source' => [
98+
'foo.php' => 'Foo'
99+
],
100+
'destination' => []
101+
]
102+
);
103+
104+
/** @var FileMappingInterface|PHPUnit_Framework_MockObject_MockObject $mapping */
105+
$mapping = $this->createMock(FileMappingInterface::class);
106+
$mapping
107+
->expects($this->once())
108+
->method('getSource')
109+
->willReturn(
110+
$fs->getChild('source/foo.php')->url()
111+
);
112+
113+
$mapping
114+
->expects($this->exactly(3))
115+
->method('getDestination')
116+
->willReturn(
117+
$fs->getChild('destination')->url() . '/foo.php'
118+
);
119+
120+
$mapping
121+
->expects($this->once())
122+
->method('getRelativeDestination')
123+
->willReturn('foo.php');
124+
125+
$reader
126+
->expects($this->exactly(4))
127+
->method('valid')
128+
->willReturnOnConsecutiveCalls(true, false, true, false);
129+
130+
$reader
131+
->expects($this->exactly(2))
132+
->method('current')
133+
->willReturn($mapping);
134+
135+
/** @var IOInterface|PHPUnit_Framework_MockObject_MockObject $io */
136+
$io = $this->createMock(IOInterface::class);
137+
$io
138+
->expects($this->once())
139+
->method('write')
140+
->with($this->isType('string'));
141+
142+
$installer->install($io);
143+
144+
$this->assertStringEqualsFile(
145+
$fs->getChild('destination/foo.php')->url(),
146+
'Foo'
147+
);
148+
149+
$installer->install($io);
150+
}
151+
}

0 commit comments

Comments
 (0)