Completed
Branch develop (c4bcdd)
by Stephen
06:03
created

TransformerMakeCommandTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A it_requires_a_name_for_the_transformer() 0 11 1
A it_builds_correct_transformer_file() 0 62 1
1
<?php
2
3
namespace Spinen\Commands;
4
5
use Illuminate\Contracts\Console\Kernel;
6
use Illuminate\Support\Facades\File;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Console\Output\BufferedOutput;
9
use Spinen\Transformers\TestCase;
10
11
class TransformerMakeCommandTest extends TestCase
12
{
13
    /**
14
     * @test
15
     * @group unit
16
     */
17
    public function it_requires_a_name_for_the_transformer()
18
    {
19
        $input = new ArrayInput([
20
            'command' => 'make:transformer',
21
        ]);
22
        $output = new BufferedOutput();
23
24
        app(Kernel::class)->handle($input, $output);
25
26
        $this->assertContains('missing: "name"', $output->fetch());
27
    }
28
29
    /**
30
     * @test
31
     * @group unit
32
     */
33
    public function it_builds_correct_transformer_file()
34
    {
35
        $stub = app_path('Console/Commands/stubs/transformer.stub');
36
        $path = app_path('Transformers/TestTransformer.php');
37
        $content = <<<EOT
38
<?php
39
40
namespace App\Transformers;
41
42
use App\Support\Transformation\Transformer;
43
use Illuminate\Database\Eloquent\Model;
44
45
/**
46
 * Class TestTransformer
47
 *
48
 * @package App\Transformers
49
 */
50
class TestTransformer extends Transformer
51
{
52
    /**
53
     * @param Model \$model
54
     *
55
     * @return array
56
     */
57
    public function transform(Model \$model)
58
    {
59
        return [
60
            // TODO: Layout transformation here
61
        ];
62
    }
63
}
64
65
EOT;
66
//        var_export('**********');
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
67
//        var_export($content, false);
68
//        var_export('**********');
69
70
        File::shouldReceive('get')
71
            ->once()
72
            ->with($stub)
73
            ->andReturn(file_get_contents($stub));
74
75
        File::shouldReceive('put')
0 ignored issues
show
Unused Code introduced by
The call to the method Mockery\Expectation::andReturnNull() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
76
            ->once()
77
            ->withArgs([
78
                $path,
79
                $content,
80
            ])
81
            ->andReturnNull();
82
83
        File::shouldIgnoreMissing();
84
85
        $input = new ArrayInput([
86
            'command' => 'make:transformer',
87
            'name'    => 'TestTransformer',
88
        ]);
89
        $output = new BufferedOutput();
90
91
        app(Kernel::class)->handle($input, $output);
92
93
        $this->assertContains('successfully', $output->fetch());
94
    }
95
}
96