Completed
Push — master ( 3cca5d...b02bf5 )
by Robbie
10s
created

GenerateCSVJobTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 78
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A tearDown() 0 7 2
B testGenerateExport() 0 32 1
A createJob() 0 9 1
1
<?php
2
3
namespace SilverStripe\GridFieldQueuedExport\Tests;
4
5
use SilverStripe\Assets\Filesystem;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Dev\SapphireTest;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\GridfieldQueuedExport\Jobs\GenerateCSVJob;
11
12
class GenerateCSVJobTest extends SapphireTest
13
{
14
15
    protected static $fixture_file = 'GenerateCSVJobTest.yml';
16
17
    protected static $extra_dataobjects = [GenerateCSVJobTestRecord::class];
18
19
    protected static $extra_controllers = [GenerateCSVJobTestController::class];
20
21
    protected function setUp()
22
    {
23
        parent::setUp();
24
25
        Config::modify()->merge(Director::class, 'rules', [
26
            'jobtest//$Action/$ID/$OtherID' => GenerateCSVJobTestController::class
27
        ]);
28
    }
29
30
    protected $paths = [];
31
32
    protected function tearDown()
33
    {
34
        foreach ($this->paths as $path) {
35
            Filesystem::removeFolder(dirname($path));
36
        }
37
        parent::tearDown();
38
    }
39
40
    public function testGenerateExport()
41
    {
42
        // Build session
43
        $memberID = $this->logInWithPermission('ADMIN');
44
        $session = ['loggedInAs' => $memberID];
45
46
        // Build controller
47
        $controller = new GenerateCSVJobTestController();
48
        $form = $controller->Form();
49
        $gridfield = $form->Fields()->fieldByName('MyGridfield');
50
51
        // Build job
52
        $job = $this->createJob($gridfield, $session);
53
        $path = sprintf('%1$s/.exports/%2$s/%2$s.csv', ASSETS_PATH, $job->getSignature());
54
        $this->paths[] = $path; // Mark for cleanup later
55
56
        // Test that the job runs
57
        $this->assertFileNotExists($path);
58
        $job->process();
59
        $this->assertFileExists($path);
60
61
        // Test that the output matches the expected
62
        $expected = <<<EOS
63
"Title","Content","Publish On"
64
"Record 1","<p>""Record 1"" Body</p>","2015-01-01 23:34:01"
65
"Record 2","<p>""Record 2"" Body</p>","2015-01-02 23:34:01"
66
"Record 3","<p>""Record 3"" Body</p>","2015-01-03 23:34:01"
67
68
EOS;
69
        $actual = file_get_contents($path);
70
        $this->assertEquals($expected, $actual);
71
    }
72
73
    /**
74
     * Rough copy of GridFieldQueuedExportButton::startExport
75
     *
76
     * @param GridField $gridField
77
     * @param array $session
78
     * @return GenerateCSVJob
79
     */
80
    protected function createJob($gridField, $session)
81
    {
82
        $job = new GenerateCSVJob();
83
        $job->setGridField($gridField);
84
        $job->setSession($session);
85
        $job->setSeparator(',');
86
        $job->setIncludeHeader(true);
87
        return $job;
88
    }
89
}
90