Skip to content

Commit

Permalink
Merge pull request #3 from brianvarskonst/feature/fix-tests
Browse files Browse the repository at this point in the history
Feature/Rewrite Tests
  • Loading branch information
brianvarskonst authored Jul 5, 2022
2 parents 8babfbf + 605fb3b commit b51f150
Show file tree
Hide file tree
Showing 25 changed files with 584 additions and 1,005 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ jobs:
runs-on: ubuntu-latest

steps:
# Notice: Ubuntu will already ship composer 2 which currently breaks the used tools.
- uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
tools: composer:v1
# Save the current branch name in an environment variable, to be used for conditional logic below
# Save the current branch name in an environment variable to use it for the conditional logic below
- name: Extract branch name into env
id: git
run: echo "::set-output name=branch::$(echo ${GITHUB_REF##*/})"
Expand All @@ -37,4 +36,7 @@ jobs:
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run Codesniffer
run: composer cs
run: composer cs

- name: Run PHPUnit Tests
run: composer tests
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"autoload-dev": {
"psr-4": {
"Bvsk\\WordPress\\NonceManager\\Tests\\": "tests/src"
"Bvsk\\WordPress\\NonceManager\\Tests\\": "tests"
}
},
"scripts": {
Expand All @@ -48,8 +48,10 @@
"psalm": "@php ./vendor/vimeo/psalm/psalm --no-cache --no-diff --no-progress --output-format=compact",
"tests": "@php ./vendor/bin/phpunit",
"tests:no-cov": "@php ./vendor/bin/phpunit --no-coverage",
"tests:unit": "@php ./vendor/bin/phpunit --testsuite=PHPUnit",
"tests:unit:no-cov": "@php ./vendor/bin/phpunit --testsuite=PHPUnit --no-coverage",
"tests:unit": "@php ./vendor/bin/phpunit --testsuite=Unit",
"tests:unit:no-cov": "@php ./vendor/bin/phpunit --testsuite=Unit --no-coverage",
"tests:integration": "@php ./vendor/bin/phpunit --testsuite=Integration",
"tests:integration:no-cov": "@php ./vendor/bin/phpunit --testsuite=Integration --no-coverage",
"qa": [
"@cs",
"@psalm",
Expand Down
3 changes: 2 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<arg name="extensions" value="php"/>

<file>./src</file>
<file>./tests</file>
<exclude-pattern>*/vendor/*</exclude-pattern>

<rule ref="Inpsyde"/>
Expand All @@ -21,7 +22,7 @@
<property
name="psr4"
type="array"
value="Bvsk\WordPress\NonceManager=>src" />
value="Bvsk\WordPress\NonceManager=>src,Bvsk\WordPress\NonceManager\Tests=>tests" />
</properties>
</rule>

Expand Down
6 changes: 5 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
</coverage>

<testsuites>
<testsuite name="PHPUnit">
<testsuite name="Integration">
<directory>tests/Integration/</directory>
</testsuite>

<testsuite name="Unit">
<directory>tests/Unit/</directory>
</testsuite>
</testsuites>
Expand Down
2 changes: 1 addition & 1 deletion src/NonceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function createFromDefaults(): NonceManager
);
}

public function createNonce(string $type, array $data): Nonce
public function createNonce(string $type, array $data = []): Nonce
{
return $this->nonceFactory->create($type, $data);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nonces/Factory/DefaultNonceProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ interface DefaultNonceProperties

public const REQUEST_NAME = '_wpnonce';

public const LIFETIME = (int) DAY_IN_SECONDS;
public const LIFETIME = DAY_IN_SECONDS;
}
1 change: 0 additions & 1 deletion src/Nonces/Factory/UrlNonceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function getSupportedType(): string

public function create(string $type, array $data = []): Nonce
{

return new UrlNonce(
$data['url'],
$data['action'] ?? DefaultNonceProperties::ACTION,
Expand Down
4 changes: 4 additions & 0 deletions src/Nonces/FieldNonce.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public function render(): void

public function getField(): string
{
if ($this->field === null) {
$this->generate();
}

return $this->field;
}
}
160 changes: 160 additions & 0 deletions tests/Integration/NonceManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

declare(strict_types=1);

namespace Bvsk\WordPress\NonceManager\Tests\Integration;

use Bvsk\WordPress\NonceManager\NonceManager;
use Bvsk\WordPress\NonceManager\Nonces\Factory\DefaultNonceProperties;
use Bvsk\WordPress\NonceManager\Nonces\FieldNonce;
use Bvsk\WordPress\NonceManager\Nonces\Nonce;
use Bvsk\WordPress\NonceManager\Nonces\SimpleNonce;
use Bvsk\WordPress\NonceManager\Nonces\UrlNonce;
use Bvsk\WordPress\NonceManager\Tests\Stubs\FooBarNonceFactory;
use Bvsk\WordPress\NonceManager\Tests\Stubs\FooBarVerifier;
use Bvsk\WordPress\NonceManager\Tests\UnitTestCase;

use function Brain\Monkey\Functions\expect;

class NonceManagerTest extends UnitTestCase
{
/**
* @dataProvider defaultDataProvider
*/
public function testCreateStubInstance(string $token): void
{
$this->assertInstanceOf(
NonceManager::class,
$this->buildTesteeWithStubs($token)
);
}

public function testCreateInstanceFromDefaults(): void
{
$this->assertInstanceOf(
NonceManager::class,
$this->buildTestee()
);
}

/**
* @dataProvider defaultDataProvider
*/
public function testCreateSimpleNonceWithNonceManager(string $token): void
{
$testee = $this->buildTestee();

expect('add_filter')->once();
expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME);
expect('wp_create_nonce')->once()->andReturn($token);

$nonce = $testee->createNonce(SimpleNonce::class);
$this->assertInstanceOf(SimpleNonce::class, $nonce);

$this->assertSame($nonce->getToken(), $token);
$this->assertSame($nonce->getLifetime(), DefaultNonceProperties::LIFETIME);
$this->assertSame($nonce->getAction(), DefaultNonceProperties::ACTION);
$this->assertSame($nonce->getRequestName(), DefaultNonceProperties::REQUEST_NAME);
}

/**
* @dataProvider fieldNonceDataProvider
*/
public function testCreateFieldNonceWithNonceManager(string $token, string $hiddenInput): void
{
$testee = $this->buildTestee();

expect('add_filter')->once();
expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME);
expect('wp_create_nonce')->once()->andReturn($token);
expect('wp_nonce_field')->once()->andReturn($hiddenInput);

$nonce = $testee->createNonce(FieldNonce::class, ['referer' => false]);
$this->assertInstanceOf(FieldNonce::class, $nonce);

$this->assertSame($hiddenInput, $nonce->getField());
$this->assertSame($token, $nonce->getToken());
$this->assertSame(DefaultNonceProperties::LIFETIME, $nonce->getLifetime());
$this->assertSame(DefaultNonceProperties::ACTION, $nonce->getAction());
$this->assertSame(DefaultNonceProperties::REQUEST_NAME, $nonce->getRequestName());
}

