Skip to content

Commit 6274bf0

Browse files
Added run tasks
Signed-off-by: Bastian Schwarz <bastian@codename-php.de>
1 parent 41ac66b commit 6274bf0

8 files changed

Lines changed: 345 additions & 0 deletions

File tree

src/task/run/AbstractRunTask.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\npm\task\run;
19+
20+
use de\codenamephp\deployer\npm\task\AbstractNpmTask;
21+
22+
abstract class AbstractRunTask extends AbstractNpmTask {
23+
24+
abstract public function getScriptName() : string;
25+
26+
public function getNpmCommand() : string {
27+
return 'run';
28+
}
29+
30+
public function getArguments() : array {
31+
return [$this->getNpmCommand(), $this->getScriptName(), ...parent::getArguments()];
32+
}
33+
}

src/task/run/Generic.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\npm\task\run;
19+
20+
use de\codenamephp\deployer\base\functions\All;
21+
use de\codenamephp\deployer\base\functions\iGet;
22+
use de\codenamephp\deployer\command\runner\iRunner;
23+
use de\codenamephp\deployer\command\runner\WithDeployerFunctions;
24+
25+
final class Generic extends AbstractRunTask {
26+
27+
private string $scriptName;
28+
29+
/**
30+
* @var array<int,string>
31+
*/
32+
private array $arguments;
33+
34+
/**
35+
* @param string $scriptName The name of the script to run, e.g. 'build'
36+
* @param array<int,string> $arguments Additional arguments to pass to the script that will be appended to the defaults
37+
* @param iRunner $runner The runner that runs the command
38+
* @param iGet $deployer Deployer function to access the config. Will not be set in the instance
39+
*/
40+
public function __construct(string $scriptName, array $arguments = [], iRunner $runner = new WithDeployerFunctions(), iGet $deployer = new All()) {
41+
$this->scriptName = $scriptName;
42+
$this->arguments = $arguments;
43+
parent::__construct($runner, $deployer);
44+
}
45+
46+
public function getScriptName() : string {
47+
return $this->scriptName;
48+
}
49+
50+
public function getArguments() : array {
51+
return [...parent::getArguments(), ...$this->arguments];
52+
}
53+
}

src/task/run/build/Development.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\npm\task\run\build;
19+
20+
use de\codenamephp\deployer\npm\task\run\AbstractRunTask;
21+
22+
/**
23+
* Runs the build:development script. Remember to create the script in your packages.json
24+
*/
25+
final class Development extends AbstractRunTask {
26+
27+
public function getScriptName() : string {
28+
return 'build:development';
29+
}
30+
}

