Skip to content

Commit 6410d9b

Browse files
authored
Merge pull request #20 from tattersoftware/tools
Bugfix Export
2 parents ab47935 + fc4c594 commit 6410d9b

File tree

9 files changed

+152
-37
lines changed

9 files changed

+152
-37
lines changed

.github/workflows/analyze.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
paths:
1010
- 'src/**'
1111
- 'tests/**'
12+
- 'composer.**'
1213
- 'phpstan*'
1314
- '.github/workflows/analyze.yml'
1415
push:
@@ -17,6 +18,7 @@ on:
1718
paths:
1819
- 'src/**'
1920
- 'tests/**'
21+
- 'composer.**'
2022
- 'phpstan*'
2123
- '.github/workflows/analyze.yml'
2224

@@ -27,7 +29,7 @@ jobs:
2729
strategy:
2830
fail-fast: false
2931
matrix:
30-
php-versions: ['7.3', '7.4']
32+
php-versions: ['7.3', '7.4', '8.0']
3133
steps:
3234
- name: Checkout
3335
uses: actions/checkout@v2
@@ -64,7 +66,7 @@ jobs:
6466
restore-keys: ${{ runner.os }}-phpstan-
6567

6668
- name: Install dependencies
67-
run: composer install --no-progress --no-interaction --prefer-dist --optimize-autoloader
69+
run: composer update --no-progress --no-interaction --prefer-dist --optimize-autoloader
6870
env:
6971
COMPOSER_AUTH: ${{ secrets.COMPOSER_AUTH }}
7072

.github/workflows/test.yml

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414

1515
strategy:
1616
matrix:
17-
php-versions: ['7.3', '7.4']
17+
php-versions: ['7.3', '7.4', '8.0']
1818

1919
runs-on: ubuntu-latest
2020

@@ -29,7 +29,7 @@ jobs:
2929
with:
3030
php-version: ${{ matrix.php-versions }}
3131
tools: composer, pecl, phpunit
32-
extensions: intl, json, mbstring, mysqlnd, xdebug, xml, sqlite3
32+
extensions: intl, json, mbstring, gd, mysqlnd, xdebug, xml, sqlite3
3333
coverage: xdebug
3434

3535
- name: Get composer cache directory
@@ -44,28 +44,36 @@ jobs:
4444
restore-keys: ${{ runner.os }}-composer-
4545

4646
- name: Install dependencies
47-
run: composer install --no-progress --no-interaction --prefer-dist --optimize-autoloader
47+
run: composer update --no-progress --no-interaction --prefer-dist --optimize-autoloader
4848
env:
4949
COMPOSER_AUTH: ${{ secrets.COMPOSER_AUTH }}
5050

5151
- name: Test with PHPUnit
5252
run: vendor/bin/phpunit --verbose --coverage-text
53+
env:
54+
TERM: xterm-256color
5355

54-
- if: matrix.php-versions == '7.4'
55-
name: Run Coveralls
56+
- if: matrix.php-versions == '8.0'
57+
name: Mutate with Infection
5658
run: |
57-
composer global require php-coveralls/php-coveralls:^2.4
58-
php-coveralls --coverage_clover=build/logs/clover.xml -v
59+
composer global require infection/infection
60+
git fetch --depth=1 origin $GITHUB_BASE_REF
61+
infection --threads=2 --skip-initial-tests --coverage=build/phpunit --git-diff-base=origin/$GITHUB_BASE_REF --git-diff-filter=AM --logger-github --ignore-msi-with-no-mutations
62+
63+
- if: matrix.php-versions == '8.0'
64+
name: Run Coveralls
65+
run: vendor/bin/php-coveralls --verbose --coverage_clover=build/phpunit/clover.xml --json_path build/phpunit/coveralls-upload.json
5966
env:
6067
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6168
COVERALLS_PARALLEL: true
62-
COVERALLS_FLAG_NAME: PHP ${{ matrix.php-versions }} - ${{ matrix.db-platforms }}
69+
COVERALLS_FLAG_NAME: PHP ${{ matrix.php-versions }}
6370

