Completed
Push — master ( 5f4179...916f6e )
by Richard
11s
created

PhpSpecTester::setUp()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 17
nc 1
nop 3
1
<?php
2
3
namespace RMiller\BehatSpec\Extension\PhpSpecExtension\Tester;
4
5
use Behat\Testwork\Environment\Environment;
6
use Behat\Testwork\Specification\SpecificationIterator;
7
use Behat\Testwork\Tester\Result\TestResult;
8
use Behat\Testwork\Tester\Setup\Setup;
9
use Behat\Testwork\Tester\Setup\Teardown;
10
use Behat\Testwork\Tester\SuiteTester;
11
use RMiller\BehatSpec\Extension\PhpSpecExtension\Process\DescRunner;
12
use Symfony\Component\Console\Helper\QuestionHelper;
13
use Symfony\Component\Console\Helper\FormatterHelper;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Question\Question;
17
use Symfony\Component\Console\Question\ConfirmationQuestion;
18
19
class PhpSpecTester implements SuiteTester
20
{
21
    private $baseTester;
22
    private $specRunner;
23
    private $output;
24
    private $input;
25
26
    public function __construct(
27
        SuiteTester $baseTester,
28
        DescRunner $specRunner,
29
        InputInterface $input,
30
        OutputInterface $output
31
    ) {
32
        $this->baseTester = $baseTester;
33
        $this->specRunner = $specRunner;
34
        $this->input = $input;
35
        $this->output = $output;
36
    }
37
38
    /**
39
     * Tests provided suite specifications.
40
     *
41
     * @param Environment $env
42
     * @param SpecificationIterator $iterator
43
     * @param Boolean $skip
44
     *
45
     * @return TestResult
46
     */
47
    public function test(Environment $env, SpecificationIterator $iterator, $skip)
48
    {
49
        return $this->baseTester->test($env, $iterator, $skip);
50
    }
51
52
    /**
53
     * Tears down suite after a test.
54
     *
55
     * @param Environment $env
56
     * @param SpecificationIterator $iterator
57
     * @param Boolean $skip
58
     * @param TestResult $result
59
     *
60
     * @return Teardown
61
     */
62
    public function tearDown(Environment $env, SpecificationIterator $iterator, $skip, TestResult $result)
63
    {
64
        return $this->baseTester->tearDown($env, $iterator, $skip, $result);
65
    }
66
67
    /**
68
     * Sets up suite for a test.
69
     *
70
     * @param Environment $env
71
     * @param SpecificationIterator $iterator
72
     * @param Boolean $skip
73
     *
74
     * @return Setup
75
     */
76
    public function setUp(Environment $env, SpecificationIterator $iterator, $skip)
77
    {
78
        spl_autoload_register(function ($class) {
79
            $errorMessages = [
80
                $class .' was not found.'
81
            ];
82
83
            $formatter = new FormatterHelper();
84
            $formattedBlock = $formatter->formatBlock($errorMessages, 'error', true);
85
            $this->output->writeln('');
86
            $this->output->writeln($formattedBlock);
87
            $this->output->writeln('');
88
89
            $question = sprintf('Do you want to create a specification for %s? (Y/n)', $class);
90
            $questionBlock = new ConfirmationQuestion($formatter->formatBlock($question, 'question', true));
91
92
            $dialog = new QuestionHelper();
93
94
            if ($dialog->ask($this->input, $this->output, $questionBlock)) {
95
                $this->output->writeln('');
96
                $this->specRunner->runDescCommand($class);
97
            }
98
        }, true, false);
99
100
        return $this->baseTester->setUp($env, $iterator, $skip);
101
    }
102
}
103