Skip to content

Commit

Permalink
Optimize tests as trait
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Dec 17, 2021
1 parent 2da9c43 commit a300658
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 67 deletions.
85 changes: 85 additions & 0 deletions src/Test/AssetsTestTrait.php
@@ -0,0 +1,85 @@
<?php

namespace Tatter\Assets\Test;

use CodeIgniter\Publisher\Publisher;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use Tatter\Assets\Asset;
use Tatter\Assets\Config\Assets as AssetsConfig;

/**
* Asset Test Trait
*
* Trait to set up a VFS instance for testing
* Assets, Bundles, and Publishers.
*/
trait AssetsTestTrait
{
/**
* Virtual workspace
*
* @var vfsStreamDirectory|null
*/
protected $root;

/**
* @var AssetsConfig
*/
protected $config;

/**
* Whether the publishers have been run.
*
* @var bool
*/
private $published = false;

/**
* Creates the VFS (if necessary) and updates the Assets config.
*/
protected function setUpAssetsTestTrait(): void
{
if ($this->root === null) {
$this->root = vfsStream::setup('root');
}

// Create the config
$this->config = config('Assets');
$this->config->directory = $this->root->url() . DIRECTORY_SEPARATOR;
$this->config->useTimestamps = false; // These make testing much harder

Asset::useConfig($this->config);

// Add VFS as an allowed Publisher directory
config('Publisher')->restrictions[$this->config->directory] = '*';
}

/**
* Resets the VFS if $refreshVfs is truthy.
*/
protected function tearDownAssetsTestTrait(): void
{
if (! empty($this->refreshVfs)) {
$this->root = null;
$this->published = false;
}
}

/**
* Publishes all files once so they are
* available for bundles.
*/
protected function publishAll(): void
{
if ($this->published) {
return;
}

foreach (Publisher::discover() as $publisher) {
$publisher->publish();
}

$this->published = true;
}
}
15 changes: 4 additions & 11 deletions src/Test/BundlesTestCase.php
Expand Up @@ -2,12 +2,12 @@

namespace Tatter\Assets\Test;

use CodeIgniter\Publisher\Publisher;
use CodeIgniter\Test\CIUnitTestCase;
use Tatter\Assets\Bundle;

abstract class BundlesTestCase extends TestCase
abstract class BundlesTestCase extends CIUnitTestCase
{
private $didPublish = false;
use AssetsTestTrait;

/**
* Publishes all files once so they are
Expand All @@ -17,14 +17,7 @@ protected function setUp(): void
{
parent::setUp();

// Make sure everything is published
if (! $this->didPublish) {
foreach (Publisher::discover() as $publisher) {
$publisher->publish(); // @codeCoverageIgnore
}

$this->didPublish = true;
}
$this->publishAll();
}

/**
Expand Down
52 changes: 0 additions & 52 deletions src/Test/TestCase.php

This file was deleted.

38 changes: 38 additions & 0 deletions tests/AssetsTestTraitTest.php
@@ -0,0 +1,38 @@
<?php

use CodeIgniter\Test\CIUnitTestCase;
use Tatter\Assets\Test\AssetsTestTrait;

/**
* @internal
*/
final class AssetsTestTraitTest extends CIUnitTestCase
{
use AssetsTestTrait;

protected $refreshVfs = false;

public function testPublishesOnce()
{
$file = $this->config->directory . $this->config->vendor . 'fruit/third_party.js';

$this->publishAll();
$this->assertFileExists($file);

unlink($file);

$this->publishAll();
$this->assertFileDoesNotExist($file);
}

public function testTearDownRefreshes()
{
$this->assertNotNull($this->root);

$this->refreshVfs = true;
$this->tearDownAssetsTestTrait();
$this->assertNull($this->root);

$this->refreshVfs = false;
}
}
19 changes: 15 additions & 4 deletions tests/_support/AssetsTestCase.php
Expand Up @@ -2,18 +2,29 @@

namespace Tests\Support;

use Tatter\Assets\Test\TestCase;
use CodeIgniter\Test\CIUnitTestCase;
use Tatter\Assets\Asset;
use Tatter\Assets\Config\Assets as AssetsConfig;

abstract class AssetsTestCase extends TestCase
abstract class AssetsTestCase extends CIUnitTestCase
{
/**
* @var AssetsConfig
*/
protected $config;

/**
* Preps the config for the test directory.
*/
protected function setUp(): void
{
parent::setUp();

$this->config->directory = SUPPORTPATH . 'Files/';
$this->config->vendor = 'external/';
$this->config = config('Assets');
$this->config->directory = SUPPORTPATH . 'Files/';
$this->config->vendor = 'external/';
$this->config->useTimestamps = false; // These make testing much harder

Asset::useConfig($this->config);
}
}
27 changes: 27 additions & 0 deletions tests/_support/Publishers/FruitPublisher.php
@@ -0,0 +1,27 @@
<?php

namespace Tests\Support\Publishers;

use CodeIgniter\Publisher\Publisher;
use Tatter\Assets\Asset;

class FruitPublisher extends Publisher
{
protected $source = SUPPORTPATH . 'Files/external';

/**
* Set the real destination.
*/
public function __construct(?string $source = null, ?string $destination = null)
{
$config = Asset::config();

$this->destination = $config->directory . $config->vendor . 'fruit';

if (! is_dir($this->destination)) {
mkdir($this->destination, 0775, true);
}

parent::__construct($source, $destination);
}
}

0 comments on commit a300658

Please sign in to comment.