scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | /* |
||
4 | * This file is part of the Symfony package. |
||
5 | * |
||
6 | * (c) Fabien Potencier <fabien@symfony.com> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Symfony\Component\DomCrawler\Tests\Field; |
||
13 | |||
14 | use Symfony\Component\DomCrawler\Field\FileFormField; |
||
15 | |||
16 | class FileFormFieldTest extends FormFieldTestCase |
||
17 | { |
||
18 | public function testInitialize() |
||
19 | { |
||
20 | $node = $this->createNode('input', '', array('type' => 'file')); |
||
21 | $field = new FileFormField($node); |
||
22 | |||
23 | $this->assertEquals(array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0), $field->getValue(), '->initialize() sets the value of the field to no file uploaded'); |
||
24 | |||
25 | $node = $this->createNode('textarea', ''); |
||
26 | try { |
||
27 | $field = new FileFormField($node); |
||
28 | $this->fail('->initialize() throws a \LogicException if the node is not an input field'); |
||
29 | } catch (\LogicException $e) { |
||
30 | $this->assertTrue(true, '->initialize() throws a \LogicException if the node is not an input field'); |
||
31 | } |
||
32 | |||
33 | $node = $this->createNode('input', '', array('type' => 'text')); |
||
34 | try { |
||
35 | $field = new FileFormField($node); |
||
36 | $this->fail('->initialize() throws a \LogicException if the node is not a file input field'); |
||
37 | } catch (\LogicException $e) { |
||
38 | $this->assertTrue(true, '->initialize() throws a \LogicException if the node is not a file input field'); |
||
39 | } |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @dataProvider getSetValueMethods |
||
44 | */ |
||
45 | public function testSetValue($method) |
||
46 | { |
||
47 | $node = $this->createNode('input', '', array('type' => 'file')); |
||
48 | $field = new FileFormField($node); |
||
49 | |||
50 | $field->$method(null); |
||
51 | $this->assertEquals(array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0), $field->getValue(), "->$method() clears the uploaded file if the value is null"); |
||
52 | |||
53 | $field->$method(__FILE__); |
||
54 | $value = $field->getValue(); |
||
55 | |||
56 | $this->assertEquals(basename(__FILE__), $value['name'], "->$method() sets the name of the file field"); |
||
57 | $this->assertEquals('', $value['type'], "->$method() sets the type of the file field"); |
||
58 | $this->assertInternalType('string', $value['tmp_name'], "->$method() sets the tmp_name of the file field"); |
||
59 | $this->assertFileExists($value['tmp_name'], "->$method() creates a copy of the file at the tmp_name path"); |
||
60 | $this->assertEquals(0, $value['error'], "->$method() sets the error of the file field"); |
||
61 | $this->assertEquals(filesize(__FILE__), $value['size'], "->$method() sets the size of the file field"); |
||
62 | |||
63 | $origInfo = pathinfo(__FILE__); |
||
64 | $tmpInfo = pathinfo($value['tmp_name']); |
||
65 | $this->assertEquals( |
||
66 | $origInfo['extension'], |
||
67 | $tmpInfo['extension'], |
||
68 | "->$method() keeps the same file extension in the tmp_name copy" |
||
69 | ); |
||
70 | |||
71 | $field->$method(__DIR__.'/../Fixtures/no-extension'); |
||
72 | $value = $field->getValue(); |
||
73 | |||
74 | $this->assertArrayNotHasKey( |
||
75 | 'extension', |
||
76 | pathinfo($value['tmp_name']), |
||
77 | "->$method() does not add a file extension in the tmp_name copy" |
||
78 | ); |
||
79 | } |
||
80 | |||
81 | public function getSetValueMethods() |
||
82 | { |
||
83 | return array( |
||
84 | array('setValue'), |
||
85 | array('upload'), |
||
86 | ); |
||
87 | } |
||
88 | |||
89 | public function testSetErrorCode() |
||
90 | { |
||
91 | $node = $this->createNode('input', '', array('type' => 'file')); |
||
92 | $field = new FileFormField($node); |
||
93 | |||
94 | $field->setErrorCode(UPLOAD_ERR_FORM_SIZE); |
||
95 | $value = $field->getValue(); |
||
96 | $this->assertEquals(UPLOAD_ERR_FORM_SIZE, $value['error'], '->setErrorCode() sets the file input field error code'); |
||
97 | |||
98 | try { |
||
99 | $field->setErrorCode('foobar'); |
||
100 | $this->fail('->setErrorCode() throws a \InvalidArgumentException if the error code is not valid'); |
||
101 | } catch (\InvalidArgumentException $e) { |
||
102 | $this->assertTrue(true, '->setErrorCode() throws a \InvalidArgumentException if the error code is not valid'); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | public function testSetRawFilePath() |
||
107 | { |
||
108 | $node = $this->createNode('input', '', array('type' => 'file')); |
||
109 | $field = new FileFormField($node); |
||
110 | $field->setFilePath(__FILE__); |
||
111 | |||
112 | $this->assertEquals(__FILE__, $field->getValue()); |
||
113 | } |
||
114 | } |