Passed
Push — master ( d6cb08...3f0cbf )
by Simon
01:31
created

SeederTaskTest::testLive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 17
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\Injector\Injector;
12
use SilverStripe\Core\Kernel;
13
use SilverStripe\Dev\SapphireTest;
14
15
/**
16
 * Class SeederTaskTest
17
 * @package Firesphere\Seeder\Tests
18
 */
19
class SeederTaskTest extends SapphireTest
20
{
21
    /**
22
     * @var SeederTask
23
     */
24
    protected $seeder;
25
26
    /**
27
     * @var bool
28
     */
29
    protected $usesDatabase = true;
30
31
    /**
32
     * @var array
33
     */
34
    protected static $extra_dataobjects = [
35
        Mock\Page::class,
36
        Mock\Quote::class
37
    ];
38
39
    public function setUp()
40
    {
41
        parent::setUp();
42
        Config::modify()->update(SeederTask::class, 'Seedfile', 'tests/fixtures/seedertasktest.yml');
43
        $this->seeder = Injector::inst()->get(SeederTask::class);
44
    }
45
46
    public function testLive()
47
    {
48
        /** @var Kernel $kernel */
49
        $kernel = Injector::inst()->get(Kernel::class);
50
        $kernel->setEnvironment(Kernel::LIVE);
51
        ob_start();
52
53
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
54
        $this->seeder->run($request);
55
56
        $result = ob_get_contents();
57
58
        ob_end_clean();
59
60
        $this->assertContains('DO NOT RUN ME ON LIVE', $result);
61
62
        $kernel->setEnvironment(Kernel::DEV);
63
    }
64
65
    public function testRun()
66
    {
67
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
68
        $this->seeder->run($request);
69
70
        $pages = Page::get();
71
        $this->assertEquals(2, $pages->count());
72
73
        /** @var Page $page */
74
        $page = $pages->filter(['Title' => 'Samuel L. Lipsum'])->first();
75
76
        $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

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

78
        $this->assertTrue($page->/** @scrutinizer ignore-call */ isPublished());
Loading history...
79
80
        $quotes = Quote::get();
81
82
        $this->assertEquals(2, $quotes->count());
83
    }
84
85
    public function testParseFixture()
86
    {
87
        SeederTask::setFixtureFile('tests/fixtures/seedertasktest.yml');
88
89
        $result = $this->seeder->parseFixture();
90
91
        $this->assertTrue(is_array($result));
92
93
        $expected = [
94
            Quote::class =>
95
                [
96
                    'quote1' =>
97
                        [
98
                            'quote' => 'Time is an illusion. Lunchtime doubly so.',
99
                        ],
100
                    'quote2' =>
101
                        [
102
                            '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.',
103
                        ],
104
                ],
105
            Page::class  =>
106
                [
107
                    'page1' =>
108
                        [
109
                            'Title'   => 'Samuel L. Lipsum',
110
                            '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>',
111
                            'Quotes'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote2',
112
                        ],
113
                    'page2' =>
114
                        [
115
                            'Title'   => 'Cat Lipsum',
116
                            '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>',
117
                            'Friend'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Page.page1',
118
                            'Quotes'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote1,=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote2',
119
                        ],
120
                ],
121
        ];
122
123
        $this->assertEquals($expected, $result);
124
    }
125
126
    public function testParseFixtureNullFile()
127
    {
128
        SeederTask::setFixtureFile(null);
129
130
        $this->assertTrue(is_array($this->seeder->parseFixture()));
131
    }
132
133
    public function testNoType()
134
    {
135
        $request = new HTTPRequest('GET', '', []);
136
        ob_start();
137
        $this->seeder->run($request);
138
139
140
        $this->assertContains('Please tell me what to do', ob_get_contents());
141
        ob_end_clean();
142
    }
143
144
    public function testGetFixture()
145
    {
146
        // As it's static, it's null by default in this situation
147
        $this->assertNull(SeederTask::getFixtureFile());
148
    }
149
150
    public function testUnseed()
151
    {
152
        $request = new HTTPRequest('GET', '', ['type' => 'unseed']);
153
        $this->seeder->run($request);
154
155
        $this->assertEquals(0, Page::get()->count());
156
157
        $this->assertEquals(0, Quote::get()->count());
158
    }
159
160
    public function testRemoveRelations()
161
    {
162
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
163
        $this->seeder->run($request);
164
165
        $this->seeder->removeManyMany(Page::class);
166
167
        $pages = Page::get();
168
169
        foreach ($pages as $page) {
170
            $this->assertEquals(0, (int)$page->Quotes()->count());
171
        }
172
    }
173
174
    public function testUnpublishEach()
175
    {
176
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
177
        $this->seeder->run($request);
178
179
        $this->seeder->unpublishEach(Page::class);
180
181
        $pages = Page::get();
182
183
        foreach ($pages as $page) {
184
            $this->assertFalse($page->isPublished());
185
        }
186
    }
187
}
188