Skip to content

Commit c0d4f4e

Browse files
committed
Optimize tests as trait
1 parent 2da9c43 commit c0d4f4e

File tree

6 files changed

+164
-67
lines changed

6 files changed

+164
-67
lines changed

src/Test/AssetsTestTrait.php

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

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/AssetsTestTraitTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use CodeIgniter\Test\CIUnitTestCase;
4+
use Tatter\Assets\Test\AssetsTestTrait;
5+
use Tatter\Assets\Test\BundlesTestCase;
6+
use Tests\Support\Bundles\FruitSalad;
7+
8+
/**
9+
* @internal
10+
*/
11+
final class AssetsTestTraitTest extends CIUnitTestCase
12+
{
13+
use AssetsTestTrait;
14+
15+
public function testPublishesOnce()
16+
{
17+
$file = $this->config->directory . $this->config->vendor . 'fruit/third_party.js';
18+
19+
$this->publishAll();
20+
$this->assertFileExists($file);
21+
22+
unlink($file);
23+
24+
$this->publishAll();
25+
$this->assertFileDoesNotExist($file);
26+
}
27+
28+
public function testTearDownRefreshes()
29+
{
30+
$this->assertNotNull($this->root);
31+
32+
$this->refreshVfs = true;
33+
$this->tearDownAssetsTestTrait();
34+
$this->assertNull($this->root);
35+
36+
$this->refreshVfs = false;
37+
}
38+
}

tests/_support/AssetsTestCase.php

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

33
namespace Tests\Support;
44

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

7-
abstract class AssetsTestCase extends TestCase
8+
abstract class AssetsTestCase extends CIUnitTestCase
89
{
910
/**
1011
* Preps the config for the test directory.
@@ -13,7 +14,11 @@ protected function setUp(): void
1314
{
1415
parent::setUp();
1516

16-
$this->config->directory = SUPPORTPATH . 'Files/';
17-
$this->config->vendor = 'external/';
17+
$this->config = config('Assets');
18+
$this->config->directory = SUPPORTPATH . 'Files/';
19+
$this->config->vendor = 'external/';
20+
$this->config->useTimestamps = false; // These make testing much harder
21+
22+
Asset::useConfig($this->config);
1823
}
1924
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Tests\Support\Publishers;
4+
5+
use CodeIgniter\Publisher\Publisher;
6+
use Tatter\Assets\Asset;
7+
8+
class FruitPublisher extends Publisher
9+
{
10+
protected $source = SUPPORTPATH . 'Files/external';
11+
12+
/**
13+
* Set the real destination.
14+
*/
15+
public function __construct(?string $source = null, ?string $destination = null)
16+
{
17+
$config = Asset::config();
18+
19+
$this->destination = $config->directory . $config->vendor . 'fruit';
20+
21+
if (! is_dir($this->destination)) {
22+
mkdir($this->destination, 0775, true);
23+
}
24+
25+
parent::__construct($source, $destination);
26+
}
27+
}

0 commit comments

Comments
 (0)