Passed
Push — master ( 9503f0...488e4f )
by Petr
02:34
created

FilesTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 53
c 2
b 0
f 0
dl 0
loc 112
rs 10
1
<?php
2
3
namespace ControlTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_forms\Controls;
8
use kalanis\kw_forms\Exceptions\EntryException;
9
use kalanis\kw_forms\Exceptions\FormsException;
10
use kalanis\kw_forms\Exceptions\RenderException;
11
use kalanis\kw_rules\Interfaces\IRules;
12
13
14
class FilesTest extends CommonTestClass
15
{
16
    /**
17
     * @throws RenderException
18
     */
19
    public function testFile(): void
20
    {
21
        $input = new Controls\File();
22
        $input->set('myown', 'original');
23
        $this->assertEquals('<input type="file" id="myown" name="myown" />', $input->renderInput());
24
        $input->setValue('jhgfd');
25
        $this->assertEquals('<input type="file" id="myown" name="myown" />', $input->renderInput());
26
    }
27
28
    /**
29
     * @throws RenderException
30
     */
31
    public function testFiles(): void
32
    {
33
        $input = new Controls\Files();
34
        $input->set('myown', ['one', 'two'], 'Upload files');
35
        $this->assertEquals(
36
  '<label for="myown_0">one</label> <input type="file" id="myown_0" name="myown[]" /> ' . PHP_EOL
37
. '<label for="myown_1">two</label> <input type="file" id="myown_1" name="myown[]" /> ' . PHP_EOL, $input->renderInput());
38
    }
39
40
    /**
41
     * @throws RenderException
42
     */
43
    public function testFiles2(): void
44
    {
45
        $input = new Controls\Files();
46
        $input->set('myown', ['foo' => 'one', 'bar' => 'two'], 'Upload files');
47
        $this->assertEquals(
48
  '<label for="myown_0">one</label> <input type="file" id="myown_0" name="myown[foo]" /> ' . PHP_EOL
49
. '<label for="myown_1">two</label> <input type="file" id="myown_1" name="myown[bar]" /> ' . PHP_EOL, $input->renderInput());
50
    }
51
52
    /**
53
     * @throws EntryException
54
     * @throws FormsException
55
     * @throws RenderException
56
     */
57
    public function testFiles3(): void
58
    {
59
        $adapter = new \Files();
60
        $adapter->loadEntries('');
61
        $adapter->rewind();
62
63
        $input = new Controls\Files();
64
        $input->set('download', ['file1' => 'one', 'file2' => 'two'], 'Upload files');
65
        $input->setValues(iterator_to_array($adapter));
66
67
        $this->assertNotEmpty($input->getValues());
68
        $this->assertEquals(
69
  '<label for="download_0">one</label> <input type="file" id="download_0" name="download[file1]" /> ' . PHP_EOL
70
. '<label for="download_1">two</label> <input type="file" id="download_1" name="download[file2]" /> ' . PHP_EOL, $input->renderInput());
71
    }
72
73
    /**
74
     * @throws FormsException
75
     * @throws RenderException
76
     */
77
    public function testFiles4(): void
78
    {
79
        $adapter = new \Files();
80
        $adapter->loadEntries('');
81
        $adapter->rewind();
82
83
        $input = new Controls\Files();
84
        $input->set('numbered', ['one', 'two'], 'Upload files');
85
        $input->setValues(iterator_to_array($adapter));
86
87
        $this->assertEquals(
88
  '<label for="numbered_0">one</label> <input type="file" id="numbered_0" name="numbered[]" /> ' . PHP_EOL
89
. '<label for="numbered_1">two</label> <input type="file" id="numbered_1" name="numbered[]" /> ' . PHP_EOL, $input->renderInput());
90
    }
91
92
    /**
93
     * @throws EntryException
94
     */
95
    public function testFileUnknown(): void
96
    {
97
        $input = new Controls\File();
98
        $input->set('files', 'not_posted');
99
        $input->addRule(IRules::FILE_EXISTS, 'file must exist');
100
        $this->expectException(EntryException::class);
101
        $input->getFile();
102
    }
103
104
    /**
105
     * @throws EntryException
106
     * @throws FormsException
107
     * @throws RenderException
108
     */
109
    public function testFileInput(): void
110
    {
111
        $adapter = new \Files();
112
        $adapter->loadEntries('');
113
        $adapter->rewind();
114
115
        $input = new Controls\File();
116
        $input->set('files', 'posted');
117
        $input->setValue($adapter->current());
118
119
        $this->assertEquals('<input type="file" id="files" name="files" />', $input->renderInput());
120
        $this->assertEquals('facepalm.jpg', $input->getValue());
121
        $this->assertEquals('image/jpeg', $input->getMimeType());
122
        $this->assertEquals('/tmp/php3zU3t5', $input->getTempName());
123
        $this->assertEquals(0, $input->getError());
124
        $this->assertEquals(591387, $input->getSize());
125
        $this->assertNotEmpty($input->getFile());
126
    }
127
}
128