Passed
Push — sheepy/introspection ( ff3f16...7fbbe0 )
by Marco
02:14
created

DocumentFactoryTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
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
use SilverStripe\View\ViewableData;
18
use SilverStripe\ORM\DataObject;
19
use SilverStripe\ErrorPage\ErrorPage;
20
21
class DocumentFactoryTest extends SapphireTest
22
{
23
    /**
24
     * We can't use the constant here for unknown reasons
25
     * If you change the constant, please replace id here with the appropriate value
26
     * @var array
27
     */
28
    protected static $expected_docs = [
29
        [
30
            'id'               => 'Page-1',
31
            'ObjectID'         => 1,
32
            'ClassName'        => 'Page',
33
            'ClassHierarchy'   =>
34
                [
35
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
36
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
37
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
38
                    'page'                               => 'Page',
39
                ],
40
            'ViewStatus'       =>
41
                [
42
                    0 => '1-null',
43
                ],
44
            'SiteTree_Title'   => 'Home',
45
            '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>",
46
        ],
47
        [
48
            'id'               => 'Page-2',
49
            'ObjectID'         => 2,
50
            'ClassName'        => 'Page',
51
            'ClassHierarchy'   =>
52
                [
53
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
54
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
55
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
56
                    'page'                               => 'Page',
57
                ],
58
            'ViewStatus'       =>
59
                [
60
                    0 => '1-null',
61
                ],
62
            'SiteTree_Title'   => 'About Us',
63
            'SiteTree_Content' => "<p>You can fill this page out with your own content, or delete it and create your own pages.</p>",
64
        ],
65
        [
66
            'id'               => 'Page-3',
67
            'ObjectID'         => 3,
68
            'ClassName'        => 'Page',
69
            'ClassHierarchy'   =>
70
                [
71
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
72
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
73
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
74
                    'page'                               => 'Page',
75
                ],
76
            'ViewStatus'       =>
77
                [
78
                    0 => '1-null',
79
                ],
80
            'SiteTree_Title'   => 'Contact Us',
81
            'SiteTree_Content' => "<p>You can fill this page out with your own content, or delete it and create your own pages.</p>",
82
        ],
83
        [
84
            'id'               => 'SilverStripe\\ErrorPage\\ErrorPage-4',
85
            'ObjectID'         => 4,
86
            'ClassName'        => ErrorPage::class,
87
            'ClassHierarchy'   =>
88
                [
89
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
90
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
91
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
92
                    'page'                               => 'Page',
93
                    'silverstripe\\errorpage\\errorpage' => ErrorPage::class,
94
                ],
95
            'ViewStatus'       =>
96
                [
97
                    0 => '1-null',
98
                ],
99
            'SiteTree_Title'   => 'Page not found',
100
            '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>',
101
        ],
102
        [
103
            'id'               => 'SilverStripe\\ErrorPage\\ErrorPage-5',
104
            'ObjectID'         => 5,
105
            'ClassName'        => ErrorPage::class,
106
            'ClassHierarchy'   =>
107
                [
108
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
109
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
110
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
111
                    'page'                               => 'Page',
112
                    'silverstripe\\errorpage\\errorpage' => ErrorPage::class,
113
                ],
114
            'ViewStatus'       =>
115
                [
116
                    0 => '1-null',
117
                ],
118
            'SiteTree_Title'   => 'Server error',
119
            'SiteTree_Content' => '<p>Sorry, there was a problem with handling your request.</p>',
120
        ],
121
    ];
122
123
    public static function setUpBeforeClass()
124
    {
125
        parent::setUpBeforeClass();
126
        // This is a hack on my local dev to make fixtures populate temporary database
127
        $conn = DB::get_conn();
128
        $dbName = self::tempDB()->build();
129
        $conn->selectDatabase($dbName);
130
        $dbAdmin = new DatabaseAdmin();
131
        $dbAdmin->setRequest(HTTPRequestBuilder::createFromEnvironment());
132
        $dbAdmin->doBuild(true, true, true);
133
    }
134
135
    public function testConstruct()
136
    {
137
        $factory = new DocumentFactory();
138
        $this->assertInstanceOf(SearchIntrospection::class, $factory->getIntrospection());
139
    }
140
141
    public function testBuildItems()
142
    {
143
        $factory = new DocumentFactory();
144
        $index = new TestIndex();
145
        $fields = $index->getFieldsForIndexing();
146
        $client = new Client([]);
147
        $update = $client->createUpdate();
148
        $count = 5;
149
        $factory->setClass(SiteTree::class);
150
        $factory->setItems(Page::get());
151
        $docs = $factory->buildItems($fields, $index, $update);
152
153
        $this->assertCount($count, $docs);
154
        $this->assertInternalType('array', $docs);
155
        $this->assertInstanceOf(BaseIndex::class, $factory->getIntrospection()->getIndex());
156
        /** @var Document $doc */
157
        foreach ($docs as $i => $doc) {
158
            // Debug::dump($doc->get
159
            $this->assertInstanceOf(Document::class, $doc);
160
            $fields = $doc->getFields();
161
            unset($fields['SiteTree_Created']); // Unset the Created, it changes per run
162
            $this->assertEquals(static::$expected_docs[$i], $fields);
163
        }
164
    }
165
}
166