Skip to content

Commit

Permalink
Merge branch 'master' into feature/symfony-update
Browse files Browse the repository at this point in the history
  • Loading branch information
nusje2000 authored Jul 18, 2020
2 parents 1d596c4 + 105c528 commit 71ab7a3
Show file tree
Hide file tree
Showing 15 changed files with 1,370 additions and 32 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Build

on: [pull_request, push]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v1
with:
php-version: 7.2

- uses: actions/checkout@v1

- name: Validate composer.json and composer.lock
run: |
composer clear-cache
composer validate
- name: Install dependencies
run: |
composer install --prefer-dist --no-progress --no-suggest
- name: PHPUnit
run: |
composer run phpunit
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/*
vendor/*
composer.phar
composer.lock
composer.lock
.phpunit.result.cache
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,10 @@
},
"require-dev": {
"phpunit/phpunit": "^8.2"
},
"scripts": {
"phpunit": [
"vendor/bin/phpunit ./tests"
]
}
}
19 changes: 19 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.5/phpunit.xsd"
colors="true"
convertNoticesToExceptions="false"
>
<testsuites>
<testsuite name="default">
<directory suffix=".php">./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
3 changes: 3 additions & 0 deletions src/Exception/TransitionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public function __construct(FormFlowInterface $flow, string $message, int $code
$this->flow = $flow;
}

/**
* @codeCoverageIgnore
*/
public function getFlow(): FormFlowInterface
{
return $this->flow;
Expand Down
3 changes: 3 additions & 0 deletions src/FormFlowEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ final class FormFlowEvents
*/
public const RESET = 'form_flow.reset';

/**
* @codeCoverageIgnore
*/
private function __construct()
{
}
Expand Down
3 changes: 1 addition & 2 deletions src/Transitioner.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function transition(FormFlowInterface $flow): Status
}

$request = Request::createByHttpRequestAndFlow($this->getHttpRequest(), $flow);
$status = new Status(Status::FAILURE);
switch ($request->getValue()) {
case (Request::FORWARDS):
// For now, we don't support multiple forwards transitions.
Expand All @@ -80,8 +81,6 @@ public function transition(FormFlowInterface $flow): Status
case (Request::RESET):
$status = $this->reset($flow);
break;
default:
$status = new Status(Status::FAILURE);
}

return $status;
Expand Down
238 changes: 238 additions & 0 deletions tests/ContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
<?php

declare(strict_types=1);

namespace Aeviiq\FormFlow\Tests;

use Aeviiq\FormFlow\Context;
use Aeviiq\FormFlow\Exception\LogicException;
use Aeviiq\FormFlow\Step\StepInterface;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use stdClass;

