Completed
Push — 1.0 ( 3aa83c...76adfb )
by Morven
01:18
created

CSVBulkLoaderTest::testFieldLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ilateral\SilverStripe\SlightlyBetterBulkLoader\Tests;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\Dev\SapphireTest;
7
use ilateral\SilverStripe\SlightlyBetterBulkLoader\CsvBulkLoader;
8
use ilateral\SilverStripe\SlightlyBetterBulkLoader\Tests\Data\Player;
9
10
class CSVBulkLoaderTest extends SapphireTest
11
{
12
13
    /**
14
     * Name of csv test dir
15
     *
16
     * @var string
17
     */
18
    protected $csv_path = null;
19
20
    protected static $extra_dataobjects = [
21
        Player::class
22
    ];
23
24
    protected function setUp()
25
    {
26
        parent::setUp();
27
        $this->csvPath = __DIR__ . '/csv/';
0 ignored issues
show
Bug introduced by
The property csvPath does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
    }
29
30
    /**
31
     * Does the importer correct CSV
32
     */
33 View Code Duplication
    public function testLoad()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $loader = new CsvBulkLoader(Player::class);
36
        $filepath = $this->csvPath . 'Player.csv';
37
        $file = fopen($filepath, 'r');
38
        $results = $loader->load($filepath);
39
40
        $this->assertEquals(
41
            5,
42
            $results->CreatedCount(),
43
            'Test correct count of imported data'
44
        );
45
46
        $obj = DataObject::get_one(
47
            Player::class,
48
            [ "FirstName" => 'John' ]
49
        );
50
51
        $this->assertNotNull($obj);
52
        $this->assertEquals("He's a good guy", $obj->Biography);
53
        $this->assertEquals("1988-01-31", $obj->Birthday);
54
        $this->assertEquals("1", $obj->IsRegistered);
55
        fclose($file);
56
    }
57
58
    /**
59
     * Does the importer correct CSV when using a Field Label?
60
     */
61 View Code Duplication
    public function testFieldLabel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        $loader = new CsvBulkLoader(Player::class);
64
        $filepath = $this->csvPath . 'PlayerFieldLabel.csv';
65
        $file = fopen($filepath, 'r');
66
        $results = $loader->load($filepath);
67
68
        $this->assertEquals(
69
            5,
70
            $results->CreatedCount(),
71
            'Test correct count of imported data'
72
        );
73
74
        $obj = DataObject::get_one(
75
            Player::class,
76
            [ "FirstName" => 'John' ]
77
        );
78
79
        $this->assertNotNull($obj);
80
        $this->assertEquals("He's a good guy", $obj->Biography);
81
        $this->assertEquals("1988-01-31", $obj->Birthday);
82
        $this->assertEquals("1", $obj->IsRegistered);
83
        fclose($file);
84
    }
85
86
    /**
87
     * Does the importer fail on a duplicate column?
88
     */
89 View Code Duplication
    public function testDuplicateColumn()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        // Remove any existing records
92
        Player::get()->removeAll();
93
94
        $loader = new CsvBulkLoader(Player::class);
95
        $filepath = $this->csvPath . 'PlayerDuplicate.csv';
96
        $file = fopen($filepath, 'r');
97
        $results = $loader->load($filepath);
98
99
        $this->assertEquals(
100
            1,
101
            $results->ErrorCount(),
102
            'Test error count on duplicates'
103
        );
104
105
        $obj = DataObject::get_one(
106
            Player::class,
107
            [ "FirstName" => 'John' ]
108
        );
109
110
        $this->assertNull($obj);
111
        fclose($file);
112
    }
113
114
    /**
115
     * Does the importer fail on a duplicate column
116
     */
117 View Code Duplication
    public function testMissingRequired()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119
        // Remove any existing records
120
        Player::get()->removeAll();
121
122
        $loader = new CsvBulkLoader(Player::class);
123
        $filepath = $this->csvPath . 'PlayerMissingRequired.csv';
124
        $file = fopen($filepath, 'r');
125
        $results = $loader->load($filepath);
126
127
        $this->assertEquals(
128
            1,
129
            $results->ErrorCount(),
130
            'Test errors generated when required fields missing'
131
        );
132
133
        $obj = DataObject::get_one(
134
            Player::class,
135
            ["FirstName" => 'Jane']
136
        );
137
138
        $this->assertNull($obj);
139
        fclose($file);
140
    }
141
}
142