Passed
Push — master ( 446f64...4b8a3e )
by Simon
02:08
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\HTTPRequest;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Dev\SapphireTest;
12
13
/**
14
 * Class SeederTaskTest
15
 * @package Firesphere\Seeder\Tests
16
 */
17
class SeederTaskTest extends SapphireTest
18
{
19
    /**
20
     * @var SeederTask
21
     */
22
    protected $seeder;
23
24
    /**
25
     * @var bool
26
     */
27
    protected $usesDatabase = true;
28
29
    /**
30
     * @var array
31
     */
32
    protected static $extra_dataobjects = [
33
        Mock\Page::class,
34
        Mock\Quote::class
35
    ];
36
37
    public function setUp()
38
    {
39
        parent::setUp();
40
        Config::modify()->update(SeederTask::class, 'Seedfile', 'tests/fixtures/seedertasktest.yml');
41
        $this->seeder = Injector::inst()->get(SeederTask::class);
42
    }
43
44
    public function testLive()
45
    {
46
        $file = Director::baseFolder() . '/.env';
0 ignored issues
show
Bug introduced by
The type Firesphere\Seeder\Tests\Director was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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