Skip to content

Commit 3da8383

Browse files
committed
Rework test properties
1 parent e52bec8 commit 3da8383

File tree

9 files changed

+71
-73
lines changed

9 files changed

+71
-73
lines changed

src/Test/AssetsTestTrait.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,42 @@
1717
trait AssetsTestTrait
1818
{
1919
/**
20-
* Virtual workspace
20+
* The virtual workspace.
2121
*
2222
* @var vfsStreamDirectory|null
2323
*/
24-
protected $root;
25-
26-
/**
27-
* @var AssetsConfig
28-
*/
29-
protected $config;
24+
protected static $root;
3025

3126
/**
3227
* Whether the publishers have been run.
3328
*
3429
* @var bool
3530
*/
36-
private $published = false;
31+
private static $published = false;
32+
33+
/**
34+
* @var AssetsConfig
35+
*/
36+
protected $assets;
3737

3838
/**
3939
* Creates the VFS (if necessary) and updates the Assets config.
4040
*/
4141
protected function setUpAssetsTestTrait(): void
4242
{
43-
if ($this->root === null) {
44-
$this->root = vfsStream::setup('root');
43+
if (self::$root === null) {
44+
self::$root = vfsStream::setup('root');
4545
}
4646

4747
// Create the config
48-
$this->config = new AssetsConfig();
49-
$this->config->directory = $this->root->url() . DIRECTORY_SEPARATOR;
50-
$this->config->useTimestamps = false; // These make testing much harder
48+
$this->assets = new AssetsConfig();
49+
$this->assets->directory = self::$root->url() . DIRECTORY_SEPARATOR;
50+
$this->assets->useTimestamps = false; // These make testing much harder
5151

52-
Asset::useConfig($this->config);
52+
Asset::useConfig($this->assets);
5353

5454
// Add VFS as an allowed Publisher directory
55-
config('Publisher')->restrictions[$this->config->directory] = '*';
55+
config('Publisher')->restrictions[$this->assets->directory] = '*';
5656
}
5757

5858
/**
@@ -61,8 +61,8 @@ protected function setUpAssetsTestTrait(): void
6161
protected function tearDownAssetsTestTrait(): void
6262
{
6363
if (! empty($this->refreshVfs)) {
64-
$this->root = null;
65-
$this->published = false;
64+
self::$root = null;
65+
self::$published = false;
6666
}
6767

6868
Asset::useConfig(null);
@@ -74,14 +74,14 @@ protected function tearDownAssetsTestTrait(): void
7474
*/
7575
protected function publishAll(): void
7676
{
77-
if ($this->published) {
77+
if (self::$published) {
7878
return;
7979
}
8080

8181
foreach (Publisher::discover() as $publisher) {
8282
$publisher->publish();
8383
}
8484

85-
$this->published = true;
85+
self::$published = true;
8686
}
8787
}

tests/AssetTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
use Tatter\Assets\Asset;
44
use Tatter\Assets\Config\Assets as AssetsConfig;
55
use Tatter\Assets\Exceptions\AssetsException;
6-
use Tests\Support\AssetsTestCase;
6+
use Tests\Support\TestCase;
77

88
/**
99
* @internal
1010
*/
11-
final class AssetTest extends AssetsTestCase
11+
final class AssetTest extends TestCase
1212
{
1313
public function testStringable()
1414
{
@@ -48,9 +48,9 @@ public function testLoadsConfig()
4848

4949
public function testUseTimestamps()
5050
{
51-
$this->config->useTimestamps = true;
51+
$this->assets->useTimestamps = true;
5252

53-
$mtime = filemtime($this->config->directory . 'apple.css');
53+
$mtime = filemtime($this->assets->directory . 'apple.css');
5454
$expected = '<link href="http://example.com/assets/apple.css?v=' . $mtime . '" rel="stylesheet" type="text/css" />';
5555
$asset = Asset::createFromPath('apple.css');
5656

tests/AssetsTestTraitTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class AssetsTestTraitTest extends CIUnitTestCase
1414

1515
public function testPublishesOnce()
1616
{
17-
$file = $this->config->directory . $this->config->vendor . 'fruit/third_party.js';
17+
$file = $this->assets->directory . $this->assets->vendor . 'fruit/third_party.js';
1818

1919
$this->publishAll();
2020
$this->assertFileExists($file);
@@ -27,12 +27,10 @@ public function testPublishesOnce()
2727

2828
public function testTearDownRefreshes()
2929
{
30-
$this->assertNotNull($this->root);
30+
$this->assertNotNull(self::$root);
3131

3232
$this->refreshVfs = true;
3333
$this->tearDownAssetsTestTrait();
34-
$this->assertNull($this->root);
35-
36-
$this->refreshVfs = false;
34+
$this->assertNull(self::$root);
3735
}
3836
}

tests/BundleTest.php

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

33
use Tatter\Assets\Asset;
44
use Tatter\Assets\Bundle;
5-
use Tests\Support\AssetsTestCase;
65
use Tests\Support\Bundles\FruitSalad;
6+
use Tests\Support\TestCase;
77

88
/**
99
* @internal
1010
*/
11-
final class BundleTest extends AssetsTestCase
11+
final class BundleTest extends TestCase
1212
{
1313
public function testConstructorPaths()
1414
{

tests/BundlesTestCaseTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ protected function setUp(): void
1515
{
1616
parent::setUp();
1717

18-
copy(SUPPORTPATH . 'Files/apple.css', $this->config->directory . 'apple.css');
19-
copy(SUPPORTPATH . 'Files/banana.js', $this->config->directory . 'banana.js');
18+
copy(SUPPORTPATH . 'Files/apple.css', $this->assets->directory . 'apple.css');
19+
copy(SUPPORTPATH . 'Files/banana.js', $this->assets->directory . 'banana.js');
2020
}
2121

2222
public function bundleProvider(): array

tests/FilterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* @internal
1313
*/
14-
final class FilterTest extends AssetsTestCase
14+
final class FilterTest extends TestCase
1515
{
1616
use FilterTestTrait;
1717

@@ -33,7 +33,7 @@ protected function setUp(): void
3333
{
3434
parent::setUp();
3535

36-
$this->config->routes = [
36+
$this->assets->routes = [
3737
'*' => [
3838
'https://pagecdn.io/lib/cleave/1.6.0/cleave.min.js',
3939
FruitSalad::class,
@@ -102,7 +102,7 @@ public function testFilterWithArguments()
102102

103103
public function testEmptyTags()
104104
{
105-
$this->config->routes = [];
105+
$this->assets->routes = [];
106106

107107
$caller = $this->getFilterCaller(AssetsFilter::class, 'after');
108108

tests/RouteBundleTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
use Tatter\Assets\Exceptions\AssetsException;
44
use Tatter\Assets\RouteBundle;
5-
use Tests\Support\AssetsTestCase;
65
use Tests\Support\Bundles\FruitSalad;
76
use Tests\Support\Bundles\LunchBreak;
7+
use Tests\Support\TestCase;
88

99
/**
1010
* @internal
1111
*/
12-
final class RouteBundleTest extends AssetsTestCase
12+
final class RouteBundleTest extends TestCase
1313
{
1414
protected function setUp(): void
1515
{
1616
parent::setUp();
1717

18-
$this->config->routes = [
18+
$this->assets->routes = [
1919
'*' => [
2020
'https://pagecdn.io/lib/cleave/1.6.0/cleave.min.js',
2121
FruitSalad::class,
@@ -51,7 +51,7 @@ public function testCreateFromRouteUsesCache()
5151
FruitSalad::class,
5252
]));
5353

54-
$this->config->useCache = true;
54+
$this->assets->useCache = true;
5555
$this->assertEmpty(cache()->getCacheInfo());
5656

5757
// Place a fake bundle in the cache
@@ -64,7 +64,7 @@ public function testCreateFromRouteUsesCache()
6464

6565
public function testCreateFromRouteSavesToCache()
6666
{
67-
$this->config->useCache = true;
67+
$this->assets->useCache = true;
6868
$this->assertEmpty(cache()->getCacheInfo());
6969

7070
$result = RouteBundle::createFromRoute('foo');
@@ -74,7 +74,7 @@ public function testCreateFromRouteSavesToCache()
7474

7575
public function testCreateFromRouteEmpty()
7676
{
77-
$this->config->routes['*'] = [];
77+
$this->assets->routes['*'] = [];
7878

7979
$result = RouteBundle::createFromRoute('foo');
8080

@@ -85,7 +85,7 @@ public function testCreateFromRouteEmpty()
8585
public function testCreateFromRouteThrowsNotString()
8686
{
8787
// @phpstan-ignore-next-line
88-
$this->config->routes['invalid'] = [true];
88+
$this->assets->routes['invalid'] = [true];
8989

9090
$this->expectException('InvalidArgumentException');
9191
$this->expectExceptionMessage('Config $route items must be strings.');
@@ -95,7 +95,7 @@ public function testCreateFromRouteThrowsNotString()
9595

9696
public function testCreateFromRouteThrowsInvalidType()
9797
{
98-
$this->config->routes['invalid'] = ['filthyflarmflam'];
98+
$this->assets->routes['invalid'] = ['filthyflarmflam'];
9999

100100
$this->expectException(AssetsException::class);
101101
$this->expectExceptionMessage(lang('Assets.invalidConfigItem', ['']));

tests/_support/AssetsTestCase.php

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

tests/_support/TestCase.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Tests\Support;
4+
5+
use CodeIgniter\Test\CIUnitTestCase;
6+
use Tatter\Assets\Asset;
7+
use Tatter\Assets\Config\Assets as AssetsConfig;
8+
9+
abstract class TestCase extends CIUnitTestCase
10+
{
11+
/**
12+
* @var AssetsConfig
13+
*/
14+
protected $assets;
15+
16+
/**
17+
* Preps the config for the test directory.
18+
*/
19+
protected function setUp(): void
20+
{
21+
parent::setUp();
22+
23+
$this->assets = config('Assets');
24+
$this->assets->directory = SUPPORTPATH . 'Files/';
25+
$this->assets->vendor = 'external/';
26+
$this->assets->useTimestamps = false; // These make testing much harder
27+
28+
Asset::useConfig($this->assets);
29+
}
30+
}

0 commit comments

Comments
 (0)