Passed
Push — master ( ad6dc0...5f4f20 )
by Simon
04:23 queued 10s
created

SeederTaskTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testGetFixture() 0 3 1
B testParseFixture() 0 39 1
A testUnseed() 0 8 1
A testNoType() 0 6 1
A testParseFixtureNullFile() 0 5 1
A testRun() 0 18 1
1
<?php
2
3
namespace Firesphere\Seeder\Tests;
4
5
6
use Firesphere\Seeder\Tasks\SeederTask;
7
use Firesphere\Seeder\Tests\Mock\Page;
8
use Firesphere\Seeder\Tests\Mock\Quote;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Dev\SapphireTest;
13
14
class SeederTaskTest extends SapphireTest
15
{
16
    /**
17
     * @var SeederTask
18
     */
19
    protected $seeder;
20
21
    protected $usesDatabase = true;
22
23
    protected static $extra_dataobjects = [
24
        Mock\Page::class,
25
        Mock\Quote::class
26
    ];
27
28
    public function setUp()
29
    {
30
        parent::setUp();
31
        Config::modify()->update(SeederTask::class, 'Seedfile', 'tests/fixtures/seedertasktest.yml');
32
        $this->seeder = Injector::inst()->get(SeederTask::class);
33
    }
34
35
    public function testRun()
36
    {
37
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
38
        $this->seeder->run($request);
39
40
        $pages = Page::get();
41
        $this->assertEquals(2, $pages->count());
42
43
        /** @var Page $page */
44
        $page = $pages->filter(['Title' => 'Samuel L. Lipsum'])->first();
45
46
        $this->assertEquals('Cat Lipsum', $page->Friends()->first()->Title);
0 ignored issues
show
Bug introduced by
The method Friends() does not exist on Firesphere\Seeder\Tests\Mock\Page. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        $this->assertEquals('Cat Lipsum', $page->/** @scrutinizer ignore-call */ Friends()->first()->Title);
Loading history...
47
48
        $this->assertTrue($page->isPublished());
0 ignored issues
show
Bug introduced by
The method isPublished() does not exist on Firesphere\Seeder\Tests\Mock\Page. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        $this->assertTrue($page->/** @scrutinizer ignore-call */ isPublished());
Loading history...
49
50
        $quotes = Quote::get();
51
52
        $this->assertEquals(2, $quotes->count());
53
    }
54
55
    public function testParseFixture()
56
    {
57
        SeederTask::setFixtureFile('tests/fixtures/seedertasktest.yml');
58
59
        $result = $this->seeder->parseFixture();
60
61
        $this->assertTrue(is_array($result));
62
63
        $expected = [
64
            Quote::class =>
65
                [
66
                    'quote1' =>
67
                        [
68
                            'quote' => 'Time is an illusion. Lunchtime doubly so.',
69
                        ],
70
                    'quote2' =>
71
                        [
72
                            'quote' => 'In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.',
73
                        ],
74
                ],
75
            Page::class  =>
76
                [
77
                    'page1' =>
78
                        [
79
                            'Title'   => 'Samuel L. Lipsum',
80
                            'Content' => '<p>Well, the way they make shows is, they make one show. That show\'s called a pilot. Then they show that show to the people who make shows, and on the strength of that one show they decide if they\'re going to make more shows. Some pilots get picked and become television programs. Some don\'t, become nothing. She starred in one of the ones that became nothing.</p>',
81
                            'Quotes'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote2',
82
                        ],
83
                    'page2' =>
84
                        [
85
                            'Title'   => 'Cat Lipsum',
86
                            'Content' => '<p>Give attitude pooping rainbow while flying in a toasted bread costume in space loved it, hated it, loved it, hated it yet has closed eyes but still sees you and stare out the window. Chase imaginary bugs throw down all the stuff in the kitchen. Stand in front of the computer screen eat half my food and ask for more hiss and stare at nothing then run suddenly away. Your pillow is now my pet bed soft kitty warm kitty little ball of furr but hiding behind the couch until lured out by a feathery toy meowzer hack, for attack dog, run away and pretend to be victim. Intently stare at the same spot cats go for world domination yet chase dog then run away jump around on couch, meow constantly until given food, and bleghbleghvomit my furball really tie the room together meow. Playing with balls of wool climb leg tuxedo cats always looking dapper. Hack up furballs thug cat prance along on top of the garden fence, annoy the neighbor\'s dog and make it bark for jump around on couch, meow constantly until given food, lick the plastic bag.</p>',
87
                            'Friend'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Page.page1',
88
                            'Quotes'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote1,=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote2',
89
                        ],
90
                ],
91
        ];
92
93
        $this->assertEquals($expected, $result);
94
    }
95
96
    public function testParseFixtureNullFile()
97
    {
98
        SeederTask::setFixtureFile(null);
99
100
        $this->assertTrue(is_array($this->seeder->parseFixture()));
101
    }
102
103
    public function testNoType()
104
    {
105
        $request = new HTTPRequest('GET', '', []);
106
        $result = $this->seeder->run($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->seeder->run($request) targeting Firesphere\Seeder\Tasks\SeederTask::run() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
107
108
        $this->assertNull($result);
109
    }
110
111
    public function testGetFixture()
112
    {
113
        $this->assertEquals('tests/fixtures/seedertasktest.yml', SeederTask::getFixtureFile());
114
    }
115
116
    public function testUnseed()
117
    {
118
        $request = new HTTPRequest('GET', '', ['type' => 'unseed']);
119
        $this->seeder->run($request);
120
        
121
        $this->assertEquals(0, Page::get()->count());
122
123
        $this->assertEquals(0, Quote::get()->count());
124
125
    }
126
}