Skip to content

Commit f4d15e2

Browse files
committed
Add ProcessFactory::createRemoveDirectoryProcess()
1 parent b452cb2 commit f4d15e2

File tree

9 files changed

+113
-7
lines changed

9 files changed

+113
-7
lines changed

bin/ci/phpunit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ else
2828

2929
"php${phpVersion}" \
3030
vendor/bin/phpunit \
31+
--bootstrap "${!composerHomeVarName}"/vendor/autoload.php \
3132
--configuration config/ci/phpunit.xml \
3233
--cache-result-file var/ci/phpunit/php-${phpVersion}/symfony-${symfonyVersion}/.phpunit.result.cache \
33-
--bootstrap "${!composerHomeVarName}"/vendor/autoload.php \
3434
${phpunitParameters}
3535
fi

bin/ci/phpunit-coverage

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ XDEBUG_MODE=coverage \
1212
php8.1 \
1313
"${ROOT_DIR}"/vendor/bin/phpunit \
1414
--bootstrap "${COMPOSER_HOME_SYMFONY_6_0}"/vendor/autoload.php \
15-
--coverage-html "${ROOT_DIR}"/var/ci/phpunit/coverage/html \
1615
--configuration "${ROOT_DIR}"/config/ci/phpunit.xml \
16+
--cache-result-file var/ci/phpunit/coverage/.phpunit.result.cache \
17+
--coverage-html "${ROOT_DIR}"/var/ci/phpunit/coverage/html \
1718
"${@}"
1819
set -e
1920

bin/start

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ docker \
1717
--env HOST_UID="$(id -u)" \
1818
--env HOST_GID="$(id -g)" \
1919
--workdir "${ROOT_DIR}" \
20-
steevanb/php-parallel-processes:parallel-processes-0.2.1 \
20+
steevanb/php-parallel-processes:parallel-processes-0.3.0 \
2121
php \
2222
bin/start.php \
2323
"${@}"

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
### master
22

3+
- Add `ParallelProcessesApplication::hasProcess()`
4+
- Add `ProcessFactory`
5+
- Add `ProcessFactory::createRemoveDirectoryProcess()`
6+
- Fix PHPUnit cache file path in `bin/ci/phpunit-coverage`
7+
38
### [0.3.0](../../compare/0.2.1...0.3.0) - 2022-01-10
49

510
- Create GitHub Actions to build and push Docker image `steevanb/php-parallel-processes:ci`

config/ci/.phpunit.result.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.

docker/parallel-processes/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ RUN \
1717
&& apt-get install -y bash unzip \
1818
&& composer global require steevanb/php-parallel-processes:${REPOSITORY_VERSION} \
1919

20-
# Purge \
20+
# Purge
2121
&& rm /usr/local/bin/composer \
2222
&& apt-get autoremove -y \
2323
&& apt-get clean \

src/Console/Application/ParallelProcessesApplication.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ public function addProcesses(ProcessArray $processes): self
5252
return $this;
5353
}
5454

55+
public function hasProcess(Process $process): bool
56+
{
57+
$return = false;
58+
foreach ($this->processes->toArray() as $loopProcess) {
59+
if (spl_object_id($loopProcess) === spl_object_id($process)) {
60+
$return = true;
61+
break;
62+
}
63+
}
64+
65+
return $return;
66+
}
67+
5568
public function getProcesses(): ProcessArray
5669
{
5770
return $this->processes;
@@ -69,15 +82,15 @@ public function getTheme(): ThemeInterface
6982
return $this->theme;
7083
}
7184

72-
/** Set refresh interval in milliseconds */
85+
/** Set refresh interval in microseconds */
7386
public function setRefreshInterval(int $refreshInterval): self
7487
{
7588
$this->refreshInterval = $refreshInterval;
7689

7790
return $this;
7891
}
7992

80-
/** Get refresh interval in milliseconds */
93+
/** Get refresh interval in microseconds */
8194
public function getRefreshInterval(): int
8295
{
8396
return $this->refreshInterval;

src/Process/ProcessFactory.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Steevanb\ParallelProcess\Process;
6+
7+
use Steevanb\ParallelProcess\Console\Application\ParallelProcessesApplication;
8+
9+
class ProcessFactory
10+
{
11+
public static function createRemoveDirectoryProcess(
12+
string $directory,
13+
ParallelProcessesApplication $application,
14+
string $name = null,
15+
string $cwd = null
16+
): ?Process {
17+
if (is_dir($directory)) {
18+
$return = new Process(['rm', '-rf', $directory], $cwd);
19+
if (is_string($name)) {
20+
$return->setName($name);
21+
}
22+
23+
$application->addProcess($return);
24+
} else {
25+
$return = null;
26+
}
27+
28+
return $return;
29+
}
30+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Steevanb\ParallelProcess\Tests\Process\ProcessFactory;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Steevanb\ParallelProcess\{
9+
Console\Application\ParallelProcessesApplication,
10+
Process\Process,
11+
Process\ProcessFactory
12+
};
13+
14+
/** @covers \Steevanb\ParallelProcess\Process\ProcessFactory::createRemoveDirectoryProcess */
15+
class CreateRemoveDirectoryProcessTest extends TestCase
16+
{
17+
public function testDirectoryExists(): void
18+
{
19+
$application = new ParallelProcessesApplication();
20+
21+
/** @var Process $process */
22+
$process = ProcessFactory::createRemoveDirectoryProcess(__DIR__, $application);
23+
24+
static::assertInstanceOf(Process::class, $process);
25+
static::assertTrue($application->hasProcess($process));
26+
}
27+
28+
public function testDirectoryNotFound(): void
29+
{
30+
$application = new ParallelProcessesApplication();
31+
32+
$process = ProcessFactory::createRemoveDirectoryProcess(__DIR__ . '/foo/bar', $application);
33+
34+
static::assertNull($process);
35+
}
36+
37+
public function testName(): void
38+
{
39+
$application = new ParallelProcessesApplication();
40+
41+
/** @var Process $process */
42+
$process = ProcessFactory::createRemoveDirectoryProcess(__DIR__, $application, 'foo');
43+
44+
static::assertInstanceOf(Process::class, $process);
45+
static::assertSame('foo', $process->getName());
46+
}
47+
48+
public function testCwd(): void
49+
{
50+
$application = new ParallelProcessesApplication();
51+
52+
/** @var Process $process */
53+
$process = ProcessFactory::createRemoveDirectoryProcess(__DIR__, $application, 'foo', '/foo/bar');
54+
55+
static::assertInstanceOf(Process::class, $process);
56+
static::assertSame('/foo/bar', $process->getWorkingDirectory());
57+
}
58+
}

0 commit comments

Comments
 (0)