Skip to content

Commit 0ab1dde

Browse files
author
Tom Schlick
committed
adding tests & the clear method
1 parent 3ad5d98 commit 0ab1dde

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/HttpPush.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class HttpPush
1313
/**
1414
* @var array
1515
*/
16-
protected $resources = [];
16+
public $resources = [];
1717

1818
/**
1919
* Push a resource onto the queue for the middleware.
@@ -44,4 +44,12 @@ public function generateLinks() : array
4444

4545
return $links;
4646
}
47+
48+
/**
49+
* Clear all resources out of the queue.
50+
*/
51+
public function clear()
52+
{
53+
$this->resources = [];
54+
}
4755
}

tests/HttpPushTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace TomSchlick\ServerPush\Tests;
4+
5+
use PHPUnit_Framework_TestCase;
6+
use TomSchlick\ServerPush\HttpPush;
7+
8+
/**
9+
* Class HttpPushTest
10+
* @package TomSchlick\ServerPush\Tests
11+
*/
12+
class HttpPushTest extends PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @var HttpPush
16+
*/
17+
protected $instance;
18+
19+
public function setUp()
20+
{
21+
$this->instance = new HttpPush();
22+
}
23+
24+
public function tearDown()
25+
{
26+
$this->instance->clear();
27+
parent::tearDown();
28+
}
29+
30+
public function test_add_resources_to_queue()
31+
{
32+
$this->instance->queueResource('style.css', 'style');
33+
34+
$expected = [
35+
[
36+
'path' => 'style.css',
37+
'type' => 'style',
38+
]
39+
];
40+
$this->assertEquals($expected, $this->instance->resources);
41+
}
42+
43+
public function test_resource_generates_link_string()
44+
{
45+
$this->instance->queueResource('/assets/app.js.min', 'script');
46+
47+
$this->assertEquals("</assets/app.js.min>; rel=preload; as=script", $this->instance->generateLinks()[0]);
48+
}
49+
50+
public function test_clear_resources()
51+
{
52+
$this->instance->queueResource('/assets/app.js.min', 'script');
53+
$this->instance->queueResource('style.css', 'style');
54+
55+
$this->instance->clear();
56+
$this->assertTrue(empty($this->instance->resources));
57+
}
58+
59+
}

0 commit comments

Comments
 (0)