Skip to content

Commit 9f8ba43

Browse files
authored
Merge pull request #8 from lion-packages/new
getNamespaceFromFile and getFiles added
2 parents 62e696f + c1398b5 commit 9f8ba43

File tree

7 files changed

+358
-209
lines changed

7 files changed

+358
-209
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/storage/
44
index.php
55
.phpunit.result.cache
6+
/tests/build/

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
}
1515
},
1616
"require": {
17-
"php": ">=8.3"
17+
"php": ">=8.3",
18+
"ext-mbstring": "*"
1819
},
1920
"require-dev": {
20-
"lion/test": "^2.0",
21-
"phpunit/phpunit": "^11.2"
21+
"phpunit/phpunit": "^11.4",
22+
"lion/test": "^2.1"
2223
}
2324
}

composer.lock

Lines changed: 185 additions & 184 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/LionFiles/Store.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,54 @@ public function normalizePath(string $path): string
3636
return implode(DIRECTORY_SEPARATOR, explode('/', $path));
3737
}
3838

39+
/**
40+
* Gets the namespace of a class through a defined path
41+
*
42+
* @param string $file [File path]
43+
* @param string $namespace [Namespace for the file]
44+
* @param string $split [Separator to obtain the namespace]
45+
*
46+
* @return string
47+
*/
48+
public function getNamespaceFromFile(string $file, string $namespace, string $split = '/'): string
49+
{
50+
$splitFile = explode($split, $file);
51+
52+
$namespace = str_replace("/", "\\", "{$namespace}{$splitFile[1]}");
53+
54+
$namespace = str_replace('.php', '', $namespace);
55+
56+
return trim($namespace);
57+
}
58+
59+
/**
60+
* Get files from a defined path
61+
*
62+
* @param string $folder [Defined route]
63+
*
64+
* @return array<string>
65+
*/
66+
public function getFiles(string $folder): array
67+
{
68+
$files = [];
69+
70+
$content = scandir($folder);
71+
72+
foreach ($content as $element) {
73+
if ($element != '.' && $element != '..') {
74+
$path = $folder . '/' . $element;
75+
76+
if (is_dir($path)) {
77+
$files = array_merge($files, $this->getFiles($path));
78+
} else {
79+
$files[] = realpath($path);
80+
}
81+
}
82+
}
83+
84+
return $files;
85+
}
86+
3987
/**
4088
* Gets the file from the defined path
4189
*

test-report.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
echo -e "\n\033[0;36m\t>> Set Time Zone \033[0m"
4+
export TZ=America/Bogota
5+
echo -e "\033[0;36m\t>> America/Bogota \033[0m"
6+
start_time=$(date +"%Y-%m-%d %H:%M:%S")
7+
echo -e "\n\033[0;31m>> -------------------------------------------------------------------------------------- << \n\033[0m";
8+
9+
echo -e "\033[0;36m\t>> Install Dependencies \033[0m"
10+
rm -rf vendor/
11+
composer install
12+
echo -e "\n\033[0;31m>> -------------------------------------------------------------------------------------- << \n\033[0m";
13+
14+
echo -e "\033[0;36m\t>> Dump Autoload \033[0m"
15+
composer dump-autoload
16+
echo -e "\n\033[0;31m>> -------------------------------------------------------------------------------------- << \n\033[0m";
17+
18+
echo -e "\033[0;36m\t>> Suite All-Test \033[0m"
19+
php vendor/bin/phpunit --testsuite All-Test --coverage-clover tests/build/logs/clover.xml --coverage-html tests/build/coverage
20+
echo -e "\n\033[0;31m>> -------------------------------------------------------------------------------------- << \n\033[0m";
21+
22+
end_time=$(date +"%Y-%m-%d %H:%M:%S")
23+
start_seconds=$(date -d "$start_time" +%s)
24+
end_seconds=$(date -d "$end_time" +%s)
25+
time_diff=$((end_seconds - start_seconds))
26+
minutes=$((time_diff / 60))
27+
seconds=$((time_diff % 60))
28+
29+
echo -e "\033[0;36m\t>> Start date and time: ${start_time} \033[0m"
30+
echo -e "\033[0;36m\t>> End date and time: ${end_time} \033[0m"
31+
echo -e "\033[0;32m\t>> Time execution: ${minutes} minutes ${seconds} seconds \n \033[0m"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Providers;
6+
7+
class CustomClassProvider
8+
{
9+
}

tests/StoreTest.php

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
namespace Tests;
66

7+
use Exception;
78
use Lion\Files\Store;
89
use Lion\Test\Test;
10+
use PHPUnit\Framework\Attributes\Test as Testing;
11+
use Tests\Providers\CustomClassProvider;
912

1013
class StoreTest extends Test
1114
{
1215
private const string URL_PATH = './storage/';
16+
private const string PROVIDERS_URL_PATH = './tests/Providers/';
1317
private const string IMAGE_SIZE = '100x100';
1418
private const string FILE_NAME = 'image.png';
1519
private const string INDICATIVE = 'FILE';
@@ -29,12 +33,40 @@ protected function tearDown(): void
2933
$this->rmdirRecursively(self::URL_PATH);
3034
}
3135

32-
public function testGet(): void
36+
#[Testing]
37+
public function getNamespaceFromFile(): void
38+
{
39+
$namespace = $this->store->getNamespaceFromFile(
40+
self::PROVIDERS_URL_PATH . 'CustomClassProvider.php',
41+
'Tests\\Providers\\',
42+
'Providers/'
43+
);
44+
45+
$this->assertIsString($namespace);
46+
$this->assertSame(CustomClassProvider::class, $namespace);
47+
}
48+
49+
#[Testing]
50+
public function getFiles(): void
51+
{
52+
$providerFiles = [
53+
'/var/www/html/tests/Providers/CustomClassProvider.php',
54+
];
55+
56+
$files = $this->store->getFiles(self::PROVIDERS_URL_PATH);
57+
58+
$this->assertIsArray($files);
59+
$this->assertSame($providerFiles, $files);
60+
}
61+
62+
#[Testing]
63+
public function get(): void
3364
{
3465
$this->assertSame(file_get_contents('./LICENSE'), $this->store->get('./LICENSE'));
3566
}
3667

37-
public function testImageSize(): void
68+
#[Testing]
69+
public function imageSize(): void
3870
{
3971
$this->createImage();
4072

@@ -46,7 +78,8 @@ public function testImageSize(): void
4678
$this->assertSame('success', $res->status);
4779
}
4880

49-
public function testImageSizeError(): void
81+
#[Testing]
82+
public function imageSizeError(): void
5083
{
5184
$this->createImage(100, 300);
5285

@@ -58,7 +91,8 @@ public function testImageSizeError(): void
5891
$this->assertSame('error', $res->status);
5992
}
6093

61-
public function testSize(): void
94+
#[Testing]
95+
public function tsize(): void
6296
{
6397
$this->createImage();
6498

@@ -72,7 +106,8 @@ public function testSize(): void
72106
$this->assertSame('success', $res->status);
73107
}
74108

75-
public function testSizeError(): void
109+
#[Testing]
110+
public function sizeError(): void
76111
{
77112
$this->createImage();
78113

@@ -84,7 +119,8 @@ public function testSizeError(): void
84119
$this->assertSame('error', $res->status);
85120
}
86121

87-
public function testView(): void
122+
#[Testing]
123+
public function view(): void
88124
{
89125
$this->createImage();
90126

@@ -94,7 +130,8 @@ public function testView(): void
94130
$this->assertCount(1, $res);
95131
}
96132

97-
public function testViewError(): void
133+
#[Testing]
134+
public function viewError(): void
98135
{
99136
$this->createImage();
100137

@@ -106,7 +143,11 @@ public function testViewError(): void
106143
$this->assertSame('error', $res->status);
107144
}
108145

109-
public function testRemove(): void
146+
/**
147+
* @throws Exception
148+
*/
149+
#[Testing]
150+
public function remove(): void
110151
{
111152
$this->createImage();
112153

@@ -118,7 +159,11 @@ public function testRemove(): void
118159
$this->assertSame('success', $res->status);
119160
}
120161