6471
coveralls:
6572
needs: [main]
73+
name: Coveralls Finished
6674
runs-on: ubuntu-latest
6775
steps:
68-
- name: Coveralls Finished
76+
- name: Upload Coveralls results
6977
uses: coverallsapp/github-action@master
7078
with:
7179
github-token: ${{ secrets.GITHUB_TOKEN }}

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
}
2121
],
2222
"require": {
23-
"php": "^7.3",
23+
"php": "^7.3 || ^8.0",
2424
"components/jquery": "^3.3",
2525
"enyo/dropzone": "^5.7",
2626
"fortawesome/font-awesome": "^5.8",
@@ -38,7 +38,7 @@
3838
"codeigniter4/codeigniter4": "dev-develop",
3939
"codeigniter4/codeigniter4-standard": "^1.0",
4040
"myth/auth": "dev-develop",
41-
"tatter/tools": "^1.3"
41+
"tatter/tools": "^1.6"
4242
},
4343
"autoload": {
4444
"psr-4": {
@@ -73,6 +73,7 @@
7373
"prefer-stable": true,
7474
"scripts": {
7575
"analyze": "phpstan analyze",
76+
"mutate": "infection --threads=2 --skip-initial-tests --coverage=build/phpunit",
7677
"style": "phpcbf --standard=./vendor/codeigniter4/codeigniter4-standard/CodeIgniter4 tests/ src/",
7778
"test": "phpunit"
7879
}

infection.json.dist

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"source": {
3+
"directories": [
4+
"src"
5+
],
6+
"excludes": [
7+
"Config",
8+
"Database/Migrations",
9+
"Views"
10+
]
11+
},
12+
"logs": {
13+
"text": "build/infection.log"
14+
},
15+
"mutators": {
16+
"@default": true
17+
},
18+
"bootstrap": "vendor/codeigniter4/codeigniter4/system/Test/bootstrap.php"
19+
}

phpunit.xml.dist

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
convertErrorsToExceptions="true"
77
convertNoticesToExceptions="true"
88
convertWarningsToExceptions="true"
9+
executionOrder="random"
910
stopOnError="false"
1011
stopOnFailure="false"
1112
stopOnIncomplete="false"
@@ -21,10 +22,11 @@
2122
<file>./src/Config/Routes.php</file>
2223
</exclude>
2324
<report>
24-
<clover outputFile="build/logs/clover.xml"/>
25-
<html outputDirectory="build/logs/html"/>
26-
<php outputFile="build/logs/coverage.serialized"/>
25+
<clover outputFile="build/phpunit/clover.xml"/>
26+
<html outputDirectory="build/phpunit/html"/>
27+
<php outputFile="build/phpunit/coverage.serialized"/>
2728
<text outputFile="php://stdout" showUncoveredFiles="false"/>
29+
<xml outputDirectory="build/phpunit/xml-coverage"/>
2830
</report>
2931
</coverage>
3032

@@ -35,9 +37,9 @@
3537
</testsuites>
3638

3739
<logging>
38-
<testdoxHtml outputFile="build/logs/testdox.html"/>
39-
<testdoxText outputFile="build/logs/testdox.txt"/>
40-
<junit outputFile="build/logs/logfile.xml"/>
40+
<testdoxHtml outputFile="build/phpunit/testdox.html"/>
41+
<testdoxText outputFile="build/phpunit/testdox.txt"/>
42+
<junit outputFile="build/phpunit/junit.xml"/>
4143
</logging>
4244

4345
<php>

src/Controllers/Files.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,14 @@ public function export(string $slug, $fileId): ResponseInterface
523523
return redirect()->back();
524524
}
525525

