Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Tests #37

Merged
merged 3 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ jobs:

- name: Analyze for refactoring
run: |
composer global require --dev rector/rector:^0.12.10
composer global require --dev rector/rector:dev-main
rector process --dry-run --no-progress-bar
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vendor/
/vendor/
build/
phpunit*.xml
phpunit
Expand Down
3 changes: 2 additions & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

* Switches to `Tatter\Preferences` for managing persistent settings; read more below
* Drops `Tatter\Audits` as a dependency and adds it as a suggestion; read more below
* Access rights are now handled via Config file; see [Tatter\Permits](https://github.com/tattersoftware/codeigniter4-permits) for more information

### `Settings` Migration

Expand All @@ -29,7 +30,7 @@ php spark migrate --all
```

Example model file in **app/Models/FileModel.php**:
```
```php
<?php

namespace App\Models;
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
"tatter/alerts": "^2.0",
"tatter/exports": "^2.0",
"tatter/frontend": "^1.0",
"tatter/permits": "^2.0",
"tatter/permits": "^3.0",
"tatter/preferences": "^1.0",
"tatter/thumbnails": "^1.2"
},
"require-dev": {
"antecedent/patchwork": "^2.1",
"codeigniter4/devkit": "^1.0",
"codeigniter4/framework": "^4.1",
"tatter/imposter": "^1.0"
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
executionOrder="random"
failOnRisky="true"
failOnWarning="true"
stopOnError="false"
Expand Down
5 changes: 5 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
__DIR__ . '/tests',
],

// Ignore files that should not be namespaced
NormalizeNamespaceByPSR4ComposerAutoloadRector::class => [
__DIR__ . '/src/Helpers',
],

// May load view files directly when detecting classes
StringClassNameToClassConstantRector::class,

Expand Down
17 changes: 17 additions & 0 deletions src/Config/Registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,21 @@ public static function Pager()
],
];
}

/**
* Adds necessary configuration values for Permits
* to identify the owner(s) of files.
*
* @return array<string,mixed>
*/
public static function Permits()
{
return [
'files' => [
'userKey' => 'user_id',
'pivotKey' => 'file_id',
'pivotTable' => 'files_users',
],
];
}
}
3 changes: 1 addition & 2 deletions src/Controllers/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Files extends Controller
protected FilesConfig $config;

/**
* The model to use, may be a child of this library's.
* The model to use.
*/
protected FileModel $model;

Expand Down Expand Up @@ -156,7 +156,6 @@ public function user($userId = null)
}

$this->setData([
'access' => $this->model->mayAdmin() ? 'manage' : 'display',
'title' => 'User Files',
'userName' => 'User',
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class File extends Entity
/**
* Resolved path to the default thumbnail
*/
protected static ?string $defaultThumbnail;
protected static ?string $defaultThumbnail = null;

/**
* Returns the absolute path to the configured default thumbnail
Expand Down
6 changes: 0 additions & 6 deletions src/Models/FileModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ class FileModel extends Model
'size' => 'permit_empty|is_natural',
];

// Permits
protected $mode = 04660;
protected $userKey = 'user_id';
protected $pivotKey = 'file_id';
protected $usersPivot = 'files_users';

//--------------------------------------------------------------------

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Structures/FileObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FileObject extends File
/**
* Base file name to override disk version
*/
protected ?string $basename;
protected ?string $basename = null;

/**
* Returns the full path to this file
Expand All @@ -35,7 +35,7 @@ public function setBasename(?string $basename = null): self
*
* @param string $suffix Optional suffix to omit from the base name returned
*/
public function getBasename($suffix = null): string
public function getBasename($suffix = ''): string
{
if ($this->basename) {
return basename($this->basename, $suffix);
Expand Down
5 changes: 0 additions & 5 deletions tests/_support/FeatureTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Tests\Support;

use CodeIgniter\Config\Factories;
use CodeIgniter\Test\FeatureTestTrait;
use Tests\Support\Models\UserModel;

/**
* @internal
Expand All @@ -22,9 +20,6 @@ protected function setUp(): void
{
parent::setUp();

// Make sure we use the correct UserModel for permissions
Factories::injectMock('models', UserModel::class, new UserModel());

// Make sure everything is published once
$this->publishAll();
}
Expand Down
26 changes: 0 additions & 26 deletions tests/_support/Models/UserModel.php

This file was deleted.

57 changes: 48 additions & 9 deletions tests/_support/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use org\bovigo\vfs\vfsStream;
use Tatter\Assets\Test\AssetsTestTrait;
use Tatter\Files\Config\Files;
use Tatter\Files\Models\FileModel;
use Tatter\Imposter\Entities\User;
use Tatter\Imposter\Factories\ImposterFactory;
use Tatter\Users\UserProvider;

/**
* @internal
Expand All @@ -17,19 +21,26 @@ abstract class TestCase extends CIUnitTestCase
use AssetsTestTrait;
use DatabaseTestTrait;

protected $refresh = false;
protected $refreshVfs = false;
protected $refresh = false;
protected $namespace;

/**
* Path to a test file to work with
*/
protected string $testPath;

/**
* @var FileModel
*/
protected $model;

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

helper(['auth', 'files', 'preferences']);
UserProvider::addFactory(ImposterFactory::class, ImposterFactory::class);
}

protected function setUp(): void
Expand All @@ -38,22 +49,50 @@ protected function setUp(): void

parent::setUp();

$_REQUEST = [];
$_POST = [];
$_GET = [];
$_FILES = [];
$_REQUEST = [];

// Copy the files to VFS
vfsStream::copyFromFileSystem(SUPPORTPATH . 'VFS', $this->root);

// "vendor" gets ignored by .gitignore so rename it after copying to VFS
$path = $this->root->url() . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR;
rename($this->root->url() . '/storage', $path);
$this->testPath = $path . 'image.jpg';
// Make sure all files are on a single page
$_REQUEST['perPage'] = 200;

// Force Files config to the virtual path
$path = self::$root->url() . DIRECTORY_SEPARATOR;
$config = config('Files');
$config->setPath($path);
Factories::injectMock('config', 'Files', $config);

// Copy the files to VFS (if necessary)
$this->testPath = $config->getPath() . 'image.jpg';
if (! is_file($this->testPath)) {
vfsStream::copyFromFileSystem(SUPPORTPATH . 'VFS', self::$root);
}

// Set up the model
$this->model = model(FileModel::class); // @phpstan-ignore-line
}

protected function tearDown(): void
{
parent::tearDown();

ImposterFactory::reset();
}

/**
* Creates a test User with some files.
*
* @return array Tuple of [User, File]
*/
protected function createUserWithFile(): array
{
$user = ImposterFactory::fake();
$user->id = ImposterFactory::add($user);

$file = fake(FileModel::class);
$this->model->addToUser($file->id, $user->id);

return [$user, $file];
}
}
File renamed without changes
8 changes: 4 additions & 4 deletions tests/feature/DisplayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/
final class DisplayTest extends FeatureTestCase
{
protected $refresh = true;
protected $refresh = true;
protected $refreshVfs = true;

public function testNoFiles()
{
Expand Down Expand Up @@ -144,8 +145,6 @@ public function testSearches(string $keyword)

public function testPages()
{
$_REQUEST['perPage'] = 2;

for ($i = 0; $i < 2; $i++) {
$file = fake(FileModel::class);
}
Expand All @@ -156,7 +155,8 @@ public function testPages()
]);

// Last file should be on the next page
$result = $this->get('files');
$_REQUEST['perPage'] = 2;
$result = $this->get('files');

$result->assertStatus(200);
$result->assertDontSee($file->filename);
Expand Down
Loading