Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/Test/AssetsTestTrait.php
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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);
}
}