526+
// Verify the file exists
527+
if (! $fileObject = $file->getObject())
528+
{
529+
log_message('error', lang('Files.fileNotFound', [$file->getPath()]));
530+
alert('warning', lang('Files.fileNotFound', [$file->filename]));
531+
return redirect()->back();
532+
}
533+
526534
// Create the record
527535
model(ExportModel::class)->insert([
528536
'handler' => $slug,

src/Helpers/files_helper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,19 @@ function return_bytes(string $value): int
8080
{
8181
$value = strtolower(trim($value));
8282
$unit = $value[strlen($value) - 1];
83-
$num = (int)rtrim($value, $unit);
83+
$num = (int) rtrim($value, $unit);
8484

8585
switch ($unit)
8686
{
8787
case 'g': $num *= 1024;
8888
case 'm': $num *= 1024;
8989
case 'k': $num *= 1024;
90+
91+
// If it is not one of those modifiers then it was numerical bytes, add the final digit back
92+
default:
93+
$num = (int) ((string) $num . $unit);
9094
}
95+
9196
return $num;
9297
}
9398
}

src/Models/FileModel.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,34 @@ public function createFromFile(CIFile $file, array $data = []): File
178178
{
179179
$this->addToUser($fileId, $userId);
180180
}
181-
182-
// Try to create a Thumbnail
183-
$thumbnail = pathinfo($row['localname'], PATHINFO_FILENAME);
184-
$output = $storage . 'thumbnails' . DIRECTORY_SEPARATOR . $thumbnail;
185-
186-
try
187-
{
188-
service('thumbnails')->create((string) $file, $output);
189-
190-
// If it succeeds then update the database
191-
$this->update($fileId, [
192-
'thumbnail' => $thumbnail,
193-
]);
194-
}
195-
catch (\Throwable $e)
181+
182+
// Check for a thumbnail handler
183+
$extension = pathinfo((string) $file, PATHINFO_EXTENSION);
184+
if (service('thumbnails')->matchHandlers($extension) !== [])
196185
{
197-
log_message('error', $e->getMessage());
198-
log_message('error', 'Unable to create thumbnail for ' . $row['filename']);
186+
// Try to create a Thumbnail
187+
$thumbnail = pathinfo($row['localname'], PATHINFO_FILENAME);
188+
$output = $storage . 'thumbnails' . DIRECTORY_SEPARATOR . $thumbnail;
189+
190+
try
191+
{
192+
service('thumbnails')->create((string) $file, $output);
193+
194+
// If it succeeds then update the database
195+
$this->update($fileId, [
196+
'thumbnail' => $thumbnail,
197+
]);
198+
}
199+
catch (\Throwable $e)
200+
{
201+
log_message('error', $e->getMessage());
202+
log_message('error', 'Unable to create thumbnail for ' . $row['filename']);
203+
204+
if (ENVIRONMENT === 'testing')
205+
{
206+
throw $e;
207+
}
208+
}
199209
}
200210

201211
// Return the File entity

tests/unit/HelperTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
use Tatter\Files\Entities\File;
4+
use Tests\Support\Fakers\FileFaker;
5+
use Tests\Support\FilesTestCase;
6+
7+
class HelperTest extends FilesTestCase
8+
{
9+
10+
public static function setUpBeforeClass(): void
11+
{
12+
parent::setUpBeforeClass();
13+
14+
helper('files');
15+
}
16+
17+
/**
18+
* @dataProvider bytesProvider
19+
*/
20+
public function testBytesToHuman($bytes, $expected)
21+
{
22+
$this->assertEquals($expected, bytes2human($bytes));
23+
}
24+
25+
public function bytesProvider()
26+
{
27+
return [
28+
[1, '1 bytes'],
29+
[1024, '1024 bytes'],
30+
[1025, '1 KB'],
31+
[1024*1024, '1024 KB'],
32+
[1024*1025, '1 MB'],
33+
[1024*1024*1024, '1024 MB'],
34+
[1024*1024*1025, '1 GB'],
35+
[1024*1024*1024*1024, '1024 GB'],
36+
[1024*1024*1024*1025, '1 TB'],
37+
[1024*1024*1024*1024*1024, '1024 TB'],
38+
[1024*1024*1024*1024*1025, '1 PB'],
39+
];
40+
}
41+
42+
/**
43+
* @dataProvider iniProvider
44+
*/
45+
public function testReturnBytes($ini, $expected)
46+
{
47+
$this->assertEquals($expected, return_bytes($ini));
48+
}
49+
50+
public function iniProvider()
51+
{
52+
return [
53+
['1', 1],
54+
['1025', 1025],
55+
['1k', 1024],
56+
['1m', 1024*1024],
57+
['1g', 1024*1024*1024],
58+
];
59+
}
60+
}

0 commit comments

Comments
 (0)