Passed
Push — master ( e4f53a...6ee727 )
by Simon
01:39
created

SeederTaskTest::testGetFixture()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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