121-
public function testRemoveWithMissingFile(): void
162+
/**
163+
* @throws Exception
164+
*/
165+
#[Testing]
166+
public function removeWithMissingFile(): void
122167
{
123168
$res = $this->store->remove(self::URL_PATH . self::FILE_NAME);
124169

@@ -128,7 +173,8 @@ public function testRemoveWithMissingFile(): void
128173
$this->assertSame('error', $res->status);
129174
}
130175

131-
public function testExist(): void
176+
#[Testing]
177+
public function exist(): void
132178
{
133179
$res = $this->store->exist(self::URL_PATH);
134180

@@ -138,7 +184,8 @@ public function testExist(): void
138184
$this->assertSame('success', $res->status);
139185
}
140186

141-
public function testExistWithFile(): void
187+
#[Testing]
188+
public function existWithFile(): void
142189
{
143190
$this->createImage();
144191

@@ -150,7 +197,8 @@ public function testExistWithFile(): void
150197
$this->assertSame('success', $res->status);
151198
}
152199

153-
public function testExistError(): void
200+
#[Testing]
201+
public function existError(): void
154202
{
155203
$res = $this->store->exist(self::URL_PATH . self::FILE_NAME);
156204

@@ -160,34 +208,40 @@ public function testExistError(): void
160208
$this->assertSame('error', $res->status);
161209
}
162210