src/task/run/build/Production.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\npm\task\run\build;
19+
20+
use de\codenamephp\deployer\npm\task\run\AbstractRunTask;
21+
22+
/**
23+
* Runs the build:production script. Remember to define the script in your packages.json
24+
*/
25+
final class Production extends AbstractRunTask {
26+
27+
public function getScriptName() : string {
28+
return 'build';
29+
}
30+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\npm\test\task\run;
19+
20+
use de\codenamephp\deployer\base\functions\iGet;
21+
use de\codenamephp\deployer\command\runner\iRunner;
22+
use de\codenamephp\deployer\npm\task\run\AbstractRunTask;
23+
use PHPUnit\Framework\TestCase;
24+
25+
final class AbstractRunTaskTest extends TestCase {
26+
27+
private AbstractRunTask $sut;
28+
29+
protected function setUp() : void {
30+
parent::setUp();
31+
32+
$runner = $this->createMock(iRunner::class);
33+
$deployer = $this->createMock(iGet::class);
34+
$deployer->method('get')->with('npm:binary', 'npm')->willReturn('npm');
35+
36+
$this->sut = $this->getMockForAbstractClass(AbstractRunTask::class, [$runner, $deployer]);
37+
}
38+
39+
public function test__construct() : void {
40+
$runner = $this->createMock(iRunner::class);
41+
$deployer = $this->createMock(iGet::class);
42+
$deployer->method('get')->with('npm:binary', 'npm')->willReturn('npm');
43+
44+
$this->sut = $this->getMockForAbstractClass(AbstractRunTask::class, [$runner, $deployer]);
45+
46+
self::assertSame($runner, $this->sut->runner);
47+
}
48+
49+
public function testGetNpmCommand() : void {
50+
self::assertEquals('run', $this->sut->getNpmCommand());
51+
}
52+
53+
public function testGetArguments() : void {
54+
$arguments = $this->sut->getArguments();
55+
56+
self::assertContains($this->sut->getNpmCommand(), $arguments);
57+
self::assertContainsOnly('string', $arguments);
58+
self::assertGreaterThan(3, count($arguments));
59+
}
60+
}

test/task/run/GenericTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\npm\test\task\run;
19+
20+
use de\codenamephp\deployer\base\functions\iGet;
21+
use de\codenamephp\deployer\command\runner\iRunner;
22+
use de\codenamephp\deployer\npm\task\run\Generic;
23+
use PHPUnit\Framework\TestCase;
24+
25+
final class GenericTest extends TestCase {
26+
27+
private Generic $sut;
28+
29+
protected function setUp() : void {
30+
parent::setUp();
31+
32+
$runner = $this->createMock(iRunner::class);
33+
$deployer = $this->createMock(iGet::class);
34+
$deployer->method('get')->with('npm:binary', 'npm')->willReturn('npm');
35+
36+
$this->sut = new Generic('', [], $runner, $deployer);
37+
}
38+
39+
public function test__construct() : void {
40+
$scriptName = 'some script';
41+
$runner = $this->createMock(iRunner::class);
42+
$deployer = $this->createMock(iGet::class);
43+
$deployer->method('get')->with('npm:binary', 'npm')->willReturn('npm');
44+
45+
$this->sut = new Generic($scriptName, ['some', 'arguments'], $runner, $deployer);
46+
47+
$arguments = $this->sut->getArguments();
48+
49+
self::assertEquals($scriptName, $this->sut->getScriptName());
50+
self::assertSame($runner, $this->sut->runner);
51+
self::assertContains('some', $arguments);
52+
self::assertContains('arguments', $arguments);
53+
self::assertGreaterThan(4, count($arguments));
54+
}
55+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\npm\test\task\run\build;
19+
20+
use de\codenamephp\deployer\base\functions\iGet;
21+
use de\codenamephp\deployer\command\runner\iRunner;
22+
use de\codenamephp\deployer\npm\task\run\build\Development;
23+
use PHPUnit\Framework\TestCase;
24+
25+
final class DevelopmentTest extends TestCase {
26+
27+
private Development $sut;
28+
29+
protected function setUp() : void {
30+
parent::setUp();
31+
32+
$runner = $this->createMock(iRunner::class);
33+
$deployer = $this->createMock(iGet::class);
34+
$deployer->method('get')->with('npm:binary', 'npm')->willReturn('npm');
35+
36+
$this->sut = new Development($runner, $deployer);
37+
}
38+
39+
public function testGetScriptName() : void {
40+
self::assertEquals('build:development', $this->sut->getScriptName());
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\npm\test\task\run\build;
19+
20+
use de\codenamephp\deployer\base\functions\iGet;
21+
use de\codenamephp\deployer\command\runner\iRunner;
22+
use de\codenamephp\deployer\npm\task\run\build\Production;
23+
use PHPUnit\Framework\TestCase;
24+
25+
final class ProductionTest extends TestCase {
26+
27+
private Production $sut;
28+
29+
protected function setUp() : void {
30+
parent::setUp();
31+
32+
$runner = $this->createMock(iRunner::class);
33+
$deployer = $this->createMock(iGet::class);
34+
$deployer->method('get')->with('npm:binary', 'npm')->willReturn('npm');
35+
36+
$this->sut = new Production($runner, $deployer);
37+
}
38+
39+
public function testGetScriptName() : void {
40+
self::assertEquals('build', $this->sut->getScriptName());
41+
}
42+
}

0 commit comments

Comments
 (0)