Passed
Push — master ( 0a5a26...fbc511 )
by Simon
01:42
created

SeederTaskTest::testLive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\Seeder\Tests;
4
5
use Firesphere\Seeder\Tasks\SeederTask;
6
use Firesphere\Seeder\Tests\Mock\Page;
7
use Firesphere\Seeder\Tests\Mock\Quote;
8
use SilverStripe\Control\Director;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Core\Environment;
12
use SilverStripe\Core\Injector\Injector;
13
use SilverStripe\Dev\SapphireTest;
14
15
class SeederTaskTest extends SapphireTest
16
{
17
    /**
18
     * @var SeederTask
19
     */
20
    protected $seeder;
21
22
    protected $usesDatabase = true;
23
24
    protected static $extra_dataobjects = [
25
        Mock\Page::class,
26
        Mock\Quote::class
27
    ];
28
29
    public function setUp()
30
    {
31
        parent::setUp();
32
        Config::modify()->update(SeederTask::class, 'Seedfile', 'tests/fixtures/seedertasktest.yml');
33
        $this->seeder = Injector::inst()->get(SeederTask::class);
34
    }
35
36
    /**
37
     * @expectedException \Exception
38
     */
39
    public function testLive()
40
    {
41
        Environment::setEnv('SS_ENVIRONMENT_TYPE', 'live');
42
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
43
        $this->seeder->run($request);
44
    }
45
46
    public function testRun()
47
    {
48
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
49
        $this->seeder->run($request);
50
51
        $pages = Page::get();
52
        $this->assertEquals(2, $pages->count());
53
54
        /** @var Page $page */
55
        $page = $pages->filter(['Title' => 'Samuel L. Lipsum'])->first();
56
57
        $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

57
        $this->assertEquals('Cat Lipsum', $page->/** @scrutinizer ignore-call */ Friends()->first()->Title);
Loading history...
58
59
        $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

59
        $this->assertTrue($page->/** @scrutinizer ignore-call */ isPublished());
Loading history...
60
61
        $quotes = Quote::get();
62
63
        $this->assertEquals(2, $quotes->count());
64
    }
65
66
    public function testParseFixture()
67
    {
68
        SeederTask::setFixtureFile('tests/fixtures/seedertasktest.yml');
69
70
        $result = $this->seeder->parseFixture();
71
72
        $this->assertTrue(is_array($result));
73
74
        $expected = [
75
            Quote::class =>
76
                [
77
                    'quote1' =>
78
                        [
79
                            'quote' => 'Time is an illusion. Lunchtime doubly so.',
80
                        ],
81
                    'quote2' =>
82
                        [
83
                            '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.',
84
                        ],
85
                ],
86
            Page::class  =>
87
                [
88
                    'page1' =>
89
                        [
90
                            'Title'   => 'Samuel L. Lipsum',
91
                            '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>',
92
                            'Quotes'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote2',
93
                        ],
94
                    'page2' =>
95
                        [
96
                            'Title'   => 'Cat Lipsum',
97
                            '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>',
98
                            'Friend'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Page.page1',
99
                            'Quotes'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote1,=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote2',
100
                        ],
101
                ],
102
        ];
103
104
        $this->assertEquals($expected, $result);
105
    }
106
107
    public function testParseFixtureNullFile()
108
    {
109
        SeederTask::setFixtureFile(null);
110
111
        $this->assertTrue(is_array($this->seeder->parseFixture()));
112
    }
113
114
    public function testNoType()
115
    {
116
        $request = new HTTPRequest('GET', '', []);
117
        $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...
118
119
        $this->assertNull($result);
120
    }
121
122
    public function testGetFixture()
123
    {
124
        // As it's static, it's null by default in this situation
125
        $this->assertNull( SeederTask::getFixtureFile());
126
    }
127
128
    public function testUnseed()
129
    {
130
        $request = new HTTPRequest('GET', '', ['type' => 'unseed']);
131
        $this->seeder->run($request);
132
133
        $this->assertEquals(0, Page::get()->count());
134
135
        $this->assertEquals(0, Quote::get()->count());
136
    }
137
}
138