Failed Conditions
Push — master ( b9670c...4ba57f )
by Zac
16:20 queued 37s
created

ConsoleTestHelperTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 2
cbo 1
dl 0
loc 81
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A assertHasStandardOutput() 0 13 4
A assertCountLinesOfOutput() 0 4 1
A assertCountRunTests() 0 8 2
A assertResults() 0 8 1
A assertRecentResultsPersisted() 0 11 2
A assertRecentResultsNotPersisted() 0 6 1
A execute() 0 14 2
1
<?php
2
3
namespace Overwatch\TestBundle\Tests\Command;
4
5
use Overwatch\TestBundle\DataFixtures\ORM\TestFixtures;
6
7
/**
8
 * ConsoleTestHelperTrait
9
 */
10
trait ConsoleTestHelperTrait
11
{
12
    protected $output = null;
13
    
14
    public function assertHasStandardOutput()
15
    {
16
        $this->assertStringStartsWith($this->application->getName(), $this->output[0]);
0 ignored issues
show
Bug introduced by
The property application does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like assertStringStartsWith() 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...
17
        $this->assertContains($this->application->getVersion(), $this->output[0]);
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...
18
        
19
        foreach ($this->output as $lineNum => $line) {
20
            if ($lineNum === 0 || $lineNum === count($this->output) - 1) {
21
                continue;
22
            }
23
            
24
            $this->assertStringStartsWith(' > ', $line);
0 ignored issues
show
Bug introduced by
It seems like assertStringStartsWith() 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...
25
        }
26
    }
27
    
28
    /**
29
     * @param integer $count
30
     */
31
    public function assertCountLinesOfOutput($count)
32
    {
33
        $this->assertCount($count, $this->output);
0 ignored issues
show
Bug introduced by
The method assertCount() does not exist on Overwatch\TestBundle\Tes...\ConsoleTestHelperTrait. Did you maybe mean assertCountLinesOfOutput()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
34
    }
35
    
36
    /**
37
     * @param integer $count
38
     */
39
    public function assertCountRunTests($count = null)
40
    {
41
        if ($count === null) {
42
            $count = count(TestFixtures::$tests);
43
        }
44
        
45
        $this->assertStringEndsWith("$count tests", $this->output[0]);
0 ignored issues
show
Bug introduced by
It seems like assertStringEndsWith() 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...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $count instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
46
    }
47
    
48
    public function assertResults($failed = '[0-9]+', $error = '[0-9]+', $unsatisfactory = '[0-9]+', $passed = '[0-9]+')
49
    {
50
        $this->assertRegExp(
0 ignored issues
show
Bug introduced by
It seems like assertRegExp() 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...
51
            "/^$failed FAILED, $error ERROR, $unsatisfactory UNSATISFACTORY, $passed PASSED/",
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $failed instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $error instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $unsatisfactory instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $passed instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
52
            $this->output[count($this->output) - 1]
53
        );
54
        $this->assertRegExp('/, in [0-9]+ minutes and [0-9]+ seconds$/i', $this->output[count($this->output) - 1]);
0 ignored issues
show
Bug introduced by
It seems like assertRegExp() 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...
55
    }
56
    
57
    public function assertRecentResultsPersisted($count = 3)
58
    {
59
        $now = (new \DateTime())->getTimestamp();
60
        $results = $this->resultRepo->getResults([], $count);
0 ignored issues
show
Bug introduced by
The property resultRepo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
        
62
        foreach ($results as $result) {
63
            $diff = $now - $result->getCreatedAt()->getTimestamp();
64
            
65
            $this->assertLessThan(60, $diff);
0 ignored issues
show
Bug introduced by
It seems like assertLessThan() 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...
66
        }
67
    }
68
    
69
    public function assertRecentResultsNotPersisted()
70
    {
71
        $result = $this->resultRepo->getResults([], 1);
72
        
73
        $this->assertGreaterThan(500, $result[0]->getCreatedAt()->getTimestamp());
0 ignored issues
show
Bug introduced by
It seems like assertGreaterThan() 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...
74
    }
75
    
76
    protected function execute($params = [], $options = [])
77
    {
78
        $params['command'] = self::COMMAND_NAME;
79
        $returnCode = $this->command->execute($params, $options);
0 ignored issues
show
Bug introduced by
The property command does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
80
        
81
        $this->output = explode(PHP_EOL, $this->command->getDisplay());
82
        $lastLine = array_pop($this->output);
83
        
84
        if (!empty($lastLine)) {
85
            array_push($this->output, $lastLine);
86
        }
87
        
88
        return $returnCode;
89
    }
90
}
91