Completed
Push — master ( b3c891...4f2776 )
by Hamish
7s
created

GenerateCSVJobTest_Controller   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 7
dl 0
loc 19
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Link() 0 3 1
A Form() 0 11 1
1
<?php
2
3
class GenerateCSVJobTest extends SapphireTest {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
5
    protected static $fixture_file = 'GenerateCSVJobTest.yml';
6
7
    protected $extraDataObjects = array('GenerateCSVJobTest_Record');
8
9
    public function setUp() {
10
        parent::setUp();
11
        Config::inst()->update('Director', 'rules', array(
12
            'jobtest//$Action/$ID/$OtherID' => 'GenerateCSVJobTest_Controller'
13
        ));
14
    }
15
16
    protected $paths = array();
17
18
    public function tearDown() {
19
        foreach($this->paths as $path) {
20
            Filesystem::removeFolder(dirname($path));
21
        }
22
        parent::tearDown();
23
    }
24
25
    public function testGenerateExport() {
26
        // Build session
27
        $memberID = $this->logInWithPermission('ADMIN');
28
        $session = array('loggedInAs' => $memberID);
29
30
        // Build controller
31
        $controller = new GenerateCSVJobTest_Controller();
32
        $form = $controller->Form();
33
        $gridfield = $form->Fields()->fieldByName('MyGridfield');
34
35
        // Build job
36
        $job = $this->createJob($gridfield, $session);
37
        $path = sprintf('%1$s/.exports/%2$s/%2$s.csv', ASSETS_PATH, $job->getSignature());
38
        $this->paths[] = $path; // Mark for cleanup later
39
40
        // Test that the job runs
41
        $this->assertFileNotExists($path);
0 ignored issues
show
Bug introduced by
The method assertFileNotExists() does not seem to exist on object<GenerateCSVJobTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
        $job->process();
43
        $this->assertFileExists($path);
0 ignored issues
show
Bug introduced by
The method assertFileExists() does not seem to exist on object<GenerateCSVJobTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
45
        // Test that the output matches the expected
46
        $expected = <<<EOS
47
"Title","Content","Publish On"
48
"Record 1","<p>""Record 1"" Body</p>","2015-01-01 23:34:01"
49
"Record 2","<p>""Record 2"" Body</p>","2015-01-02 23:34:01"
50
"Record 3","<p>""Record 3"" Body</p>","2015-01-03 23:34:01"
51
52
EOS;
53
        $actual = file_get_contents($path);
54
        $this->assertEquals($expected, $actual);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<GenerateCSVJobTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
    }
56
57
    /**
58
     * Rough copy of GridFieldQueuedExportButton::startExport
59
     *
60
     * @param GridField $gridField
61
     * @param array $session
62
     * @return GenerateCSVJob
63
     */
64
    protected function createJob($gridField, $session) {
65
        $job = new GenerateCSVJob();
66
        $job->setGridField($gridField);
67
        $job->setSession($session);
68
        $job->setSeparator(',');
69
        $job->setIncludeHeader(true);
70
        return $job;
71
    }
72
}
73
74
75
class GenerateCSVJobTest_Record extends DataObject implements TestOnly {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
76
77
    private static $summary_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $summary_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
78
        'Title',
79
        'Content',
80
        'PublishOn',
81
    );
82
83
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
84
        'Title' => 'Varchar',
85
        'Content' => 'Varchar',
86
        'PublishOn' => 'SS_DateTime'
87
    );
88
}
89
90
class GenerateCSVJobTest_Controller extends Controller implements TestOnly {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
91
    private static $allowed_actions = array('Form');
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
92
93
    public function Link() {
94
        return 'jobtest/';
95
    }
96
97
    public function Form() {
98
        // Get records
99
        $records = GenerateCSVJobTest_Record::get();
100
101
        // Set config
102
        $config = GridFieldConfig_RecordEditor::create();
103
        $config->removeComponentsByType('GridFieldExportButton');
104
        $config->addComponent(new GridFieldQueuedExportButton('buttons-after-left'));
105
        $fields = new GridField('MyGridfield', 'My Records', $records, $config);
106
        return new Form($this, 'Form', new FieldList($fields), new FieldList());
107
    }
108
}
109