Completed
Pull Request — master (#575)
by Greg
03:10
created

SeeInOutputTrait::simplify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
namespace Codeception\Module;
3
4
use Robo\Robo;
5
use Symfony\Component\Console\Output\BufferedOutput;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
trait SeeInOutputTrait
9
{
10
    protected $testPrinter;
11
    protected $capturedOutput;
12
    protected $logger;
13
14
    public function initSeeInOutputTrait($container, $input = null)
15
    {
16
        $this->capturedOutput = '';
17
        $this->testPrinter = new BufferedOutput(OutputInterface::VERBOSITY_DEBUG);
18
19
        $app = Robo::createDefaultApplication();
20
        $config = new \Robo\Config();
0 ignored issues
show
Deprecated Code introduced by
The class Robo\Config has been deprecated with message: Use \Robo\Config\Config

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
21
        \Robo\Robo::configureContainer($container, $app, $config, $input, $this->testPrinter);
22
23
        // Set the application dispatcher
24
        $app->setDispatcher($container->get('eventDispatcher'));
25
        $this->logger = $container->get('logger');
26
    }
27
28
    public function capturedOutputStream()
29
    {
30
        return $this->testPrinter;
31
    }
32
33
    public function logger()
34
    {
35
        return $this->logger;
36
    }
37
38
    protected function accumulate()
39
    {
40
        $this->capturedOutput .= $this->testPrinter->fetch();
41
        return $this->capturedOutput;
42
    }
43
44
    public function seeInOutput($value)
45
    {
46
        $output = $this->accumulate();
47
        $output = $this->simplify($output);
48
        $this->assertContains($value, $output);
0 ignored issues
show
Bug introduced by
It seems like assertContains() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
49
    }
50
51
    public function doNotSeeInOutput($value)
52
    {
53
        $output = $this->accumulate();
54
        $output = $this->simplify($output);
55
        $this->assertNotContains($value, $output);
0 ignored issues
show
Bug introduced by
It seems like assertNotContains() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
56
    }
57
58
    public function seeOutputEquals($value)
59
    {
60
        $output = $this->accumulate();
61
        $output = $this->simplify($output);
62
        $this->assertEquals($value, $output);
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
63
    }
64
65
    /**
66
     * Make our output comparisons more platform-agnostic by converting
67
     * CRLF (Windows) or raw CR (confused output) to a LF (unix/Mac).
68
     */
69
    protected function simplify($output)
70
    {
71
        $output = str_replace("\r\n", "\n", $output);
72
        $output = str_replace("\r", "\n", $output);
73
74
        return $output;
75
    }
76
}
77