Skip to content

Commit bce640b

Browse files
committed
Optimize test as trait
1 parent 2da9c43 commit bce640b

File tree

4 files changed

+92
-65
lines changed

4 files changed

+92
-65
lines changed

src/Test/AssetsTestTrait.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Tatter\Assets\Test;
4+
5+
use CodeIgniter\Publisher\Publisher;
6+
use org\bovigo\vfs\vfsStream;
7+
use org\bovigo\vfs\vfsStreamDirectory;
8+
use Tatter\Assets\Asset;
9+
use Tatter\Assets\Config\Assets as AssetsConfig;
10+
11+
/**
12+
* Asset Test Trait
13+
*
14+
* Trait to set up a VFS instance for testing
15+
* Assets, Bundles, and Publishers.
16+
*/
17+
trait AssetsTestTrait
18+
{
19+
/**
20+
* Virtual workspace
21+
*
22+
* @var vfsStreamDirectory|null
23+
*/
24+
protected $root;
25+
26+
/**
27+
* @var AssetsConfig
28+
*/
29+
protected $config;
30+
31+
/**
32+
* Whether the publishers have been run.
33+
*/
34+
private $published = false;
35+
36+
/**
37+
* Creates the VFS (if necessary) and updates the Assets config.
38+
*/
39+
protected function setUpAssetsTestTrait(): void
40+
{
41+
if ($this->root === null) {
42+
$this->root = vfsStream::setup('root');
43+
}
44+
45+
// Create the config
46+
$this->config = config('Assets');
47+
$this->config->directory = $this->root->url() . DIRECTORY_SEPARATOR;
48+
$this->config->useTimestamps = false; // These make testing much harder
49+
50+
Asset::useConfig($this->config);
51+
52+
// Add VFS as an allowed Publisher directory
53+
config('Publisher')->restrictions[$this->config->directory] = '*';
54+
}
55+
56+
/**
57+
* Resets the VFS if $refreshVfs is truthy.
58+
*/
59+
protected function tearDownAssetsTestTrait(): void
60+
{
61+
if (! empty($this->refreshVfs)) {
62+
$this->root = null;
63+
$this->published = false;
64+
}
65+
}
66+
67+
/**
68+
* Publishes all files once so they are
69+
* available for bundles.
70+
*/
71+
protected function publishAll(): void
72+
{
73+
if ($this->published) {
74+
return;
75+
}
76+
77+
foreach (Publisher::discover() as $publisher) {
78+
$publisher->publish(); // @codeCoverageIgnore
79+
}
80+
81+
$this->published = true;
82+
}
83+
}

src/Test/BundlesTestCase.php

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

33
namespace Tatter\Assets\Test;
44

5-
use CodeIgniter\Publisher\Publisher;
5+
use CodeIgniter\Test\CIUnitTestCase;
66
use Tatter\Assets\Bundle;
77

8-
abstract class BundlesTestCase extends TestCase
8+
abstract class BundlesTestCase extends CIUnitTestCase
99
{
10-
private $didPublish = false;
10+
use AssetsTestTrait;
1111

1212
/**
1313
* Publishes all files once so they are
@@ -17,14 +17,7 @@ protected function setUp(): void
1717
{
1818
parent::setUp();
1919

20-
// Make sure everything is published
21-
if (! $this->didPublish) {
22-
foreach (Publisher::discover() as $publisher) {
23-
$publisher->publish(); // @codeCoverageIgnore
24-
}
25-
26-
$this->didPublish = true;
27-
}
20+
$this->publishAll();
2821
}
2922

3023
/**

src/Test/TestCase.php

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

tests/_support/AssetsTestCase.php

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

33
namespace Tests\Support;
44

5-
use Tatter\Assets\Test\TestCase;
5+
use CodeIgniter\Test\CIUnitTestCase;
6+
use Tatter\Assets\Test\AssetsTestTrait;
67

7-
abstract class AssetsTestCase extends TestCase
8+
abstract class AssetsTestCase extends CIUnitTestCase
89
{
10+
use AssetsTestTrait;
11+
912
/**
1013
* Preps the config for the test directory.
1114
*/

0 commit comments

Comments
 (0)