Passed
Pull Request — master (#77)
by Marco
08:43
created

DocumentFactoryTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Tests;
5
6
use Firesphere\SolrSearch\Factories\DocumentFactory;
7
use Firesphere\SolrSearch\Helpers\SearchIntrospection;
8
use Firesphere\SolrSearch\Indexes\BaseIndex;
9
use Page;
10
use SilverStripe\CMS\Model\SiteTree;
11
use SilverStripe\Control\HTTPRequestBuilder;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\ORM\DatabaseAdmin;
14
use SilverStripe\ORM\DB;
15
use Solarium\Core\Client\Client;
16
use Solarium\QueryType\Update\Query\Document\Document;
17
18
class DocumentFactoryTest extends SapphireTest
19
{
20
    protected static $expected_docs = [
21
        [
22
            '_documentid'      => 'Page-1',
23
            'ID'               => 1,
24
            'ClassName'        => 'Page',
25
            'ClassHierarchy'   =>
26
                [
27
                    'silverstripe\\view\\viewabledata'   => 'SilverStripe\\View\\ViewableData',
28
                    'silverstripe\\orm\\dataobject'      => 'SilverStripe\\ORM\\DataObject',
29
                    'silverstripe\\cms\\model\\sitetree' => 'SilverStripe\\CMS\\Model\\SiteTree',
30
                    'page'                               => 'Page',
31
                ],
32
            'ViewStatus'       =>
33
                [
34
                    0 => '1-null',
35
                ],
36
            'SiteTree_Title'   => 'Home',
37
            'SiteTree_Content' => "<p>Welcome to SilverStripe! This is the default homepage. You can edit this page by opening <a href=\"admin/\">the CMS</a>.</p><p>You can now access the <a href=\"http://docs.silverstripe.org\">developer documentation</a>, or begin the <a href=\"http://www.silverstripe.org/learn/lessons\">SilverStripe lessons</a>.</p>",
38
        ],
39
        [
40
            '_documentid'      => 'Page-2',
41
            'ID'               => 2,
42
            'ClassName'        => 'Page',
43
            'ClassHierarchy'   =>
44
                [
45
                    'silverstripe\\view\\viewabledata'   => 'SilverStripe\\View\\ViewableData',
46
                    'silverstripe\\orm\\dataobject'      => 'SilverStripe\\ORM\\DataObject',
47
                    'silverstripe\\cms\\model\\sitetree' => 'SilverStripe\\CMS\\Model\\SiteTree',
48
                    'page'                               => 'Page',
49
                ],
50
            'ViewStatus'       =>
51
                [
52
                    0 => '1-null',
53
                ],
54
            'SiteTree_Title'   => 'About Us',
55
            'SiteTree_Content' => "<p>You can fill this page out with your own content, or delete it and create your own pages.</p>",
56
        ],
57
        [
58
            '_documentid'      => 'Page-3',
59
            'ID'               => 3,
60
            'ClassName'        => 'Page',
61
            'ClassHierarchy'   =>
62
                [
63
                    'silverstripe\\view\\viewabledata'   => 'SilverStripe\\View\\ViewableData',
64
                    'silverstripe\\orm\\dataobject'      => 'SilverStripe\\ORM\\DataObject',
65
                    'silverstripe\\cms\\model\\sitetree' => 'SilverStripe\\CMS\\Model\\SiteTree',
66
                    'page'                               => 'Page',
67
                ],
68
            'ViewStatus'       =>
69
                [
70
                    0 => '1-null',
71
                ],
72
            'SiteTree_Title'   => 'Contact Us',
73
            'SiteTree_Content' => "<p>You can fill this page out with your own content, or delete it and create your own pages.</p>",
74
        ],
75
        [
76
            '_documentid'      => 'SilverStripe\\ErrorPage\\ErrorPage-4',
77
            'ID'               => 4,
78
            'ClassName'        => 'SilverStripe\\ErrorPage\\ErrorPage',
79
            'ClassHierarchy'   =>
80
                [
81
                    'silverstripe\\view\\viewabledata'   => 'SilverStripe\\View\\ViewableData',
82
                    'silverstripe\\orm\\dataobject'      => 'SilverStripe\\ORM\\DataObject',
83
                    'silverstripe\\cms\\model\\sitetree' => 'SilverStripe\\CMS\\Model\\SiteTree',
84
                    'page'                               => 'Page',
85
                    'silverstripe\\errorpage\\errorpage' => 'SilverStripe\\ErrorPage\\ErrorPage',
86
                ],
87
            'ViewStatus'       =>
88
                [
89
                    0 => '1-null',
90
                ],
91
            'SiteTree_Title'   => 'Page not found',
92
            'SiteTree_Content' => '<p>Sorry, it seems you were trying to access a page that doesn\'t exist.</p><p>Please check the spelling of the URL you were trying to access and try again.</p>',
93
        ],
94
        [
95
            '_documentid'      => 'SilverStripe\\ErrorPage\\ErrorPage-5',
96
            'ID'               => 5,
97
            'ClassName'        => 'SilverStripe\\ErrorPage\\ErrorPage',
98
            'ClassHierarchy'   =>
99
                [
100
                    'silverstripe\\view\\viewabledata'   => 'SilverStripe\\View\\ViewableData',
101
                    'silverstripe\\orm\\dataobject'      => 'SilverStripe\\ORM\\DataObject',
102
                    'silverstripe\\cms\\model\\sitetree' => 'SilverStripe\\CMS\\Model\\SiteTree',
103
                    'page'                               => 'Page',
104
                    'silverstripe\\errorpage\\errorpage' => 'SilverStripe\\ErrorPage\\ErrorPage',
105
                ],
106
            'ViewStatus'       =>
107
                [
108
                    0 => '1-null',
109
                ],
110
            'SiteTree_Title'   => 'Server error',
111
            'SiteTree_Content' => '<p>Sorry, there was a problem with handling your request.</p>',
112
        ],
113
    ];
114
    public static function setUpBeforeClass()
115
    {
116
        parent::setUpBeforeClass();
117
        // This is a hack on my local dev to make fixtures populate temporary database
118
        $conn = DB::get_conn();
119
        $dbName = self::tempDB()->build();
120
        $conn->selectDatabase($dbName);
121
        $dbAdmin = new DatabaseAdmin();
122
        $dbAdmin->setRequest(HTTPRequestBuilder::createFromEnvironment());
123
        $dbAdmin->doBuild(true, true, true);
124
    }
125
126
    public function testConstruct()
127
    {
128
        $factory = new DocumentFactory();
129
        $this->assertInstanceOf(SearchIntrospection::class, $factory->getIntrospection());
130
    }
131
132
    public function testBuildItems()
133
    {
134
        $factory = new DocumentFactory();
135
        $index = new TestIndex();
136
        $fields = $index->getFieldsForIndexing();
137
        $client = new Client([]);
138
        $update = $client->createUpdate();
139
        $count = 5;
140
        $factory->setClass(SiteTree::class);
141
        $factory->setItems(Page::get());
142
        $docs = $factory->buildItems($fields, $index, $update);
143
144
        $this->assertCount($count, $docs);
145
        $this->assertInternalType('array', $docs);
146
        $this->assertInstanceOf(BaseIndex::class, $factory->getIntrospection()->getIndex());
147
        /** @var Document $doc */
148
        foreach ($docs as $i => $doc) {
149
            // Debug::dump($doc->get
150
            $this->assertInstanceOf(Document::class, $doc);
151
            $fields = $doc->getFields();
152
            unset($fields['SiteTree_Created']); // Unset the Created, it changes per run
153
            $this->assertEquals(static::$expected_docs[$i], $fields);
154
        }
155
    }
156
}
157