Completed
Push — master ( 03e47f...db33ac )
by WEBEWEB
06:42 queued 01:35
created

FileUtilityTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testDelete() 0 15 2
B testFormatSize() 0 28 2
A testGetContents() 0 13 2
A testGetFilenames() 0 14 2
A testGetSize() 0 11 2
A testGetUnits() 0 5 1
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2017 NdC/WBW
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 WBW\Library\Core\Tests\Utility;
13
14
use Exception;
15
use PHPUnit_Framework_TestCase;
16
use WBW\Library\Core\Exception\Argument\IllegalArgumentException;
17
use WBW\Library\Core\Exception\Directory\DirectoryNotFoundException;
18
use WBW\Library\Core\Exception\File\FileNotFoundException;
19
use WBW\Library\Core\Utility\FileUtility;
20
21
/**
22
 * File utility test.
23
 *
24
 * @author NdC/WBW <https://github.com/webeweb/>
25
 * @package WBW\Library\Core\Tests\Utility
26
 * @final
27
 */
28
final class FileUtilityTest extends PHPUnit_Framework_TestCase {
29
30
	/**
31
	 * Tests the delete() method.
32
	 *
33
	 * @return void
34
	 */
35
	public function testDelete() {
36
37
		$filename = getcwd() . "/Tests/Utility/phpunit.txt";
38
39
		fclose(fopen($filename, "w"));
40
41
		$this->assertEquals(true, FileUtility::delete($filename));
42
43
		try {
44
			FileUtility::delete($filename);
45
		} catch (Exception $ex) {
46
			$this->assertInstanceOf(FileNotFoundException::class, $ex);
47
			$this->assertEquals("The file \"" . $filename . "\" is not found", $ex->getMessage());
48
		}
49
	}
50
51
	/**
52
	 * Tests the formatSize() method.
53
	 *
54
	 * @return void
55
	 */
56
	public function testFormatSize() {
57
58
		$this->assertEquals("99.00 B", FileUtility::formatSize(99));
59
		$this->assertEquals("1.00 KB", FileUtility::formatSize(1000));
60
		$this->assertEquals("1.00 MB", FileUtility::formatSize(1000000));
61
		$this->assertEquals("1.00 GB", FileUtility::formatSize(1000000000));
62
		$this->assertEquals("1.00 TB", FileUtility::formatSize(1000000000000));
63
		$this->assertEquals("1.00 PB", FileUtility::formatSize(1000000000000000));
64
		$this->assertEquals("1.00 EB", FileUtility::formatSize(1000000000000000000));
65
		$this->assertEquals("1.00 ZB", FileUtility::formatSize(1000000000000000000000));
66
		$this->assertEquals("1.00 YB", FileUtility::formatSize(1000000000000000000000000));
67
68
		$this->assertEquals("0.099 KB", FileUtility::formatSize(99, "KB", 3));
69
		$this->assertEquals("0.001 MB", FileUtility::formatSize(1000, "MB", 3));
70
		$this->assertEquals("0.001 GB", FileUtility::formatSize(1000000, "GB", 3));
71
		$this->assertEquals("0.001 TB", FileUtility::formatSize(1000000000, "TB", 3));
72
		$this->assertEquals("0.001 PB", FileUtility::formatSize(1000000000000, "PB", 3));
73
		$this->assertEquals("0.001 EB", FileUtility::formatSize(1000000000000000, "EB", 3));
74
		$this->assertEquals("0.001 ZB", FileUtility::formatSize(1000000000000000000, "ZB", 3));
75
		$this->assertEquals("0.001 YB", FileUtility::formatSize(1000000000000000000000, "YB", 3));
76
77
		try {
78
			FileUtility::formatSize(99, "exception");
79
		} catch (Exception $ex) {
80
			$this->assertInstanceOf(IllegalArgumentException::class, $ex);
81
			$this->assertEquals("The unit \"exception\" does not exists", $ex->getMessage());
82
		}
83
	}
84
85
	/**
86
	 * Tests the getContents() method.
87
	 *
88
	 * @return void
89
	 */
90
	public function testGetContents() {
91
92
		$filename = getcwd() . "/Tests/Utility/FileUtilityTest.txt";
93
94
		$this->assertEquals("FileUtilityTest", FileUtility::getContents($filename), 'The method getContents() does not return the expected content');
95
96
		try {
97
			FileUtility::getContents("exception");
98
		} catch (Exception $ex) {
99
			$this->assertInstanceOf(FileNotFoundException::class, $ex);
100
			$this->assertEquals("The file \"exception\" is not found", $ex->getMessage());
101
		}
102
	}
103
104
	/**
105
	 * Tests the getFilenames() method.
106
	 *
107
	 * @return void
108
	 */
109
	public function testGetFilenames() {
110
111
		$pathname = getcwd() . "/Tests/Utility";
112
113
		$this->assertContains("FileUtilityTest.php", FileUtility::getFilenames($pathname));
114
		$this->assertEquals(["FileUtilityTest.txt"], FileUtility::getFilenames($pathname, ".txt"));
115
116
		try {
117
			FileUtility::getFilenames("exception");
118
		} catch (Exception $ex) {
119
			$this->assertInstanceOf(DirectoryNotFoundException::class, $ex);
120
			$this->assertEquals("The directory \"exception\" is not found", $ex->getMessage());
121
		}
122
	}
123
124
	/**
125
	 * Tests the getSize() method.
126
	 *
127
	 * @return void
128
	 */
129
	public function testGetSize() {
130
131
		$this->assertEquals(15, FileUtility::getSize(getcwd() . "/Tests/Utility/FileUtilityTest.txt"));
132
133
		try {
134
			FileUtility::getSize("exception");
135
		} catch (Exception $ex) {
136
			$this->assertInstanceOf(FileNotFoundException::class, $ex);
137
			$this->assertEquals("The file \"exception\" is not found", $ex->getMessage());
138
		}
139
	}
140
141
	/**
142
	 * Tests the getUnits() method.
143
	 *
144
	 * @return void
145
	 */
146
	public function testGetUnits() {
147
148
		$res = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
149
		$this->assertEquals($res, FileUtility::getUnits());
150
	}
151
152
}
153