Passed
Push — sheepy/introspection ( 851cdd...69e16c )
by Marco
06:22
created

DocumentFactoryTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Tests;
5
6
use Firesphere\SolrSearch\Extensions\DataObjectExtension;
7
use Firesphere\SolrSearch\Factories\DocumentFactory;
8
use Firesphere\SolrSearch\Helpers\SearchIntrospection;
9
use Firesphere\SolrSearch\Indexes\BaseIndex;
10
use Page;
11
use Psr\Log\LoggerInterface;
12
use SilverStripe\CMS\Model\SiteTree;
13
use SilverStripe\Core\Config\Config;
14
use SilverStripe\Core\Injector\Injector;
15
use SilverStripe\Dev\SapphireTest;
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
21
class DocumentFactoryTest extends SapphireTest
22
{
23
    protected $usesDatabase = true;
24
    protected static $extra_dataobjects = [
25
        TestObject::class,
26
        TestPage::class,
27
        TestRelationObject::class,
28
    ];
29
30
    /**
31
     * We can't use the constant here for unknown reasons
32
     * If you change the constant, please replace id here with the appropriate value
33
     * @var array
34
     */
35
    protected static $expected_docs = [
36
        [
37
            'id'               => 'Page-1',
38
            'ObjectID'         => 1,
39
            'ClassName'        => 'Page',
40
            'ClassHierarchy'   =>
41
                [
42
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
43
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
44
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
45
                    'page'                               => 'Page',
46
                ],
47
            'ViewStatus'       =>
48
                [
49
                    0 => '1-null',
50
                ],
51
            'SiteTree_Title'   => 'Home',
52
            '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>",
53
        ],
54
        [
55
            'id'               => 'Page-2',
56
            'ObjectID'         => 2,
57
            'ClassName'        => 'Page',
58
            'ClassHierarchy'   =>
59
                [
60
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
61
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
62
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
63
                    'page'                               => 'Page',
64
                ],
65
            'ViewStatus'       =>
66
                [
67
                    0 => '1-null',
68
                ],
69
            'SiteTree_Title'   => 'About Us',
70
            'SiteTree_Content' => "<p>You can fill this page out with your own content, or delete it and create your own pages.</p>",
71
        ],
72
        [
73
            'id'               => 'Page-3',
74
            'ObjectID'         => 3,
75
            'ClassName'        => 'Page',
76
            'ClassHierarchy'   =>
77
                [
78
                    'silverstripe\\view\\viewabledata'   => ViewableData::class,
79
                    'silverstripe\\orm\\dataobject'      => DataObject::class,
80
                    'silverstripe\\cms\\model\\sitetree' => SiteTree::class,
81
                    'page'                               => 'Page',
82
                ],
83
            'ViewStatus'       =>
84
                [
85
                    0 => '1-null',
86
                ],
87
            'SiteTree_Title'   => 'Contact Us',
88
            'SiteTree_Content' => "<p>You can fill this page out with your own content, or delete it and create your own pages.</p>",
89
        ]
90
    ];
91
92
    public function setUp()
93
    {
94
        parent::setUp();
95
        Injector::inst()->get(Page::class)->requireDefaultRecords();
96
    }
97
98
    public function testConstruct()
99
    {
100
        $factory = new DocumentFactory();
101
        $this->assertInstanceOf(SearchIntrospection::class, $factory->getIntrospection());
102
    }
103
104
    public function testBuildItems()
105
    {
106
        $factory = new DocumentFactory();
107
        $index = new TestIndex();
108
        $fields = $index->getFieldsForIndexing();
109
        $client = new Client([]);
110
        $update = $client->createUpdate();
111
        $factory->setClass(SiteTree::class);
112
        $factory->setItems(Page::get());
113
        $docs = $factory->buildItems($fields, $index, $update);
114
115
        $this->assertCount(SiteTree::get()->count(), $docs);
116
        $this->assertInternalType('array', $docs);
117
        $this->assertInstanceOf(BaseIndex::class, $factory->getIntrospection()->getIndex());
118
        /** @var Document $doc */
119
        foreach ($docs as $i => $doc) {
120
            // Debug::dump($doc->get
121
            $this->assertInstanceOf(Document::class, $doc);
122
            $fields = $doc->getFields();
123
            unset($fields['SiteTree_Created']); // Unset the Created, it changes per run
124
            $this->assertEquals(static::$expected_docs[$i], $fields);
125
        }
126
    }
127
128
    public function testSanitiseField()
129
    {
130
        $factory = new DocumentFactory();
131
132
        $this->assertEquals('hello', $factory->sanitiseName('Test\\Name\\hello'));
133
    }
134
135
    public function testGetLogger()
136
    {
137
        $this->assertInstanceOf(LoggerInterface::class, (new DocumentFactory())->getLogger());
138
    }
139
}
140