Completed
Push — master ( fdad86...4add92 )
by Alessandro
08:39
created

ProcessFactory::createUniqueId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Paraunit\Process;
4
5
use Paraunit\Configuration\JSONLogFilename;
6
use Paraunit\Configuration\PHPUnitBinFile;
7
use Paraunit\Configuration\PHPUnitConfig;
8
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
9
10
/**
11
 * Class ProcessFactory
12
 * @package Paraunit\Process
13
 */
14
class ProcessFactory
15
{
16
    /** @var  string */
17
    private $phpUnitBin;
18
19
    /** @var  JSONLogFilename */
20
    private $jsonLogFilename;
21
22
    /** @var  PHPUnitConfig */
23
    private $phpunitConfig;
24
25
    /**
26
     * ProcessFactory constructor.
27
     * @param PHPUnitBinFile $phpUnitBinFile
28 11
     * @param JSONLogFilename $jsonLogFilename
29
     */
30 11
    public function __construct(PHPUnitBinFile $phpUnitBinFile, JSONLogFilename $jsonLogFilename)
31 11
    {
32 11
        $this->phpUnitBin = $phpUnitBinFile->getPhpUnitBin();
33
        $this->jsonLogFilename = $jsonLogFilename;
34
    }
35
36
    /**
37
     * @param $testFilePath
38
     * @return SymfonyProcessWrapper
39 11
     * @throws \Exception
40
     */
41 11
    public function createProcess($testFilePath)
42 1
    {
43
        if (! $this->phpunitConfig instanceof PHPUnitConfig) {
44
            throw new InvalidConfigurationException('PHPUnit config missing');
45 10
        }
46 10
47
        $uniqueId = $this->createUniqueId($testFilePath);
48 10
        $command = $this->createCommandLine($testFilePath, $uniqueId);
49
50
        return new SymfonyProcessWrapper($command, $uniqueId);
51
    }
52
53
    /**
54 10
     * @param PHPUnitConfig $configFile
55
     */
56 10
    public function setConfig(PHPUnitConfig $configFile)
57 10
    {
58
        $this->phpunitConfig = $configFile;
59
    }
60
61
    /**
62
     * @param string $testFilePath
63
     * @param string $uniqueId
64 10
     * @return string
65
     */
66 10
    private function createCommandLine($testFilePath, $uniqueId)
67 10
    {
68 10
        $commandLine = $this->phpUnitBin
69 10
            . ' --configuration=' . $this->phpunitConfig->getFileFullPath()
70 10
            . ' --log-json=' . $this->jsonLogFilename->generateFromUniqueId($uniqueId);
71
72
        foreach ($this->phpunitConfig->getPhpunitOptions() as $option) {
73
            $commandLine .= ' --' . $option->getName();
74
            if ($option->hasValue()) {
75
                $commandLine .= '=' . $option->getValue();
76
            }
77 10
        }
78
79 10
        return $commandLine . ' ' . $testFilePath;
80
    }
81
82
    /**
83
     * @param string $testFilePath
84
     * @return string
85
     */
86
    private function createUniqueId($testFilePath)
87
    {
88
        return md5($testFilePath);
89
    }
90
}
91