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