TestTasksTrait::capturedOutputStream()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
namespace Robo\Traits;
3
4
use Robo\Robo;
5
use Robo\TaskAccessor;
6
use Robo\Collection\CollectionBuilder;
7
use Symfony\Component\Console\Output\BufferedOutput;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
trait TestTasksTrait
11
{
12
    use TaskAccessor;
13
14
    protected $testPrinter;
15
    protected $capturedOutput;
16
    protected $logger;
17
18
    public function initTestTasksTrait($commandClass = null, $container = null, $input = null)
19
    {
20
        if (!$container) {
21
            $container = Robo::createDefaultContainer();
22
        }
23
        $this->capturedOutput = '';
24
        $this->testPrinter = new BufferedOutput(OutputInterface::VERBOSITY_DEBUG);
25
26
        $app = Robo::createDefaultApplication();
27
        $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...
28
        \Robo\Robo::configureContainer($container, $app, $config, $input, $this->testPrinter);
29
30
        // Set the application dispatcher
31
        $app->setDispatcher($container->get('eventDispatcher'));
32
        $this->logger = $container->get('logger');
33
34
        // Use test class as command class if a specific one is not provided
35
        if (!$commandClass) {
36
            $commandClass = $this;
37
        }
38
39
        if ($commandClass instanceof Psr\Log\LoggerAwareInterface) {
0 ignored issues
show
Bug introduced by
The class Robo\Traits\Psr\Log\LoggerAwareInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
40
            $commandClass->setLogger($this->logger);
41
        }
42
43
        // Configure BuilderAwareTrait
44
        $builder = CollectionBuilder::create($container, $commandClass);
45
        $commandClass->setBuilder($builder);
46
        $this->setBuilder($builder);
47
48
        return $container;
49
    }
50
51
    public function capturedOutputStream()
52
    {
53
        if (!$this->testPrinter) {
54
            $this->initTestTasksTrait();
55
        }
56
        return $this->testPrinter;
57
    }
58
59
    public function logger()
60
    {
61
        return $this->logger;
62
    }
63
64
    protected function accumulate()
65
    {
66
        $this->capturedOutput .= $this->capturedOutputStream()->fetch();
67
        return $this->capturedOutput;
68
    }
69
70
    public function assertOutputContains($value)
71
    {
72
        $output = $this->accumulate();
73
        $output = $this->simplify($output);
74
        $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...
75
    }
76
77
    public function assertOutputNotContains($value)
78
    {
79
        $output = $this->accumulate();
80
        $output = $this->simplify($output);
81
        $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...
82
    }
83
84
    public function assertOutputEquals($value)
85
    {
86
        $output = $this->accumulate();
87
        $output = $this->simplify($output);
88
        $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...
89
    }
90
91
    /**
92
     * Make our output comparisons more platform-agnostic by converting
93
     * CRLF (Windows) or raw CR (confused output) to a LF (unix/Mac).
94
     */
95 View Code Duplication
    protected function simplify($output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        $output = str_replace("\r\n", "\n", $output);
98
        $output = str_replace("\r", "\n", $output);
99
100
        return $output;
101
    }
102
}
103