Skip to content

Commit

Permalink
Merge pull request #12 from tito10047/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
Huluti authored Feb 14, 2025
2 parents 539cc62 + 150071c commit e36fdd6
Show file tree
Hide file tree
Showing 15 changed files with 395 additions and 34 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,3 @@ jobs:

- name: Run phpstan
run: vendor/bin/phpstan analyse -c phpstan.neon src

- name: Run tests
run: vendor/bin/phpunit --configuration=./tests/phpunit.xml.dist
62 changes: 62 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: "Tests"

on:
pull_request:
push:
schedule:
- cron: '* 8 * * *'

jobs:
compatibility:
name: "Tests"

runs-on: ${{ matrix.operating-system }}

strategy:
matrix:
dependencies:
- "lowest"
- "highest"
php-version:
- "8.1"
- "8.2"
- "8.3"
- "8.4"
operating-system:
- "ubuntu-latest"
- "windows-latest"

steps:
- name: "Checkout"
uses: "actions/checkout@v3"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "pcov"
php-version: "${{ matrix.php-version }}"
ini-values: memory_limit=-1

- name: "Cache dependencies"
uses: "actions/cache@v3"
with:
path: |
~/.composer/cache
vendor
key: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"
restore-keys: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"

- name: "Install lowest dependencies"
if: ${{ matrix.dependencies == 'lowest' }}
run: "composer update --prefer-lowest --no-interaction --no-progress --no-suggest"

- name: "Install highest dependencies"
if: ${{ matrix.dependencies == 'highest' }}
run: "composer update --no-interaction --no-progress --no-suggest"

- name: "Install locked dependencies"
if: ${{ matrix.dependencies == 'locked' }}
run: "composer install --no-interaction --no-progress --no-suggest"

- name: "Tests"
run: "composer test"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
composer.lock
tools/
tools/
/.phpunit.result.cache
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"autoload-dev": {
"psr-4": {
"Huluti\\AltchaBundle\\": "tests/"
"Huluti\\AltchaBundle\\Tests\\": "tests/"
}
},
"require-dev": {
Expand All @@ -45,6 +45,11 @@
"rector/rector": "^0.14.5",
"symfony/asset-mapper": "6.4 || ^7.0",
"symfony/stimulus-bundle": "^2.16",
"phpunit/phpunit": "^9.6"
"phpunit/phpunit": "^10.5"
},
"scripts" : {
"test": [
"phpunit"
]
}
}
7 changes: 6 additions & 1 deletion config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ services:
autowire: true
autoconfigure: true
arguments:
$hmacKey: '%huluti_altcha.hmacKey%'
$challengeOptionResolver: '@Huluti\AltchaBundle\Service\ChallengeResolverInterface'
Huluti\AltchaBundle\Service\ChallengeResolverInterface:
class: Huluti\AltchaBundle\Service\ChallengeResolver
arguments:
$hmacKey: '%huluti_altcha.hmacKey%'
$expires: '+15 minute'
15 changes: 15 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Ace Bundle Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
12 changes: 3 additions & 9 deletions src/Controller/HulutiAltchaChallengeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,21 @@

use AltchaOrg\Altcha\Altcha;
use AltchaOrg\Altcha\ChallengeOptions;
use Huluti\AltchaBundle\Service\ChallengeResolverInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;

class HulutiAltchaChallengeController extends AbstractController
{
public function __construct(private readonly string $hmacKey)
public function __construct(private readonly ChallengeResolverInterface $challengeOptionResolver)
{
}

#[Route('/huluti_altcha/challenge', name: 'huluti_altcha_challenge')]
public function challenge(): JsonResponse
{
$options = new ChallengeOptions([
'hmacKey' => $this->hmacKey,
'maxNumber' => 100000,
'expires' => (new \DateTime())->modify('+15 minute'),
]);

$challenge = Altcha::createChallenge($options);

return new JsonResponse($challenge);
return new JsonResponse($this->challengeOptionResolver->getChallenge());
}
}
30 changes: 30 additions & 0 deletions src/Service/ChallengeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Huluti\AltchaBundle\Service;

use AltchaOrg\Altcha\Altcha;
use AltchaOrg\Altcha\Challenge;
use AltchaOrg\Altcha\ChallengeOptions;

class ChallengeResolver implements ChallengeResolverInterface
{

public function __construct(
private readonly string $hmacKey,
private readonly string $expires,
)
{
}

public function getChallenge(): Challenge
{
$options = new ChallengeOptions([
'hmacKey' => $this->hmacKey,
'maxNumber' => 100000,
'expires' => (new \DateTime())->modify($this->expires),
]);
return Altcha::createChallenge($options);
}
}
12 changes: 12 additions & 0 deletions src/Service/ChallengeResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Huluti\AltchaBundle\Service;

