Skip to content

Commit efaff07

Browse files
authored
Merge pull request #14 from tattersoftware/test
Test Cases
2 parents b79ee32 + f3e4158 commit efaff07

7 files changed

Lines changed: 103 additions & 76 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ jobs:
8585
steps:
8686
- name: Upload Coveralls results
8787
uses: coverallsapp/github-action@master
88+
continue-on-error: true
8889
with:
8990
github-token: ${{ secrets.GITHUB_TOKEN }}
9091
parallel-finished: true

src/Test/BundlesTestCase.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Tatter\Frontend\Test;
4+
5+
use CodeIgniter\Publisher\Publisher;
6+
use Tatter\Frontend\FrontendBundle;
7+
8+
abstract class BundlesTestCase extends TestCase
9+
{
10+
private $didPublish = false;
11+
12+
/**
13+
* Publishes all files once so they are
14+
* available for bundles.
15+
*/
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
// Make sure everything is published
21+
if (! $this->didPublish) {
22+
foreach (Publisher::discover() as $publisher) {
23+
$publisher->publish();
24+
}
25+
26+
$this->didPublish = true;
27+
}
28+
}
29+
30+
/**
31+
* @dataProvider bundleProvider
32+
*
33+
* @param class-string<FrontendBundle> $class
34+
* @param string[] $expectedHeadFiles
35+
* @param string[] $expectedBodyFiles
36+
*/
37+
public function testBundlesFiles(string $class, array $expectedHeadFiles, array $expectedBodyFiles): void
38+
{
39+
$bundle = new $class();
40+
$head = $bundle->head();
41+
$body = $bundle->body();
42+
43+
foreach ($expectedHeadFiles as $file) {
44+
$this->assertStringContainsString($file, $head);
45+
}
46+
47+
foreach ($expectedBodyFiles as $file) {
48+
$this->assertStringContainsString($file, $body);
49+
}
50+
}
51+
52+
/**
53+
* Returns an array of items to test with each item
54+
* as a triple of [string publisherClassName, string[] headFileNames, string[] bodyFileNames]
55+
*/
56+
abstract public function bundleProvider(): array;
57+
}

src/Test/PublishersTestCase.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Tatter\Frontend\Test;
4+
5+
use Tatter\Frontend\FrontendPublisher;
6+
7+
abstract class PublishersTestCase extends TestCase
8+
{
9+
/**
10+
* @dataProvider publisherProvider
11+
*
12+
* @param class-string<FrontendPublisher> $class
13+
* @param string[] $expected
14+
*/
15+
public function testPublishesFiles(string $class, array $expected): void
16+
{
17+
$publisher = new $class();
18+
$result = $publisher->publish();
19+
20+
// Verify that publication succeeded
21+
$this->assertTrue($result);
22+
$this->assertSame([], $publisher->getErrors());
23+
$this->assertNotSame([], $publisher->getPublished());
24+
25+
// Check for each of the expected files
26+
foreach ($expected as $path) {
27+
$file = $this->config->directory . $this->config->vendor . $path;
28+
$this->assertFileExists($file);
29+
}
30+
}
31+
32+
/**
33+
* Returns an array of items to test with each item
34+
* as a tuple of [string publisherClassName, string[] expectedFileNames]
35+
*/
36+
abstract public function publisherProvider(): array;
37+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\Support;
3+
namespace Tatter\Frontend\Test;
44

55
use CodeIgniter\Test\CIUnitTestCase;
66
use org\bovigo\vfs\vfsStream;

tests/BundlesTest.php

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,14 @@
22

33
namespace Tatter\Frontend\Bundles;
44

5-
use CodeIgniter\Publisher\Publisher;
6-
use Tatter\Frontend\FrontendBundle;
7-
use Tests\Support\TestCase;
5+
use Tatter\Frontend\Test\BundlesTestCase;
86

97
/**
108
* @internal
119
*/
12-
final class BundlesTest extends TestCase
10+
final class BundlesTest extends BundlesTestCase
1311
{
14-
private $didPublish = false;
15-
16-
/**
17-
* Publishes all files once so they are
18-
* available for bundles.
19-
*/
20-
protected function setUp(): void
21-
{
22-
parent::setUp();
23-
24-
// Make sure everything is published
25-
if (! $this->didPublish) {
26-
foreach (Publisher::discover() as $publisher) {
27-
$publisher->publish();
28-
}
29-
30-
$this->didPublish = true;
31-
}
32-
}
33-
34-
/**
35-
* @dataProvider bundleProvider
36-
*
37-
* @param class-string<FrontendBundle> $class
38-
* @param string[] $expectedHeadFiles
39-
* @param string[] $expectedBodyFiles
40-
*/
41-
public function testBundlesFiles(string $class, array $expectedHeadFiles, array $expectedBodyFiles): void
42-
{
43-
$bundle = new $class();
44-
$head = $bundle->head();
45-
$body = $bundle->body();
46-
47-
foreach ($expectedHeadFiles as $file) {
48-
$this->assertStringContainsString($file, $head);
49-
}
50-
51-
foreach ($expectedBodyFiles as $file) {
52-
$this->assertStringContainsString($file, $body);
53-
}
54-
}
55-
56-
public function bundleProvider()
12+
public function bundleProvider(): array
5713
{
5814
return [
5915
[

tests/FrontendPublisherTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
use Tatter\Frontend\FrontendPublisher;
4-
use Tests\Support\TestCase;
4+
use Tatter\Frontend\Test\TestCase;
55

66
/**
77
* @internal

tests/PublishersTest.php

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,14 @@
22

33
namespace Tatter\Frontend\Publishers;
44

5-
use Tatter\Frontend\FrontendPublisher;
6-
use Tests\Support\TestCase;
5+
use Tatter\Frontend\Test\PublishersTestCase;
76

87
/**
98
* @internal
109
*/
11-
final class PublishersTest extends TestCase
10+
final class PublishersTest extends PublishersTestCase
1211
{
13-
/**
14-
* @dataProvider publisherProvider
15-
*
16-
* @param class-string<FrontendPublisher> $class
17-
* @param string[] $expected
18-
*/
19-
public function testPublishesFiles(string $class, array $expected): void
20-
{
21-
$publisher = new $class();
22-
$result = $publisher->publish();
23-
24-
// Verify that publication succeeded
25-
$this->assertTrue($result);
26-
$this->assertSame([], $publisher->getErrors());
27-
$this->assertNotSame([], $publisher->getPublished());
28-
29-
// Check for each of the expected files
30-
foreach ($expected as $path) {
31-
$file = $this->config->directory . $this->config->vendor . $path;
32-
$this->assertFileExists($file);
33-
}
34-
}
35-
36-
public function publisherProvider()
12+
public function publisherProvider(): array
3713
{
3814
return [
3915
[

0 commit comments

Comments
 (0)