Passed
Push — master ( 167999...3de927 )
by Matthew
02:12
created

MapperTest::subTestExisting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Dynamic\Salsify\Tests\Model\Mapper;
4
5
use Dynamic\Salsify\ORM\SalsifyIDExtension;
6
use Dynamic\Salsify\Model\Mapper;
7
use Dynamic\Salsify\Task\ImportTask;
8
use Dynamic\Salsify\Tests\TestOnly\MappedObject;
9
use Dynamic\Salsify\Tests\TestOnly\MapperModification;
10
use Dynamic\Salsify\Tests\TestOnly\MapperSkipper;
11
use Exception;
12
use SilverStripe\Assets\Image;
13
use SilverStripe\Core\Config\Config;
14
use SilverStripe\Dev\SapphireTest;
15
16
/**
17
 * Class MapperTest
18
 * @package Dynamic\Salsify\Tests\Model\Mapper
19
 */
20
class MapperTest extends SapphireTest
21
{
22
23
    /**
24
     * @var string
25
     */
26
    protected static $fixture_file = '../fixtures.yml';
27
28
    /**
29
     * @var array
30
     */
31
    protected static $required_extensions = [
32
        Mapper::class => [
33
            MapperModification::class,
34
            MapperSkipper::class,
35
        ],
36
        MappedObject::class => [
37
            SalsifyIDExtension::class,
38
        ]
39
    ];
40
41
    /**
42
     * @var array
43
     */
44
    protected static $extra_dataobjects = [
45
        MappedObject::class,
46
    ];
47
48
    /**
49
     * @var string
50
     */
51
    private $importerKey = 'test';
52
53
    /**
54
     *
55
     */
56
    public function setUp()
57
    {
58
        Config::modify()->set(
59
            Mapper::class . '.' . $this->importerKey,
60
            'mapping',
61
            [
62
                MappedObject::class => [
63
                    'SalsifyID' => [
64
                        'salsifyField' => 'salsify:id',
65
                    ],
66
                    'SalsifyUpdatedAt' => 'salsify:updated_at',
67
                    'Unique' => [
68
                        'salsifyField' => 'custom-field-unique',
69
                        'unique' => true,
70
                        'shouldSkip' => 'testSkip',
71
                    ],
72
                    'Title' => 'custom-field-title',
73
                    'Seller' => [
74
                        'salsifyField' => 'custom-field-seller',
75
                        'unique' => false,
76
                    ],
77
                    'Modified' => [
78
                        'salsifyField' => 'custom-field-modified',
79
                        'modification' => 'testModification'
80
                    ],
81
                    'MainImage' => [
82
                        'salsifyField' => 'custom-field-front-image',
83
                        'type' => 'Image',
84
                    ],
85
                    'Images' => [
86
                        'salsifyField' => 'custom-field-images',
87
                        'type' => 'ManyImages',
88
                        'sortColumn' => 'SortOrder',
89
                    ],
90
                    'FallbackString' => [
91
                        'salsifyField' => 'YYYYY',
92
                        'fallback' => 'custom-field-title',
93
                    ],
94
                    'FallbackArray' => [
95
                        'salsifyField' => 'YYYYY',
96
                        'fallback' => [
97
                            'XXXXX',
98
                            'custom-field-seller',
99
                        ],
100
                    ],
101
                    'Unknown' => [
102
                        'unique' => true,
103
                    ],
104
                    'Unknown2' => [
105
                        'salsifyField' => 'XXXXX',
106
                    ],
107
                ],
108
            ]
109
        );
110
        Config::modify()->set(ImportTask::class, 'output', false);
111
        return parent::setUp();
112
    }
113
114
    /**
115
     * @throws Exception
116
     */
117
    public function testConstructorFailsWithoutMapping()
118
    {
119
        Config::modify()->remove(Mapper::class . '.' . $this->importerKey, 'mapping');
120
        $this->expectException(Exception::class);
121
        new Mapper($this->importerKey, __DIR__ . '/../data.json');
122
    }
123
124
    /**
125
     * @throws Exception
126
     */
127
    public function testConstructor()
128
    {
129
        $mapper = new Mapper($this->importerKey, __DIR__ . '/../data.json');
130
        $this->assertInstanceOf(Mapper::class, $mapper);
131
    }
132
133
    /**
134
     * @throws \Exception
135
     */
136
    public function testMap()
137
    {
138
        $mapper = new Mapper($this->importerKey, __DIR__ . '/../data.json');
139
        $mapper->map();
140
141
        // check to see if existing added
142
        $this->assertEquals(7, MappedObject::get()->count());
143
        $this->subTestExisting();
144
145
        // tests for unchanged
146
        $mapper = new Mapper($this->importerKey, __DIR__ . '/../data.json');
147
        $mapper->map();
148
149
        $this->subTestImages();
150
151
        $this->assertEquals(7, MappedObject::get()->count());
152
        $this->assertEquals('modified TEST_MOD', MappedObject::get()->find('Unique', '2')->Modified);
153
154
        // test fallbacks
155
        $this->assertEquals('Brooklyn Bridge', MappedObject::get()->find('Unique', '3')->FallbackString);
156
        $this->assertEquals('William McCloundy', MappedObject::get()->find('Unique', '3')->FallbackArray);
157
    }
158
159
    /**
160
     * Checks existing records to see if they got updated
161
     */
162
    private function subTestExisting()
163
    {
164
        // check to see if existing object with unique was modified
165
        $this->assertEquals('William McCloundy', MappedObject::get()->find('Unique', '3')->Seller);
166
        $this->assertEquals('00000000000002', MappedObject::get()->find('Unique', '3')->SalsifyID);
167
168
        // check to see if existing object with salsify id was modified
169
        $this->assertEquals('Victor Lustig', MappedObject::get()->find('Unique', '2')->Seller);
170
        $this->assertEquals('00000000000001', MappedObject::get()->find('Unique', '2')->SalsifyID);
171
    }
172
173
    /**
174
     * Tests images and sorting
175
     */
176
    private function subTestImages()
177
    {
178
        // test images
179
        $this->assertEquals(9, Image::get()->count());
180
        $this->assertEquals(2, MappedObject::get()->find('Unique', '3')->Images()->count());
181
        // first in image array is actually first with sort column specified
182
        $this->assertEquals(
183
            'DA-002-002',
184
            MappedObject::get()->find('Unique', '3')->Images()->sort('SortOrder')->first()->SalsifyID
185
        );
186
187
        $this->assertTrue(MappedObject::get()->first()->MainImageID > 0);
188
        $this->assertTrue(MappedObject::get()->last()->MainImageID > 0);
189
    }
190
}
191