1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information please see |
17
|
|
|
* <http://phing.info>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Phing\Task\Optional; |
21
|
|
|
|
22
|
|
|
use ComposerTask; |
23
|
|
|
use Phing\Io\FileSystem; |
24
|
|
|
use Phing\Project; |
25
|
|
|
use Phing\Type\CommandlineArgument; |
26
|
|
|
use ReflectionMethod; |
27
|
|
|
use ReflectionProperty; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Test class for the ComposerTask. |
31
|
|
|
* |
32
|
|
|
* @author Nuno Costa <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class ComposerTaskTest extends \PHPUnit\Framework\TestCase |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var ComposerTask |
38
|
|
|
*/ |
39
|
|
|
protected $object; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Sets up the fixture, for example, opens a network connection. |
43
|
|
|
* This method is called before a test is executed. |
44
|
|
|
*/ |
45
|
|
|
protected function setUp(): void |
46
|
|
|
{ |
47
|
|
|
$this->object = new ComposerTask(); |
48
|
|
|
$this->object->setProject(new Project()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Tears down the fixture, for example, closes a network connection. |
53
|
|
|
* This method is called after a test is executed. |
54
|
|
|
*/ |
55
|
|
|
protected function tearDown(): void |
56
|
|
|
{ |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @covers ComposerTask::setCommand |
61
|
|
|
* @covers ComposerTask::getCommand |
62
|
|
|
*/ |
63
|
|
|
public function testSetGetCommand() |
64
|
|
|
{ |
65
|
|
|
$o = $this->object; |
66
|
|
|
$o->setCommand('foo'); |
67
|
|
|
$this->assertEquals('foo', $o->getCommand()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @covers ComposerTask::getPhp |
72
|
|
|
* @covers ComposerTask::setPhp |
73
|
|
|
*/ |
74
|
|
|
public function testSetGetPhp() |
75
|
|
|
{ |
76
|
|
|
$o = $this->object; |
77
|
|
|
$o->setPhp('foo'); |
78
|
|
|
$this->assertEquals('foo', $o->getPhp()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @covers ComposerTask::setComposer |
83
|
|
|
*/ |
84
|
|
|
public function testSetComposer() |
85
|
|
|
{ |
86
|
|
|
$composer = 'foobar'; |
87
|
|
|
$o = $this->object; |
88
|
|
|
$o->setComposer($composer); |
89
|
|
|
|
90
|
|
|
$prop = new ReflectionProperty('ComposerTask', 'composer'); |
91
|
|
|
$prop->setAccessible(true); |
92
|
|
|
|
93
|
|
|
$this->assertEquals($composer, $prop->getValue($o)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @covers ComposerTask::getComposer |
98
|
|
|
*/ |
99
|
|
|
public function testGetComposerNotOnPath() |
100
|
|
|
{ |
101
|
|
|
$composer = 'bar'; |
102
|
|
|
$o = $this->object; |
103
|
|
|
|
104
|
|
|
$orgPath = getenv("PATH"); |
105
|
|
|
|
106
|
|
|
$prop = new ReflectionProperty('ComposerTask', 'composer'); |
107
|
|
|
$prop->setAccessible(true); |
108
|
|
|
$prop->setValue($o, $composer); |
109
|
|
|
|
110
|
|
|
putenv("PATH=/foo/bar"); |
111
|
|
|
|
112
|
|
|
$pathComposer = $o->getComposer(); |
113
|
|
|
|
114
|
|
|
putenv("PATH=$orgPath"); |
115
|
|
|
|
116
|
|
|
$this->assertEquals($composer, $pathComposer); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @covers ComposerTask::getComposer |
121
|
|
|
*/ |
122
|
|
|
public function testGetComposerFromPath() |
123
|
|
|
{ |
124
|
|
|
$composer = 'foo'; |
125
|
|
|
$o = $this->object; |
126
|
|
|
$o->setComposer($composer); |
127
|
|
|
|
128
|
|
|
$testPath = PHING_TEST_BASE . '/etc/tasks/ext/composer'; |
129
|
|
|
$orgPath = getenv("PATH"); |
130
|
|
|
|
131
|
|
|
$pathSeparator = FileSystem::getFileSystem()->getPathSeparator(); |
132
|
|
|
putenv("PATH=$testPath$pathSeparator$orgPath"); |
133
|
|
|
|
134
|
|
|
$pathComposer = $o->getComposer(); |
135
|
|
|
|
136
|
|
|
putenv("PATH=$orgPath"); |
137
|
|
|
|
138
|
|
|
// The composer found shouldn't be the one we set |
139
|
|
|
$this->assertNotEquals($composer, $pathComposer); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @covers ComposerTask::createArg |
144
|
|
|
*/ |
145
|
|
|
public function testCreateArg() |
146
|
|
|
{ |
147
|
|
|
$o = $this->object; |
148
|
|
|
$arg = $o->createArg(); |
149
|
|
|
$this->assertInstanceOf(CommandlineArgument::class, $arg); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testMultipleCalls() |
153
|
|
|
{ |
154
|
|
|
$o = $this->object; |
155
|
|
|
$o->setPhp('php'); |
156
|
|
|
$o->setCommand('install'); |
157
|
|
|
$o->createArg()->setValue('--dry-run'); |
158
|
|
|
$composer = $o->getComposer(); |
159
|
|
|
$method = new ReflectionMethod('ComposerTask', 'prepareCommandLine'); |
160
|
|
|
$method->setAccessible(true); |
161
|
|
|
$this->assertEquals('php ' . $composer . ' install --dry-run', (string) $method->invoke($o)); |
162
|
|
|
$o->setCommand('update'); |
163
|
|
|
$o->createArg()->setValue('--dev'); |
164
|
|
|
$this->assertEquals('php ' . $composer . ' update --dev', (string) $method->invoke($o)); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|