Passed
Pull Request — master (#13)
by Matthew
01:38
created

MapperTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 40
c 4
b 0
f 0
dl 0
loc 96
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testMap() 0 18 1
A testConstructorFailsWithoutMapping() 0 5 1
A testConstructor() 0 4 1
A setUp() 0 31 1
1
<?php
2
3
namespace Dynamic\Salsify\Tests\Model\Mapper;
4
5
use Dynamic\Salsify\Model\Mapper;
6
use Dynamic\Salsify\Task\ImportTask;
7
use Dynamic\Salsify\Tests\TestOnly\MappedObject;
8
use Exception;
9
use SilverStripe\Assets\Image;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Dev\SapphireTest;
12
13
/**
14
 * Class MapperTest
15
 * @package Dynamic\Salsify\Tests\Model\Mapper
16
 */
17
class MapperTest extends SapphireTest
18
{
19
20
    /**
21
     * @var string
22
     */
23
    protected static $fixture_file = '../fixtures.yml';
24
25
    /**
26
     * @var array
27
     */
28
    protected static $extra_dataobjects = [
29
        MappedObject::class,
30
    ];
31
32
    /**
33
     * @var string
34
     */
35
    private $importerKey = 'test';
36
37
    /**
38
     *
39
     */
40
    public function setUp()
41
    {
42
        Config::modify()->set(
43
            Mapper::class . '.' . $this->importerKey,
44
            'mapping',
45
            [
46
                MappedObject::class => [
47
                    'Unique' => [
48
                        'salsifyField' => 'custom-field-unique',
49
                        'unique' => true,
50
                    ],
51
                    'Title' => 'custom-field-title',
52
                    'Seller' => [
53
                        'salsifyField' => 'custom-field-seller',
54
                        'unique' => false,
55
                    ],
56
                    'Image' => [
57
                        'salsifyField' => 'custom-field-image',
58
                        'type' => 'IMAGE',
59
                    ],
60
                    'Unknown' => [
61
                        'unique' => true,
62
                    ],
63
                    'Unknown2' => [
64
                        'salsifyField' => 'XXXXX',
65
                    ],
66
                ],
67
            ]
68
        );
69
        Config::modify()->set(ImportTask::class, 'output', false);
70
        return parent::setUp();
71
    }
72
73
    /**
74
     * @throws Exception
75
     */
76
    public function testConstructorFailsWithoutMapping()
77
    {
78
        Config::modify()->remove(Mapper::class . '.' . $this->importerKey, 'mapping');
79
        $this->expectException(Exception::class);
80
        new Mapper($this->importerKey, __DIR__ . '/../data.json');
81
    }
82
83
    /**
84
     * @throws Exception
85
     */
86
    public function testConstructor()
87
    {
88
        $mapper = new Mapper($this->importerKey, __DIR__ . '/../data.json');
89
        $this->assertInstanceOf(Mapper::class, $mapper);
90
    }
91
92
    /**
93
     * @throws \Exception
94
     */
95
    public function testMap()
96
    {
97
        $this->assertEquals(1, MappedObject::get()->count());
98
99
        $mapper = new Mapper($this->importerKey, __DIR__ . '/../data.json');
100
        $mapper->map();
101
102
        // check to see if added
103
        $this->assertEquals(7, MappedObject::get()->count());
104
        // check to see if existing object was modified
105
        $this->assertEquals('William McCloundy', MappedObject::get()->find('Unique', '3')->Seller);
106
107
        // tests for unchanged
108
        $mapper = new Mapper($this->importerKey, __DIR__ . '/../data.json');
109
        $mapper->map();
110
        $this->assertEquals(7, MappedObject::get()->count());
111
        $this->assertEquals(7, Image::get()->count());
112
        $this->assertTrue(MappedObject::get()->first()->ImageID > 0);
113
    }
114
}
115