Completed
Push — master ( 597ac5...b01469 )
by Robbie
9s
created

FactoryTest::testGetCommandFromTask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace SilverLeague\Console\Tests\Command;
4
5
use SilverLeague\Console\Command\Dev\Tasks\AbstractTaskCommand;
6
use SilverLeague\Console\Framework\Scaffold;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Dev\Tasks\CleanupTestDatabasesTask;
10
use Symfony\Component\Console\Application;
11
use Symfony\Component\Console\Tester\CommandTester;
12
13
/**
14
 * @coversDefaultClass \SilverLeague\Console\Command\Factory
15
 * @package silverstripe-console
16
 * @author  Robbie Averill <[email protected]>
17
 */
18
class FactoryTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * Test that a Symfony command is bootstrapped and returned when a SilverStripe BuildTask is given
22
     */
23
    public function testGetCommandFromTask()
24
    {
25
        $task = new CleanupTestDatabasesTask;
26
27
        $command = $this->getFactory()->getCommandFromTask($task);
28
        $this->assertInstanceOf(AbstractTaskCommand::class, $command);
29
        $this->assertInstanceOf(Application::class, $command->getApplication());
30
        $this->assertSame($task, $command->getTask());
31
        $this->assertSame($task->getTitle(), $command->getDescription());
32
    }
33
34
    /**
35
     * Test that a SilverStrip task name is converted to a Symfony friendly command name
36
     *
37
     * @param string $input
38
     * @param string $expected
39
     * @dataProvider commandNameProvider
40
     */
41
    public function testGetCommandName($input, $expected)
42
    {
43
        Config::inst()->nest();
44
45
        $fakeTask = new CleanupTestDatabasesTask;
46
        Config::inst()->update(get_class($fakeTask), 'segment', $input);
47
        $this->assertSame($expected, $this->getFactory()->getCommandName($fakeTask));
48
49
        Config::inst()->unnest();
50
    }
51
52
    /**
53
     * @return array[]
54
     */
55
    public function commandNameProvider()
56
    {
57
        return [
58
            ['CleanupTestDatabasesTask', 'dev:tasks:cleanup-test-databases'],
59
            ['FooBar', 'dev:tasks:foo-bar'],
60
            ['FooBarTask', 'dev:tasks:foo-bar']
61
        ];
62
    }
63
64
    /**
65
     * Ensure that the BuildTask functionality can be returned as a closure for the Command
66
     */
67
    public function testGetTaskAsClosure()
68
    {
69
        $taskMock = $this
70
            ->getMockBuilder(CleanupTestDatabasesTask::class)
71
            ->setMethods(['run'])
72
            ->getMock();
73
74
        $taskMock
75
            ->expects($this->once())
76
            ->method('run')
77
            ->with($this->isInstanceOf(HTTPRequest::class));
78
79
        $factory = $this->getFactory();
80
        $command = $factory->getCommandFromTask($taskMock);
81
        $this->assertTrue(is_callable($factory->getTaskAsClosure($command)));
0 ignored issues
show
Security Bug introduced by
It seems like $command defined by $factory->getCommandFromTask($taskMock) on line 80 can also be of type false; however, SilverLeague\Console\Com...ory::getTaskAsClosure() does only seem to accept object<SilverLeague\Cons...ks\AbstractTaskCommand>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
82
83
        $tester = new CommandTester($command);
0 ignored issues
show
Security Bug introduced by
It seems like $command defined by $factory->getCommandFromTask($taskMock) on line 80 can also be of type false; however, Symfony\Component\Consol...ndTester::__construct() does only seem to accept object<Symfony\Component\Console\Command\Command>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
84
        $tester->execute([]);
85
    }
86
87
    /**
88
     * Get a Factory to test with
89
     *
90
     * @return Factory
91
     */
92
    protected function getFactory()
93
    {
94
        return (new Scaffold)->getSilverStripeLoader()->getCommandFactory();
95
    }
96
}
97