-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
444 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: CI-coverage | ||
|
||
on: [ push ] | ||
|
||
jobs: | ||
build-test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
php-version: [ 8.2, 8.3 ] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: php-actions/composer@v6 | ||
|
||
- name: PHPUnit Tests | ||
uses: php-actions/phpunit@v3 | ||
with: | ||
php_version: ${{ matrix.php-version }} | ||
php_extensions: xdebug | ||
bootstrap: vendor/autoload.php | ||
configuration: phpunit.xml | ||
coverage_text: true | ||
env: | ||
XDEBUG_MODE: coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
composer.lock | ||
vendor | ||
Packages | ||
.phpunit.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Unit\Factory; | ||
|
||
use Flowpack\ContentSecurityPolicy\Factory\PolicyFactory; | ||
use Flowpack\ContentSecurityPolicy\Model\Directive; | ||
use Flowpack\ContentSecurityPolicy\Model\Nonce; | ||
use Flowpack\ContentSecurityPolicy\Model\Policy; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\UsesClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(PolicyFactory::class)] | ||
#[UsesClass(Policy::class)] | ||
#[UsesClass(Directive::class)] | ||
class PolicyFactoryTest extends TestCase | ||
{ | ||
public function testCreateShouldReturnPolicy(): void | ||
{ | ||
$nonceMock = $this->createMock(Nonce::class); | ||
|
||
$defaultDirective = [ | ||
'base-uri' => [ | ||
'self', | ||
], | ||
'script-src' => [ | ||
'self', | ||
], | ||
]; | ||
$customDirective = [ | ||
'script-src' => [ | ||
'{nonce}', | ||
], | ||
]; | ||
|
||
$expected = [ | ||
'base-uri' => [ | ||
"'self'", | ||
], | ||
'script-src' => [ | ||
"'self'", | ||
"'nonce-'", | ||
], | ||
]; | ||
|
||
$result = PolicyFactory::create($nonceMock, $defaultDirective, $customDirective); | ||
|
||
self::assertSame($expected, $result->getDirectives()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Unit\Helpers; | ||
|
||
use Flowpack\ContentSecurityPolicy\Helpers\TagHelper; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(TagHelper::class)] | ||
class TagHelperTest extends TestCase | ||
{ | ||
public function testTagHasAttributeWithoutValueShouldReturnTrue(): void | ||
{ | ||
$tag = '<script src="https://google.com"></script>'; | ||
self::assertTrue(TagHelper::tagHasAttribute($tag, 'src')); | ||
} | ||
|
||
public function testTagHasAttributeWithoutValueShouldReturnFalse(): void | ||
{ | ||
$tag = '<script src="https://google.com"></script>'; | ||
self::assertFalse(TagHelper::tagHasAttribute($tag, 'bar')); | ||
} | ||
|
||
public function testTagHasAttributeWithValueShouldReturnTrue(): void | ||
{ | ||
$tag = '<script src="https://google.com"></script>'; | ||
self::assertTrue(TagHelper::tagHasAttribute($tag, 'src', 'https://google.com')); | ||
} | ||
|
||
public function testTagHasAttributeWithValueShouldReturnFalse(): void | ||
{ | ||
$tag = '<script src="https://google.com"></script>'; | ||
self::assertFalse(TagHelper::tagHasAttribute($tag, 'src', 'another value')); | ||
} | ||
|
||
public function testTagChangeAttributeValueShouldChangeValue(): void | ||
{ | ||
$tag = '<script src="https://google.com"></script>'; | ||
|
||
self::assertSame( | ||
'<script src="https://test.com"></script>', | ||
TagHelper::tagChangeAttributeValue($tag, 'src', 'https://test.com') | ||
); | ||
} | ||
|
||
public function testTagChangeAttributeValueShouldDoNothingIfAttributeDoesntExist(): void | ||
{ | ||
$tag = '<script src="https://google.com"></script>'; | ||
|
||
self::assertSame( | ||
'<script src="https://google.com"></script>', | ||
TagHelper::tagChangeAttributeValue($tag, 'nonce', 'da65sf1g') | ||
); | ||
} | ||
|
||
public function testTagAddAttributeShouldAddAttributeWithValue(): void | ||
{ | ||
$tag = '<script src="https://google.com"></script>'; | ||
|
||
self::assertSame( | ||
'<script src="https://google.com" nonce="da65sf1g"></script>', | ||
TagHelper::tagAddAttribute($tag, 'nonce', 'da65sf1g') | ||
); | ||
} | ||
|
||
public function testTagAddAttributeShouldAddAttributeWithoutValue(): void | ||
{ | ||
$tag = '<script src="https://google.com"></script>'; | ||
|
||
self::assertSame( | ||
'<script src="https://google.com" defer></script>', | ||
TagHelper::tagAddAttribute($tag, 'defer') | ||
); | ||
} | ||
} |
Oops, something went wrong.