Skip to content

Commit 6863e55

Browse files
committed
Refactor and optimize tests
1 parent 7c04401 commit 6863e55

File tree

15 files changed

+136
-282
lines changed

15 files changed

+136
-282
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
vendor/
1+
/vendor/
22
build/
33
phpunit*.xml
44
phpunit

UPGRADING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

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

910
### `Settings` Migration
1011

@@ -29,7 +30,7 @@ php spark migrate --all
2930
```
3031

3132
Example model file in **app/Models/FileModel.php**:
32-
```
33+
```php
3334
<?php
3435

3536
namespace App\Models;

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@
2626
"tatter/alerts": "^2.0",
2727
"tatter/exports": "^2.0",
2828
"tatter/frontend": "^1.0",
29-
"tatter/permits": "^2.0",
29+
"tatter/permits": "^3.0",
3030
"tatter/preferences": "^1.0",
3131
"tatter/thumbnails": "^1.2"
3232
},
3333
"require-dev": {
34-
"antecedent/patchwork": "^2.1",
3534
"codeigniter4/codeigniter4": "dev-develop",
3635
"tatter/imposter": "^1.0",
3736
"tatter/tools": "^1.15"

src/Config/Registrar.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,21 @@ public static function Pager()
1717
],
1818
];
1919
}
20+
21+
/**
22+
* Adds necessary configuration values for Permits
23+
* to identify the owner(s) of files.
24+
*
25+
* @return array<string,mixed>
26+
*/
27+
public static function Permits()
28+
{
29+
return [
30+
'files' => [
31+
'userKey' => 'user_id',
32+
'pivotKey' => 'file_id',
33+
'pivotTable' => 'files_users',
34+
],
35+
];
36+
}
2037
}

src/Controllers/Files.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Files extends Controller
2525
protected $config;
2626

2727
/**
28-
* The model to use, may be a child of this library's.
28+
* The model to use.
2929
*
3030
* @var FileModel
3131
*/
@@ -162,7 +162,6 @@ public function user($userId = null)
162162
}
163163

164164
$this->setData([
165-
'access' => $this->model->mayAdmin() ? 'manage' : 'display',
166165
'title' => 'User Files',
167166
'userName' => 'User',
168167
]);

src/Models/FileModel.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ class FileModel extends Model
3333
'size' => 'permit_empty|is_natural',
3434
];
3535

36-
// Permits
37-
protected $mode = 04660;
38-
protected $userKey = 'user_id';
39-
protected $pivotKey = 'file_id';
40-
protected $usersPivot = 'files_users';
41-
4236
//--------------------------------------------------------------------
4337

4438
/**

tests/_support/FeatureTestCase.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Tests\Support;
44

5-
use CodeIgniter\Config\Factories;
65
use CodeIgniter\Test\FeatureTestTrait;
7-
use Tests\Support\Models\UserModel;
86

97
/**
108
* @internal
@@ -13,36 +11,19 @@ abstract class FeatureTestCase extends TestCase
1311
{
1412
use FeatureTestTrait;
1513

16-
/**
17-
* If present, will override application
18-
* routes when using call().
19-
*
20-
* @var \CodeIgniter\Router\RouteCollection
21-
*/
22-
protected $routes;
23-
24-
/**
25-
* Values to be set in the SESSION global
26-
* before running the test.
27-
*
28-
* @var array
29-
*/
30-
protected $session = [];
31-
3214
/**
3315
* Enabled auto clean op buffer after request call
3416
*
3517
* @var bool
3618
*/
3719
protected $clean = true;
3820

21+
protected $refreshVfs = false;
22+
3923
protected function setUp(): void
4024
{
4125
parent::setUp();
4226

43-
// Make sure we use the correct UserModel for permissions
44-
Factories::injectMock('models', UserModel::class, new UserModel());
45-
4627
// Make sure everything is published once
4728
$this->publishAll();
4829
}

tests/_support/Models/UserModel.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/_support/TestCase.php

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
use org\bovigo\vfs\vfsStream;
99
use Tatter\Assets\Test\AssetsTestTrait;
1010
use Tatter\Files\Config\Files;
11+
use Tatter\Files\Models\FileModel;
12+
use Tatter\Imposter\Entities\User;
13+
use Tatter\Imposter\Factories\ImposterFactory;
14+
use Tatter\Users\UserProvider;
1115

1216
/**
1317
* @internal
@@ -17,21 +21,36 @@ abstract class TestCase extends CIUnitTestCase
1721
use AssetsTestTrait;
1822
use DatabaseTestTrait;
1923

20-
protected $refresh = false;
24+
/**
25+
* Whether the files have been copied to
26+
* the virtual workspave yet.
27+
*
28+
* @var bool
29+
*/
30+
private static $copiedVfs = false;
31+
32+
protected $refreshVfs = true;
33+
protected $refresh = false;
2134
protected $namespace;
2235

2336
/**
2437
* Path to a test file to work with
2538
*
26-
* @var string
39+
* @var string|null
2740
*/
2841
protected $testPath;
2942

43+
/**
44+
* @var FileModel
45+
*/
46+
protected $model;
47+
3048
public static function setUpBeforeClass(): void
3149
{
3250
parent::setUpBeforeClass();
3351

3452
helper(['auth', 'files', 'preferences']);
53+
UserProvider::addFactory(ImposterFactory::class, ImposterFactory::class);
3554
}
3655

3756
protected function setUp(): void
@@ -40,22 +59,55 @@ protected function setUp(): void
4059

4160
parent::setUp();
4261

43-
$_REQUEST = [];
4462
$_POST = [];
4563
$_GET = [];
4664
$_FILES = [];
65+
$_REQUEST = [];
4766

48-
// Copy the files to VFS
49-
vfsStream::copyFromFileSystem(SUPPORTPATH . 'VFS', $this->root);
67+
// Make sure all files are on a single page
68+
$_REQUEST['perPage'] = 200;
5069

51-
// "vendor" gets ignored by .gitignore so rename it after copying to VFS
52-
$path = $this->root->url() . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR;
53-
rename($this->root->url() . '/storage', $path);
54-
$this->testPath = $path . 'image.jpg';
70+
// Copy the files to VFS (if necessary)
71+
if (! self::$copiedVfs) {
72+
vfsStream::copyFromFileSystem(SUPPORTPATH . 'VFS', self::$root);
73+
}
5574

5675
// Force Files config to the virtual path
76+
$path = self::$root->url() . DIRECTORY_SEPARATOR;
5777
$config = config('Files');
5878
$config->setPath($path);
5979
Factories::injectMock('config', 'Files', $config);
80+
81+
$this->testPath = $config->getPath() . 'image.jpg';
82+
83+
// Set up the model
84+
$this->model = model(FileModel::class); // @phpstan-ignore-line
85+
}
86+
87+
protected function tearDown(): void
88+
{
89+
parent::tearDown();
90+
91+
if (! empty($this->refreshVfs)) {
92+
self::$copiedVfs = false;
93+
}
94+
95+
ImposterFactory::reset();
96+
}
97+
98+
/**
99+
* Creates a test User with some files.
100+
*
101+
* @return array Tuple of [User, File]
102+
*/
103+
protected function createUserWithFile(): array
104+
{
105+
$user = ImposterFactory::fake();
106+
$user->id = ImposterFactory::add($user);
107+
108+
$file = fake(FileModel::class);
109+
$this->model->addToUser($file->id, $user->id);
110+
111+
return [$user, $file];
60112
}
61113
}
File renamed without changes.

0 commit comments

Comments
 (0)