use AltchaOrg\Altcha\Challenge;

interface ChallengeResolverInterface
{
public function getChallenge(): Challenge;
}
27 changes: 27 additions & 0 deletions tests/AltchaBundleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Huluti\AltchaBundle\Tests;

use Huluti\AltchaBundle\DependencyInjection\Compiler\HulutiAltchaBundleCompilerPass;
use Huluti\AltchaBundle\HulutiAltchaBundle;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class AltchaBundleTest extends TestCase
{
public function testBuild(): void
{
$container = new ContainerBuilder();
$bundle = new HulutiAltchaBundle();
$bundle->build($container);

$this->assertNotEmpty(array_filter(
$container->getCompilerPassConfig()->getPasses(),
function ($value) {
return $value instanceof HulutiAltchaBundleCompilerPass;
}
));
}
}
46 changes: 46 additions & 0 deletions tests/Controller/HulutiAltchaChallengeControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Huluti\AltchaBundle\Tests\Controller;

use AltchaOrg\Altcha\Altcha;
use AltchaOrg\Altcha\ChallengeOptions;
use Huluti\AltchaBundle\Controller\HulutiAltchaChallengeController;
use Huluti\AltchaBundle\Service\ChallengeResolverInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\JsonResponse;

class HulutiAltchaChallengeControllerTest extends KernelTestCase
{

public function testChallenge()
{

$challengeOptions = new ChallengeOptions([
'hmacKey' => 'test-key',
'maxNumber' => 100000,
'number' => 10,
'expires' => (new \DateTime())->modify("+1 day"),
]);
$challenge = Altcha::createChallenge($challengeOptions);

$challengeResolver = $this->createMock(ChallengeResolverInterface::class);
$challengeResolver->method('getChallenge')->willReturn($challenge);

$controller = new HulutiAltchaChallengeController($challengeResolver);

$response = $controller->challenge();

$this->assertInstanceOf(JsonResponse::class, $response);

$payload = json_decode($response->getContent(), true);

$payload['number'] = 10;

$isValid = Altcha::verifySolution($payload, 'test-key');

$this->assertTrue($isValid);
}

}
38 changes: 38 additions & 0 deletions tests/DependencyInjection/Compiler/TwigFormPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Huluti\AltchaBundle\Tests\DependencyInjection\Compiler;

use Huluti\AltchaBundle\DependencyInjection\Compiler\HulutiAltchaBundleCompilerPass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class TwigFormPassTest extends TestCase
{
public function testProcessHasNotTwigFormResources(): void
{
$container = $this->createMock(ContainerBuilder::class);
$container->expects($this->once())->method('hasParameter')
->with('twig.form.resources')->willReturn(false);

$container->expects($this->never())->method('setParameter');

$compiler = new HulutiAltchaBundleCompilerPass();
$compiler->process($container);
}

public function testProcessHasTwigFormResources(): void
{
$container = new ContainerBuilder();
$container->setParameter('twig.form.resources', ['foo']);

$compiler = new HulutiAltchaBundleCompilerPass();
$compiler->process($container);

$this->assertSame(
['@HulutiAltcha/fields.html.twig', 'foo'],
$container->getParameter('twig.form.resources')
);
}
}
45 changes: 45 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Huluti\AltchaBundle\Tests\DependencyInjection;

use Huluti\AltchaBundle\DependencyInjection\Configuration;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Processor;

class ConfigurationTest extends TestCase
{
public function testProcessConfiguration(): void
{
$configuration = new Configuration();
$processor = new Processor();
$config = $processor->processConfiguration($configuration, [
'huluti_altcha' => [
'hmacKey' => 'test',
],
]);

$this->assertArrayHasKey('enable', $config);
$this->assertTrue($config['enable']);

$this->assertArrayHasKey('floating', $config);
$this->assertFalse($config['floating']);

$this->assertArrayHasKey('use_stimulus', $config);
$this->assertNull($config['use_stimulus']);

$this->assertArrayHasKey('hide_logo', $config);
$this->assertFalse($config['hide_logo']);

$this->assertArrayHasKey('hide_footer', $config);
$this->assertFalse($config['hide_footer']);

$this->assertArrayHasKey('altcha_js_path', $config);
$this->assertIsString($config['altcha_js_path']);

$this->assertArrayHasKey('hmacKey', $config);
$this->assertNotNull($config['hmacKey']);

}
}
Loading

0 comments on commit e36fdd6

Please sign in to comment.