Passed
Push — master ( 6ec46a...c58646 )
by Simon
02:37
created

SeederTaskTest::testUnpublishEach()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
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
class SeederTaskTest extends SapphireTest
14
{
15
    /**
16
     * @var SeederTask
17
     */
18
    protected $seeder;
19
20
    protected $usesDatabase = true;
21
22
    protected static $extra_dataobjects = [
23
        Mock\Page::class,
24
        Mock\Quote::class
25
    ];
26
27
    public function setUp()
28
    {
29
        parent::setUp();
30
        Config::modify()->update(SeederTask::class, 'Seedfile', 'tests/fixtures/seedertasktest.yml');
31
        $this->seeder = Injector::inst()->get(SeederTask::class);
32
    }
33
34
    public function testRun()
35
    {
36
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
37
        $this->seeder->run($request);
38
39
        $pages = Page::get();
40
        $this->assertEquals(2, $pages->count());
41
42
        /** @var Page $page */
43
        $page = $pages->filter(['Title' => 'Samuel L. Lipsum'])->first();
44
45
        $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

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

47
        $this->assertTrue($page->/** @scrutinizer ignore-call */ isPublished());
Loading history...
48
49
        $quotes = Quote::get();
50
51
        $this->assertEquals(2, $quotes->count());
52
    }
53
54
    public function testParseFixture()
55
    {
56
        SeederTask::setFixtureFile('tests/fixtures/seedertasktest.yml');
57
58
        $result = $this->seeder->parseFixture();
59
60
        $this->assertTrue(is_array($result));
61
62
        $expected = [
63
            Quote::class =>
64
                [
65
                    'quote1' =>
66
                        [
67
                            'quote' => 'Time is an illusion. Lunchtime doubly so.',
68
                        ],
69
                    'quote2' =>
70
                        [
71
                            '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.',
72
                        ],
73
                ],
74
            Page::class  =>
75
                [
76
                    'page1' =>
77
                        [
78
                            'Title'   => 'Samuel L. Lipsum',
79
                            '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>',
80
                            'Quotes'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote2',
81
                        ],
82
                    'page2' =>
83
                        [
84
                            'Title'   => 'Cat Lipsum',
85
                            '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>',
86
                            'Friend'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Page.page1',
87
                            'Quotes'  => '=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote1,=>Firesphere\\Seeder\\Tests\\Mock\\Quote.quote2',
88
                        ],
89
                ],
90
        ];
91
92
        $this->assertEquals($expected, $result);
93
    }
94
95
    public function testParseFixtureNullFile()
96
    {
97
        SeederTask::setFixtureFile(null);
98
99
        $this->assertTrue(is_array($this->seeder->parseFixture()));
100
    }
101
102
    /**
103
     * @expectedException \Exception
104
     */
105
    public function testNoType()
106
    {
107
        $request = new HTTPRequest('GET', '', []);
108
        $result = $this->seeder->run($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->seeder->run($request) targeting Firesphere\Seeder\Tasks\SeederTask::run() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
109
110
        $this->assertNull($result);
111
    }
112
113
    public function testGetFixture()
114
    {
115
        // As it's static, it's null by default in this situation
116
        $this->assertNull(SeederTask::getFixtureFile());
117
    }
118
119
    public function testUnseed()
120
    {
121
        $request = new HTTPRequest('GET', '', ['type' => 'unseed']);
122
        $this->seeder->run($request);
123
124
        $this->assertEquals(0, Page::get()->count());
125
126
        $this->assertEquals(0, Quote::get()->count());
127
    }
128
129
    public function testRemoveRelations()
130
    {
131
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
132
        $this->seeder->run($request);
133
134
        $this->seeder->removeManyMany(Page::class);
135
136
        $pages = Page::get();
137
138
        foreach ($pages as $page) {
139
            $this->assertEquals(0, (int)$page->Quotes()->count());
140
        }
141
    }
142
143
    public function testUnpublishEach()
144
    {
145
        $request = new HTTPRequest('GET', '', ['type' => 'seed']);
146
        $this->seeder->run($request);
147
148
        $this->seeder->unpublishEach(Page::class);
0 ignored issues
show
Bug introduced by
Firesphere\Seeder\Tests\Mock\Page::class of type string is incompatible with the type SilverStripe\ORM\DataObject expected by parameter $class of Firesphere\Seeder\Tasks\...erTask::unpublishEach(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

148
        $this->seeder->unpublishEach(/** @scrutinizer ignore-type */ Page::class);
Loading history...
149
150
        $pages = Page::get();
151
152
        foreach ($pages as $page) {
153
            $this->assertFalse($page->isPublished());
154
        }
155
    }
156
}
157