SeederTaskTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 167
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testRemoveRelations() 0 11 2
A testGetFixture() 0 4 1
A testUnpublishEach() 0 11 2
B testParseFixture() 0 39 1
A testUnseed() 0 8 1
A testNoType() 0 9 1
A testParseFixtureNullFile() 0 5 1
A testRun() 0 18 1
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
    // @todo Try to get this test running properly. I don't know what's wrong
47
    // public function testLive()
48
    // {
49
    //     /** @var Kernel $kernel */
50
    //     $kernel = Injector::inst()->get(Kernel::class);
51
    //     $kernel->setEnvironment(Kernel::LIVE);
52
    //     $request = new HTTPRequest('GET', '', []);
53
    //     Debug::dump(Director::isLive());
54
    //     ob_start();
55
56
    //     $this->seeder->run($request);
57
58
    //     $this->assertContains('DO NOT RUN ME ON LIVE', ob_get_contents());
59
60
    //     ob_end_clean();
61
62
63
    //     $kernel->setEnvironment(Kernel::DEV);
64
    // }
65
66
    public function testRun()
67
    {
68
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
69
        $this->seeder->run($request);
70
71
        $pages = Page::get();
72
        $this->assertEquals(2, $pages->count());
73
74
        /** @var Page $page */
75
        $page = $pages->filter(['Title' => 'Samuel L. Lipsum'])->first();
76
77
        $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

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

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