Passed
Pull Request — master (#77)
by Marco
04:31 queued 02: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 Psr\Log\LoggerInterface;
11
use SilverStripe\CMS\Model\SiteTree;
12
use SilverStripe\Control\HTTPRequestBuilder;
13
use SilverStripe\Dev\SapphireTest;
14
use SilverStripe\ORM\DatabaseAdmin;
15
use SilverStripe\ORM\DB;
16
use Solarium\Core\Client\Client;
17
use Solarium\QueryType\Update\Query\Document\Document;
18
use SilverStripe\View\ViewableData;
19
use SilverStripe\ORM\DataObject;
20
use SilverStripe\ErrorPage\ErrorPage;
21
22
class DocumentFactoryTest extends SapphireTest
23
{
24
    /**
25
     * We can't use the constant here for unknown reasons
26
     * If you change the constant, please replace id here with the appropriate value
27
     * @var array
28
     */
29
    protected static $expected_docs = [
30
        [
31
            'id'               => 'Page-1',
32
            'ObjectID'         => 1,
33
            'ClassName'        => 'Page',
34
            'ClassHierarchy'   =>
35
                [
36
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
37
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
38
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
39
                    'page'                               => 'Page',
40
                ],
41
            'ViewStatus'       =>
42
                [
43
                    0 => '1-null',
44
                ],
45
            'SiteTree_Title'   => 'Home',
46
            '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>",
47
        ],
48
        [
49
            'id'               => 'Page-2',
50
            'ObjectID'         => 2,
51
            'ClassName'        => 'Page',
52
            'ClassHierarchy'   =>
53
                [
54
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
55
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
56
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
57
                    'page'                               => 'Page',
58
                ],
59
            'ViewStatus'       =>
60
                [
61
                    0 => '1-null',
62
                ],
63
            'SiteTree_Title'   => 'About Us',
64
            'SiteTree_Content' => "<p>You can fill this page out with your own content, or delete it and create your own pages.</p>",
65
        ],
66
        [
67
            'id'               => 'Page-3',
68
            'ObjectID'         => 3,
69
            'ClassName'        => 'Page',
70
            'ClassHierarchy'   =>
71
                [
72
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
73
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
74
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
75
                    'page'                               => 'Page',
76
                ],
77
            'ViewStatus'       =>
78
                [
79
                    0 => '1-null',
80
                ],
81
            'SiteTree_Title'   => 'Contact Us',
82
            'SiteTree_Content' => "<p>You can fill this page out with your own content, or delete it and create your own pages.</p>",
83
        ],
84
        [
85
            'id'               => 'SilverStripe\\ErrorPage\\ErrorPage-4',
86
            'ObjectID'         => 4,
87
            'ClassName'        => ErrorPage::class,
88
            'ClassHierarchy'   =>
89
                [
90
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
91
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
92
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
93
                    'page'                               => 'Page',
94
                    'silverstripe\\errorpage\\errorpage' => ErrorPage::class,
95
                ],
96
            'ViewStatus'       =>
97
                [
98
                    0 => '1-null',
99
                ],
100
            'SiteTree_Title'   => 'Page not found',
101
            '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>',
102
        ],
103
        [
104
            'id'               => 'SilverStripe\\ErrorPage\\ErrorPage-5',
105
            'ObjectID'         => 5,
106
            'ClassName'        => ErrorPage::class,
107
            'ClassHierarchy'   =>
108
                [
109
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
110
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
111
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
112
                    'page'                               => 'Page',
113
                    'silverstripe\\errorpage\\errorpage' => ErrorPage::class,
114
                ],
115
            'ViewStatus'       =>
116
                [
117
                    0 => '1-null',
118
                ],
119
            'SiteTree_Title'   => 'Server error',
120
            'SiteTree_Content' => '<p>Sorry, there was a problem with handling your request.</p>',
121
        ],
122
    ];
123
124
    public static function setUpBeforeClass()
125
    {
126
        parent::setUpBeforeClass();
127
        // This is a hack on my local dev to make fixtures populate temporary database
128
        $conn = DB::get_conn();
129
        $dbName = self::tempDB()->build();
130
        $conn->selectDatabase($dbName);
131
        $dbAdmin = new DatabaseAdmin();
132
        $dbAdmin->setRequest(HTTPRequestBuilder::createFromEnvironment());
133
        $dbAdmin->doBuild(true, true, true);
134
    }
135
136
    public function testConstruct()
137
    {
138
        $factory = new DocumentFactory();
139
        $this->assertInstanceOf(SearchIntrospection::class, $factory->getIntrospection());
140
    }
141
142
    public function testBuildItems()
143
    {
144
        $factory = new DocumentFactory();
145
        $index = new TestIndex();
146
        $fields = $index->getFieldsForIndexing();
147
        $client = new Client([]);
148
        $update = $client->createUpdate();
149
        $factory->setClass(SiteTree::class);
150
        $factory->setItems(Page::get());
151
        $docs = $factory->buildItems($fields, $index, $update);
152
153
        $this->assertCount(SiteTree::get()->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
    public function testSanitiseField()
167
    {
168
        $factory = new DocumentFactory();
169
170
        $this->assertEquals('hello', $factory->sanitiseName('Test\\Name\\hello'));
171
    }
172
173
    public function testGetLogger()
174
    {
175
        $this->assertInstanceOf(LoggerInterface::class, (new DocumentFactory())->getLogger());
176
    }
177
}
178