Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sopamo committed May 21, 2022
1 parent 87ec876 commit fa34b5a
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
composer.phar
composer.lock
.DS_Store
.idea
.idea
.phpunit.result.cache
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@ FilePond.setOptions({
}
});
```

## Package development
Please make sure all tests run successfully before submitting a PR.
### Testing
- Start a docker container to execute the tests in with ` docker run -it -v $PWD:/app composer /bin/bash`
- Run `composer install`
- Run `./vendor/bin/phpunit`
14 changes: 12 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
},
"autoload": {
"psr-4": {
"Sopamo\\LaravelFilepond\\": "src/"
"Sopamo\\LaravelFilepond\\": "src/",
"Sopamo\\LaravelFilepond\\Tests\\": "tests/"
}
},
"extra": {
Expand All @@ -34,6 +35,15 @@
]
}
},
"scripts": {
"post-autoload-dump": [
"@php ./vendor/bin/testbench package:discover --ansi"
]
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"require-dev": {
"phpunit/phpunit": "^9.5",
"orchestra/testbench": "^7.5"
}
}
33 changes: 33 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<php>
<env name="DB_CONNECTION" value="testing"/>
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>
</php>
</phpunit>
7 changes: 4 additions & 3 deletions src/Http/Controllers/FilepondController.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ public function chunk(Request $request)
*/
private function persistFileIfDone($disk, $basePath, $length, $finalFilePath)
{
$storage = Storage::disk($disk);
// Check total chunks size
$size = 0;
$chunks = Storage::disk($disk)
$chunks = $storage
->files($basePath);

foreach ($chunks as $chunk) {
$size += Storage::disk($disk)
$size += $storage
->size($chunk);
}

Expand All @@ -166,7 +167,7 @@ private function persistFileIfDone($disk, $basePath, $length, $finalFilePath)
$data = '';
foreach ($chunks as $chunk) {
// Get chunk contents
$chunkContents = Storage::disk($disk)
$chunkContents = $storage
->get($chunk);

// Laravel's local disk implementation is quite inefficient for appending data to existing files
Expand Down
36 changes: 36 additions & 0 deletions tests/Feature/SingleFileUploadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Sopamo\LaravelFilepond\Tests\Feature;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Sopamo\LaravelFilepond\Filepond;
use Sopamo\LaravelFilepond\Tests\TestCase;

class SingleFileUploadTest extends TestCase
{
/** @test */
public function test_normal_file_upload()
{
$tmpPath = config('filepond.temporary_files_path', 'filepond');
$diskName = config('filepond.temporary_files_disk', 'local');

Storage::fake($diskName);

$response = $this->postJson('/filepond/api/process', [
'file' => UploadedFile::fake()->create('test.txt', 1),
]);

$response->assertStatus(200);
$serverId = $response->content();
$this->assertGreaterThan(50, strlen($serverId));

/** @var Filepond $filepond */
$filepond = app(Filepond::class);
$pathFromServerId = $filepond->getPathFromServerId($serverId);

$this->assertStringStartsWith($tmpPath, $pathFromServerId, 'tmp file was not created in the temporary_files_path directory');

Storage::disk($diskName)->assertExists($pathFromServerId);
}
}
27 changes: 27 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Sopamo\LaravelFilepond\Tests;


use Sopamo\LaravelFilepond\LaravelFilepondServiceProvider;

class TestCase extends \Orchestra\Testbench\TestCase
{
public function setUp(): void
{
parent::setUp();
// additional setup
}

protected function getPackageProviders($app)
{
return [
LaravelFilepondServiceProvider::class,
];
}

protected function getEnvironmentSetUp($app)
{
// perform environment setup
}
}

0 comments on commit fa34b5a

Please sign in to comment.