/**
* @dataProvider urlNonceDataProvider
*/
public function testCreateUrlNonceWithNonceManager(string $token, string $url): void
{
$testee = $this->buildTestee();

expect('add_filter')->once();
expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME);
expect('wp_create_nonce')->once()->andReturn($token);

$nonce = $testee->createNonce(UrlNonce::class, ['url' => $url]);
$this->assertInstanceOf(UrlNonce::class, $nonce);

$this->assertSame($token, $nonce->getToken());
$this->assertSame(DefaultNonceProperties::LIFETIME, $nonce->getLifetime());
$this->assertSame(DefaultNonceProperties::ACTION, $nonce->getAction());
$this->assertSame(DefaultNonceProperties::REQUEST_NAME, $nonce->getRequestName());
$this->assertSame($url, $nonce->getUrl());
}

/**
* @dataProvider defaultDataProvider
*/
public function testVerifyNonceWithNonceManager(string $token): void
{
$testee = $this->buildTestee();

expect('add_filter')->once();
expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME);
expect('wp_create_nonce')->once()->andReturn($token);

$nonce = $testee->createNonce(SimpleNonce::class);
$this->assertInstanceOf(SimpleNonce::class, $nonce);

expect('wp_verify_nonce')->once()->andReturn(true);