final class ContextTest extends TestCase
{
public function testConstruct(): void
{
$data = new stdClass();
$context = new Context($data, 2);
self::assertSame($data, $context->getData());
self::assertSame(1, $context->getCurrentStepNumber());

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The total number of steps must be 2 or more. "0" given.');
new Context(new stdClass(), 0);
}

public function testSetCurrentStepNumberWithNegativeNumber(): void
{
$context = new Context(new stdClass(), 2);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Step number "-1" is invalid for this context.');
$context->setCurrentStepNumber(-1);
}

public function testSetCyrrentStepNumberWithNonExistentStep(): void
{
$context = new Context(new stdClass(), 2);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Step number "3" is invalid for this context.');
$context->setCurrentStepNumber(3);
}

public function testSetCompleted(): void
{
$context = new Context(new stdClass(), 2);
$step = $this->createStep(1);

$context->setCompleted($step);
self::assertTrue($context->isCompleted($step));
}

public function testSetCompletedWithNegativeStepNumber(): void
{
$context = new Context(new stdClass(), 2);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Step number "-1" is invalid for this context.');
$context->setCompleted($this->createStep(-1));
}

public function testSetCompletedWithNonExistentStep(): void
{
$context = new Context(new stdClass(), 2);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Step number "3" is invalid for this context.');
$context->setCompleted($this->createStep(3));
}

public function testSetCompletedWithNumberGreaterThanCurrentStep(): void
{
$context = new Context(new stdClass(), 2);
$step = $this->createStep(2);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Can not complete a step that is greater than or equal to the current step.');
$context->setCompleted($step);
}

public function testUnsetCompleted(): void
{
$context = new Context(new stdClass(), 2);
$step = $this->createStep(1);

$context->setCompleted($step);
self::assertTrue($context->isCompleted($step));
$context->unsetCompleted($step);
self::assertFalse($context->isCompleted($step));
$context->unsetCompleted($step);
self::assertFalse($context->isCompleted($step));
}

public function testSetSoftSkipped(): void
{
$context = new Context(new stdClass(), 3);

$step = $this->createStep(2);
$context->setSoftSkipped($step);
self::assertTrue($context->isSoftSkipped($step));
}

public function testSetSoftSkippedWithNegativeStepNumber(): void
{
$context = new Context(new stdClass(), 2);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Step number "-1" is invalid for this context.');
$context->setSoftSkipped($this->createStep(-1));
}

public function testSetSoftSkippedWithNonExistentStep(): void
{
$context = new Context(new stdClass(), 2);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Step number "3" is invalid for this context.');
$context->setSoftSkipped($this->createStep(3));
}

public function testSetSoftSkippedOnFirstStep(): void
{
$context = new Context(new stdClass(), 2);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('It is not yet possible to skip the first step of a form flow.');
$context->setSoftSkipped($this->createStep(1));
}

public function testSetSoftSkippedOnLastStep(): void
{
$context = new Context(new stdClass(), 2);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('It is not possible to skip the last step of a form flow.');
$context->setSoftSkipped($this->createStep(2));
}

public function testUnsetSoftSkipped(): void
{
$context = new Context(new stdClass(), 3);

$step = $this->createStep(2);
$context->setSoftSkipped($step);
self::assertTrue($context->isSoftSkipped($step));
$context->unsetSoftSkipped($step);
self::assertFalse($context->isSoftSkipped($step));
$context->unsetSoftSkipped($step);
self::assertFalse($context->isSoftSkipped($step));
}

public function testSetHardSkipped(): void
{
$context = new Context(new stdClass(), 3);

$step = $this->createStep(2);
$context->setHardSkipped($step);
self::assertTrue($context->isHardSkipped($step));
}

public function testSetHardSkippedWithNegativeStepNumber(): void
{
$context = new Context(new stdClass(), 2);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Step number "-1" is invalid for this context.');
$context->setHardSkipped($this->createStep(-1));
}

public function testSetHardSkippedWithNonExistentStep(): void
{
$context = new Context(new stdClass(), 2);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Step number "3" is invalid for this context.');
$context->setHardSkipped($this->createStep(3));
}

public function testSetHardSkippedOnFirstStep(): void
{
$context = new Context(new stdClass(), 2);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('It is not yet possible to skip the first step of a form flow.');
$context->setHardSkipped($this->createStep(1));
}

public function testSetHardSkippedOnLastStep(): void
{
$context = new Context(new stdClass(), 2);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('It is not possible to skip the last step of a form flow.');
$context->setHardSkipped($this->createStep(2));
}

public function testUnsetHardSkipped(): void
{
$context = new Context(new stdClass(), 3);

$step = $this->createStep(2);
$context->setHardSkipped($step);
self::assertTrue($context->isHardSkipped($step));
$context->unsetHardSkipped($step);
self::assertFalse($context->isHardSkipped($step));
$context->unsetHardSkipped($step);
self::assertFalse($context->isHardSkipped($step));
}

public function testIsSkipped(): void
{
$context = new Context(new stdClass(), 3);

$step = $this->createStep(2);

$context->setHardSkipped($step);
self::assertTrue($context->isSkipped($step));

$context->setSoftSkipped($step);
self::assertTrue($context->isSkipped($step));

$context->unsetHardSkipped($step);
self::assertTrue($context->isSkipped($step));

$context->unsetSoftSkipped($step);
self::assertFalse($context->isSkipped($step));
}

private function createStep(int $stepNumber): StepInterface
{
$step = $this->createStub(StepInterface::class);
$step->method('getNumber')->willReturn($stepNumber);

return $step;
}
}
Loading

0 comments on commit 71ab7a3

Please sign in to comment.