Passed
Push — master ( 08f7b1...e4f53a )
by Simon
01:40
created

SeederTaskTest::testLive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 15
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\Dev\SapphireTest;
13
14
/**
15
 * Class SeederTaskTest
16
 * @package Firesphere\Seeder\Tests
17
 */
18
class SeederTaskTest extends SapphireTest
19
{
20
    /**
21
     * @var SeederTask
22
     */
23
    protected $seeder;
24
25
    /**
26
     * @var bool
27
     */
28
    protected $usesDatabase = true;
29
30
    /**
31
     * @var array
32
     */
33
    protected static $extra_dataobjects = [
34
        Mock\Page::class,
35
        Mock\Quote::class
36
    ];
37
38
    public function setUp()
39
    {
40
        parent::setUp();
41
        Config::modify()->update(SeederTask::class, 'Seedfile', 'tests/fixtures/seedertasktest.yml');
42
        $this->seeder = Injector::inst()->get(SeederTask::class);
43
    }
44
45
    public function testLive()
46
    {
47
        Dir
48
        $file = Director::baseFolder() . '/.env';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_VARIABLE on line 48 at column 8
Loading history...
49
        file_put_contents($file,
50
            str_replace('dev', 'live', file_get_contents($file)));
51
        ob_start();
52
53
        $request = new HTTPRequest('GET', '', ['type' => '']);
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
63
    public function testRun()
64
    {
65
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
66
        $this->seeder->run($request);
67
68
        $pages = Page::get();
69
        $this->assertEquals(2, $pages->count());
70
71
        /** @var Page $page */
72
        $page = $pages->filter(['Title' => 'Samuel L. Lipsum'])->first();
73
74
        $this->assertEquals('Cat Lipsum', $page->Friends()->first()->Title);
75
76
        $this->assertTrue($page->isPublished());
77
78
        $quotes = Quote::get();
79
80
        $this->assertEquals(2, $quotes->count());
81
    }
82
83
    public function testParseFixture()
84
    {
85
        SeederTask::setFixtureFile('tests/fixtures/seedertasktest.yml');
86
87
        $result = $this->seeder->parseFixture();
88
89
        $this->assertTrue(is_array($result));
90
91
        $expected = [
92
            Quote::class =>
93
                [
94
                    'quote1' =>
95
                        [
96
                            'quote' => 'Time is an illusion. Lunchtime doubly so.',
97
                        ],
98
                    'quote2' =>
99
                        [
100
                            '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.',
101
                        ],
102
                ],
103
            Page::class  =>
104
                [
105
                    'page1' =>
106
                        [
107
                            'Title'   => 'Samuel L. Lipsum',
108
                            '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>',
109
                            'Quotes'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote2',
110
                        ],
111
                    'page2' =>
112
                        [
113
                            'Title'   => 'Cat Lipsum',
114
                            '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>',
115
                            'Friend'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Page.page1',
116
                            'Quotes'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote1,=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote2',
117
                        ],
118
                ],
119
        ];
120
121
        $this->assertEquals($expected, $result);
122
    }
123
124
    public function testParseFixtureNullFile()
125
    {
126
        SeederTask::setFixtureFile(null);
127
128
        $this->assertTrue(is_array($this->seeder->parseFixture()));
129
    }
130
131
    public function testNoType()
132
    {
133
        $request = new HTTPRequest('GET', '', []);
134
        ob_start();
135
        $this->seeder->run($request);
136
137
138
        $this->assertContains('Please tell me what to do', ob_get_contents());
139
        ob_end_clean();
140
    }
141
142
    public function testGetFixture()
143
    {
144
        // As it's static, it's null by default in this situation
145
        $this->assertNull(SeederTask::getFixtureFile());
146
    }
147
148
    public function testUnseed()
149
    {
150
        $request = new HTTPRequest('GET', '', ['type' => 'unseed']);
151
        $this->seeder->run($request);
152
153
        $this->assertEquals(0, Page::get()->count());
154
155
        $this->assertEquals(0, Quote::get()->count());
156
    }
157
158
    public function testRemoveRelations()
159
    {
160
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
161
        $this->seeder->run($request);
162
163
        $this->seeder->removeManyMany(Page::class);
164
165
        $pages = Page::get();
166
167
        foreach ($pages as $page) {
168
            $this->assertEquals(0, (int)$page->Quotes()->count());
169
        }
170
    }
171
172
    public function testUnpublishEach()
173
    {
174
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
175
        $this->seeder->run($request);
176
177
        $this->seeder->unpublishEach(Page::class);
178
179
        $pages = Page::get();
180
181
        foreach ($pages as $page) {
182
            $this->assertFalse($page->isPublished());
183
        }
184
    }
185
}
186