163-
public function testRenameWithoutIndicative(): void
211+
#[Testing]
212+
public function renameWithoutIndicative(): void
164213
{
165214
$this->assertMatchesRegularExpression('/^[a-f0-9]{32}\.png$/', $this->store->rename(self::FILE_NAME));
166215
}
167216

168-
public function testRenameWithIndicative(): void
217+
#[Testing]
218+
public function renameWithIndicative(): void
169219
{
170220
$result = $this->store->rename(self::FILE_NAME, self::INDICATIVE);
171221

172222
$this->assertMatchesRegularExpression('/^FILE-[a-f0-9]{32}\.png$/', $result);
173223
}
174224

175-
public function testGetExtension(): void
225+
#[Testing]
226+
public function getExtension(): void
176227
{
177228
$this->assertSame('png', $this->store->getExtension(self::FILE_NAME));
178229
}
179230

180-
public function testGetName(): void
231+
#[Testing]
232+
public function getName(): void
181233
{
182234
$this->assertSame('image', $this->store->getName(self::URL_PATH . self::FILE_NAME));
183235
}
184236

185-
public function testGetBaseName(): void
237+
#[Testing]
238+
public function getBaseName(): void
186239
{
187240
$this->assertSame('image', $this->store->getName(self::URL_PATH . self::FILE_NAME));
188241
}
189242

190-
public function testFolder(): void
243+
#[Testing]
244+
public function folder(): void
191245
{
192246
$res = $this->store->folder(self::URL_PATH);
193247

@@ -198,7 +252,8 @@ public function testFolder(): void
198252
$this->assertFileExists(self::URL_PATH);
199253
}
200254

201-
public function testFolderCustomSuccess(): void
255+
#[Testing]
256+
public function folderCustomSuccess(): void
202257
{
203258
$res = $this->store->folder(self::URL_PATH . 'new/');
204259

@@ -209,7 +264,8 @@ public function testFolderCustomSuccess(): void
209264
$this->assertFileExists(self::URL_PATH . 'new/');
210265
}
211266

212-
public function testValidate(): void
267+
#[Testing]
268+
public function validate(): void
213269
{
214270
$res = $this->store->validate([self::FILE_NAME], self::EXTENSIONS);
215271

@@ -219,7 +275,8 @@ public function testValidate(): void
219275
$this->assertSame('success', $res->status);
220276
}
221277

222-
public function testValidateError(): void
278+
#[Testing]
279+
public function validateError(): void
223280
{
224281
$res = $this->store->validate([self::FILE_NAME], ['php']);
225282

@@ -229,7 +286,8 @@ public function testValidateError(): void
229286
$this->assertSame('error', $res->status);
230287
}
231288

232-
public function testReplace(): void
289+
#[Testing]
290+
public function replace(): void
233291
{
234292
$res = mb_convert_encoding('áéíóúñ', 'ISO-8859-1', 'UTF-8');
235293

0 commit comments

Comments
 (0)