Completed
Push — master ( 71bce5...79e63b )
by Daniel
02:59
created

testInvalidFileExtensionValidatingMimeType()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 8.8571
cc 2
eloc 19
nc 2
nop 0
1
<?php
2
class MimeUploadValidatorTest 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...
Coding Style introduced by
As per PSR2, the opening brace for this class should be on a new line.
Loading history...
3
4
	public function testInvalidFileExtensionValidatingMimeType() {
5
		// setup plaintext file with invalid extension
6
		$tmpFileName = 'UploadTest-testUpload.jpg';
7
		$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
8
		$tmpFileContent = '';
9
		for($i=0; $i<10000; $i++) $tmpFileContent .= '0';
10
		file_put_contents($tmpFilePath, $tmpFileContent);
11
12
		// emulates the $_FILES array
13
		$tmpFile = array(
14
			'name' => $tmpFileName,
15
			'size' => filesize($tmpFilePath),
16
			'tmp_name' => $tmpFilePath,
17
			'extension' => 'jpg',
18
			'error' => UPLOAD_ERR_OK,
19
		);
20
21
		$u = new Upload();
22
		$u->setValidator(new MimeUploadValidator());
23
		$result = $u->load($tmpFile);
24
		$errors = $u->getErrors();
25
		$this->assertFalse($result, 'Load failed because file extension does not match excepted MIME type');
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<MimeUploadValidatorTest>.

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...
26
		$this->assertEquals('File extension does not match known MIME type', $errors[0]);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MimeUploadValidatorTest>.

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...
27
28
		unlink($tmpFilePath);
29
	}
30
31
32
	public function testGetExpectedMimeTypes() {
33
		// Setup a file with a capitalised extension and try to match it against a lowercase file.
34
		$tmpFileName = 'text.TXT';
35
		$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
36
		$tmpFileContent = '';
37
		for($i=0; $i<10000; $i++) $tmpFileContent .= '0';
38
		file_put_contents($tmpFilePath, $tmpFileContent);
39
40
		$validator = new MimeUploadValidator();
41
		$tmpFile = array(
42
			'name' => $tmpFileName,
43
			'tmp_name' => $tmpFilePath,
44
		);
45
		$expected = $validator->getExpectedMimeTypes($tmpFile);
46
		$this->assertCount(1, $expected);
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<MimeUploadValidatorTest>.

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...
47
		$this->assertContains('text/plain', $expected);
48
49
		unlink($tmpFilePath);
50
51
		// Test a physical ico file with capitalised extension
52
		$tmpFile = array(
53
			'name' => 'favicon.ICO',
54
			'tmp_name' => 'assets/favicon.ICO',
55
		);
56
		$expected = $validator->getExpectedMimeTypes($tmpFile);
57
		$this->assertCount(3, $expected);
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<MimeUploadValidatorTest>.

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...
58
	}
59
60
	public function testMimeComparison() {
61
		$validator = new MimeUploadValidator();
62
63
		$this->assertTrue($validator->compareMime('application/xhtml+xml', 'application/xml'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<MimeUploadValidatorTest>.

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...
64
		$this->assertTrue($validator->compareMime('application/vnd.text', 'application/text'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<MimeUploadValidatorTest>.

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...
65
		$this->assertTrue($validator->compareMime('application/vnd.vnd.text', 'application/text'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<MimeUploadValidatorTest>.

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...
66
		$this->assertTrue($validator->compareMime('application/x-text', 'application/text'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<MimeUploadValidatorTest>.

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...
67
		$this->assertTrue($validator->compareMime('application/gzip', 'application/gzip'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<MimeUploadValidatorTest>.

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...
68
		$this->assertTrue($validator->compareMime('application/x-gzip', 'application/gzip'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<MimeUploadValidatorTest>.

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...
69
		$this->assertFalse($validator->compareMime('application/png', 'application/json'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<MimeUploadValidatorTest>.

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...
70
		$this->assertFalse($validator->compareMime('text/plain', 'text/json'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<MimeUploadValidatorTest>.

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...
71
	}
72
73
}
0 ignored issues
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...
74