$this->assertTrue($testee->verify($nonce));
}

public function defaultDataProvider(): iterable
{
yield 'test' => [
'token' => 'fooBar',
];
}

public function fieldNonceDataProvider(): iterable
{
yield 'test' => [
'token' => 'fooBar',
'hiddenInput' => '<input type="hidden" />',
];
}

public function urlNonceDataProvider(): iterable
{
yield 'test' => [
'token' => 'fooBar',
'url' => 'https://example.com',
];
}

private function buildTestee(): NonceManager
{
return NonceManager::createFromDefaults();
}

private function buildTesteeWithStubs(string $fooBar): NonceManager
{
return new NonceManager(
new FooBarNonceFactory($fooBar),
new FooBarVerifier(
fn(Nonce $nonce): bool => true,
$fooBar
)
);
}
}
98 changes: 98 additions & 0 deletions tests/Integration/NonceVerifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace Bvsk\WordPress\NonceManager\Tests\Integration;

use Bvsk\WordPress\NonceManager\Nonces\Factory\FieldNonceFactory;
use Bvsk\WordPress\NonceManager\Nonces\Factory\SimpleNonceFactory;
use Bvsk\WordPress\NonceManager\Nonces\Factory\UrlNonceFactory;
use Bvsk\WordPress\NonceManager\Nonces\FieldNonce;
use Bvsk\WordPress\NonceManager\Nonces\Nonce;
use Bvsk\WordPress\NonceManager\Nonces\UrlNonce;
use Bvsk\WordPress\NonceManager\Tests\UnitTestCase;
use Bvsk\WordPress\NonceManager\Verification\NonceVerifier;
use Bvsk\WordPress\NonceManager\Verification\Verifier;
use InvalidArgumentException;

use function Brain\Monkey\Functions\expect;

class NonceVerifierTest extends UnitTestCase
{
public function testCreateNonceVerifier(): void
{
$this->assertInstanceOf(
Verifier::class,
$this->buildTestee()
);
}

/**
* @dataProvider defaultDataProvider
*/
public function testSuccessfulNonceVerifier(Nonce $nonce): void
{
$verifier = $this->buildTestee();

expect('wp_verify_nonce')->once()->andReturn(true);

if ($nonce instanceof FieldNonce) {
expect('check_ajax_referer')->once()->andReturn(true);
}

if ($nonce instanceof UrlNonce) {
expect('check_admin_referer')->once()->andReturn(true);
}

$this->assertTrue($verifier->verify($nonce));
}

/**
* @dataProvider defaultDataProvider
*/
public function testFailingNonceVerifier(Nonce $nonce): void
{
$verifier = $this->buildTestee();

expect('wp_verify_nonce')->once()->andReturn(false);

if ($nonce instanceof UrlNonce) {
expect('check_admin_referer')->once()->andReturn(false);

$this->expectException(InvalidArgumentException::class);
$this->expectDeprecationMessage('Invalid UrlNonce was provided.');
}

if ($nonce instanceof FieldNonce) {
expect('check_ajax_referer')->once()->andReturn(false);

$this->expectException(InvalidArgumentException::class);
$this->expectDeprecationMessage('Invalid FieldNonce was provided.');
}

$this->assertFalse($verifier->verify($nonce));
}

public function defaultDataProvider(): iterable
{
expect('add_filter')->times(3);
expect('wp_create_nonce')->times(3)->andReturn('fooBar');

yield 'testSimpleNonce' => [
'nonce' => (new SimpleNonceFactory())->create('simple'),
];

yield 'testFieldNonce' => [
'nonce' => (new FieldNonceFactory())->create('field', ['referer' => false]),
];

yield 'testUrlNonce' => [
'nonce' => (new UrlNonceFactory())->create('url', ['url' => 'https://www.example.com']),
];
}

private function buildTestee(): NonceVerifier
{
return new NonceVerifier();
}
}
Loading

0 comments on commit b51f150

Please sign in to comment.