Passed
Push — master ( 128ca3...35f67e )
by Simon
01:39
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 testRun()
47
    {
48
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
49
        $this->seeder->run($request);
50
51
        $pages = Page::get();
52
        $this->assertEquals(2, $pages->count());
53
54
        /** @var Page $page */
55
        $page = $pages->filter(['Title' => 'Samuel L. Lipsum'])->first();
56
57
        $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

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

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