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