1515
1616use CodeIgniter \Files \Exceptions \FileNotFoundException ;
1717use CodeIgniter \Test \CIUnitTestCase ;
18+ use PHPUnit \Framework \Attributes \DataProvider ;
1819use PHPUnit \Framework \Attributes \Group ;
1920use ZipArchive ;
2021
@@ -113,6 +114,38 @@ public function testGetSizeReturnsBytes(): void
113114 $ this ->assertSame ($ size , $ file ->getSizeByUnit ('b ' ));
114115 }
115116
117+ #[DataProvider('provideGetSizeData ' )]
118+ public function testGetSizeBinary (FileSizeUnit $ unit ): void
119+ {
120+ $ divider = 1024 ** $ unit ->value ;
121+ $ file = new File (SYSTEMPATH . 'Common.php ' );
122+ $ size = number_format (filesize (SYSTEMPATH . 'Common.php ' ) / $ divider , 3 );
123+ $ this ->assertSame ($ size , $ file ->getSizeByBinaryUnit ($ unit ));
124+ }
125+
126+ public function testGetSizeBinaryBytes (): void
127+ {
128+ $ file = new File (SYSTEMPATH . 'Common.php ' );
129+ $ size = filesize (SYSTEMPATH . 'Common.php ' );
130+ $ this ->assertSame ($ size , $ file ->getSizeByBinaryUnit (FileSizeUnit::B));
131+ }
132+
133+ #[DataProvider('provideGetSizeData ' )]
134+ public function testGetSizeMetric (FileSizeUnit $ unit ): void
135+ {
136+ $ divider = 1000 ** $ unit ->value ;
137+ $ file = new File (SYSTEMPATH . 'Common.php ' );
138+ $ size = number_format (filesize (SYSTEMPATH . 'Common.php ' ) / $ divider , 3 );
139+ $ this ->assertSame ($ size , $ file ->getSizeByMetricUnit ($ unit ));
140+ }
141+
142+ public function testGetSizeMetricBytes (): void
143+ {
144+ $ file = new File (SYSTEMPATH . 'Common.php ' );
145+ $ size = filesize (SYSTEMPATH . 'Common.php ' );
146+ $ this ->assertSame ($ size , $ file ->getSizeByMetricUnit (FileSizeUnit::B));
147+ }
148+
116149 public function testThrowsExceptionIfNotAFile (): void
117150 {
118151 $ this ->expectException (FileNotFoundException::class);
@@ -135,4 +168,25 @@ public function testGetDestination(): void
135168 unlink (SYSTEMPATH . 'Common_Copy.php ' );
136169 unlink (SYSTEMPATH . 'Common_Copy_5.php ' );
137170 }
171+
172+ /**
173+ * @return array<string, array<int, FileSizeUnit>>
174+ */
175+ public static function provideGetSizeData ()
176+ {
177+ return [
178+ 'returns KB binary ' => [
179+ FileSizeUnit::KB ,
180+ ],
181+ 'returns MB binary ' => [
182+ FileSizeUnit::MB ,
183+ ],
184+ 'returns GB binary ' => [
185+ FileSizeUnit::GB ,
186+ ],
187+ 'returns TB binary ' => [
188+ FileSizeUnit::TB ,
189+ ],
190+ ];
191+ }
138192}
0 commit comments