From b8b3a80baf8fa6b74de2f6e46023d7d47e0116bc Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Tue, 16 Jan 2024 21:00:24 +0100 Subject: [PATCH] switch to attributes --- src/test/php/ParamRequestTest.php | 69 ++--- src/test/php/ParamsTest.php | 46 +-- src/test/php/ValueReaderTest.php | 286 +++++++----------- src/test/php/ValueValidatorTest.php | 131 +++----- src/test/php/broker/ParamBrokersTest.php | 92 +++--- .../RequestBrokerTargetMethodsOfTest.php | 46 +-- src/test/php/broker/RequestBrokerTest.php | 55 ++-- .../php/broker/param/ArrayParamBrokerTest.php | 121 ++++---- .../php/broker/param/BoolParamBrokerTest.php | 25 +- .../param/CustomDatespanParamBrokerTest.php | 236 ++++++--------- .../php/broker/param/DateParamBrokerTest.php | 80 +++-- .../broker/param/DatespanParamBrokerTest.php | 82 +++-- .../php/broker/param/DayParamBrokerTest.php | 83 +++-- .../php/broker/param/FloatParamBrokerTest.php | 81 +++-- .../broker/param/HttpUriParamBrokerTest.php | 64 ++-- .../broker/param/IntegerParamBrokerTest.php | 81 +++-- .../php/broker/param/JsonParamBrokerTest.php | 120 ++++---- .../php/broker/param/MailParamBrokerTest.php | 24 +- .../php/broker/param/MonthParamBrokerTest.php | 82 +++-- .../MultipleSourceParamBrokerTestBase.php | 23 +- .../php/broker/param/OneOfParamBrokerTest.php | 268 ++++++++-------- .../broker/param/PasswordParamBrokerTest.php | 158 ++++------ .../broker/param/SecretParamBrokerTest.php | 151 ++++----- .../broker/param/StringParamBrokerTest.php | 85 +++--- .../php/broker/param/TextParamBrokerTest.php | 108 +++---- .../php/broker/param/WeekParamBrokerTest.php | 81 +++-- src/test/php/errors/ParamErrorTest.php | 98 +++--- src/test/php/errors/ParamErrorsTest.php | 85 ++---- .../errors/messages/LocalizedMessageTest.php | 58 ++-- .../PropertyBasedParamErrorMessagesTest.php | 202 ++++++------- src/test/php/filter/AcceptFilterTest.php | 27 +- src/test/php/filter/ArrayFilterTest.php | 57 ++-- src/test/php/filter/BoolFilterTest.php | 64 ++-- src/test/php/filter/DateFilterTest.php | 78 ++--- src/test/php/filter/DatespanFilterTest.php | 95 ++---- src/test/php/filter/DayFilterTest.php | 71 ++--- .../php/filter/EmptyValuesDataProvider.php | 23 ++ .../php/filter/ExistingHttpUriFilterTest.php | 80 ++--- src/test/php/filter/FilterTestBase.php | 3 - src/test/php/filter/FloatFilterTest.php | 78 ++--- src/test/php/filter/HttpUriFilterTest.php | 78 ++--- src/test/php/filter/IntegerFilterTest.php | 62 ++-- src/test/php/filter/JsonFilterTest.php | 124 +++----- src/test/php/filter/MailFilterTest.php | 121 +++----- src/test/php/filter/MonthFilterTest.php | 96 ++---- src/test/php/filter/PasswordFilterTest.php | 112 +++---- src/test/php/filter/RangeFilterTest.php | 66 ++-- src/test/php/filter/SecretFilterTest.php | 70 ++--- .../php/filter/SimplePasswordCheckerTest.php | 58 ++-- src/test/php/filter/StringFilterTest.php | 71 ++--- src/test/php/filter/TextFilterTest.php | 99 +++--- src/test/php/filter/WeekFilterTest.php | 27 +- src/test/php/filter/range/DateRangeTest.php | 115 +++---- .../php/filter/range/DatespanRangeTest.php | 131 ++++---- src/test/php/filter/range/NumberRangeTest.php | 114 +++---- .../php/filter/range/StringLengthTest.php | 175 +++++------ 56 files changed, 2102 insertions(+), 3114 deletions(-) create mode 100644 src/test/php/filter/EmptyValuesDataProvider.php diff --git a/src/test/php/ParamRequestTest.php b/src/test/php/ParamRequestTest.php index 6523c25..c4b4b89 100644 --- a/src/test/php/ParamRequestTest.php +++ b/src/test/php/ParamRequestTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use stubbles\input\errors\ParamErrors; @@ -19,17 +22,11 @@ }; /** * Tests for stubbles\input\ParamRequest. - * - * @group core */ +#[Group('core')] class ParamRequestTest extends TestCase { - /** - * instance to test - * - * @var ParamRequest - */ - private $paramRequest; + private ParamRequest $paramRequest; protected function setUp(): void { @@ -43,85 +40,69 @@ public function method(): string }; } - /** - * @test - */ + #[Test] public function returnsListOfParamNames(): void { assertThat( - $this->paramRequest->paramNames(), - equals(['foo', 'roland']) + $this->paramRequest->paramNames(), + equals(['foo', 'roland']) ); } - /** - * @test - */ + #[Test] public function returnsParamErrors(): void { assertThat( - $this->paramRequest->paramErrors(), - isInstanceOf(ParamErrors::class) + $this->paramRequest->paramErrors(), + isInstanceOf(ParamErrors::class) ); } - /** - * @test - */ + #[Test] public function returnsFalseOnCheckForNonExistingParam(): void { assertFalse($this->paramRequest->hasParam('baz')); } - /** - * @test - */ + #[Test] public function returnsTrueOnCheckForExistingParam(): void { assertTrue($this->paramRequest->hasParam('foo')); } - /** - * @test - */ + #[Test] public function validateParamReturnsValueValidator(): void { assertThat( - $this->paramRequest->validateParam('foo'), - isInstanceOf(ValueValidator::class) + $this->paramRequest->validateParam('foo'), + isInstanceOf(ValueValidator::class) ); } - /** - * @test - */ + #[Test] public function validateParamReturnsValueValidatorForNonExistingParam(): void { assertThat( - $this->paramRequest->validateParam('baz'), - isInstanceOf(ValueValidator::class) + $this->paramRequest->validateParam('baz'), + isInstanceOf(ValueValidator::class) ); } - /** - * @test - */ + #[Test] public function readParamReturnsValueReader(): void { assertThat( - $this->paramRequest->readParam('foo'), - isInstanceOf(ValueReader::class) + $this->paramRequest->readParam('foo'), + isInstanceOf(ValueReader::class) ); } - /** - * @test - */ + #[Test] public function readParamReturnsValueReaderForNonExistingParam(): void { assertThat( - $this->paramRequest->readParam('baz'), - isInstanceOf(ValueReader::class) + $this->paramRequest->readParam('baz'), + isInstanceOf(ValueReader::class) ); } } diff --git a/src/test/php/ParamsTest.php b/src/test/php/ParamsTest.php index db5ffee..dbc4ba2 100644 --- a/src/test/php/ParamsTest.php +++ b/src/test/php/ParamsTest.php @@ -7,13 +7,15 @@ * file that was distributed with this source code. */ namespace stubbles\input; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use stubbles\values\Value; use function bovigo\assert\{ assertThat, assertFalse, - assertNull, assertTrue, predicate\equals, predicate\isOfSize, @@ -21,82 +23,60 @@ }; /** * Tests for stubbles\input\Params. - * - * @group core */ +#[Group('core')] class ParamsTest extends TestCase { - /** - * instanct to test - * - * @var Params - */ - private $params; + private Params $params; protected function setUp(): void { $this->params = new Params(['foo' => 'bar', 'baz' => 'value']); } - /** - * @test - */ + #[Test] public function returnsFalseIfParamDoesNotExist(): void { assertFalse($this->params->contain('doesNotExist')); } - /** - * @test - */ + #[Test] public function returnsTrueIfParamDoesExist(): void { assertTrue($this->params->contain('foo')); } - /** - * @test - */ + #[Test] public function returnsNullValueIfParamDoesNotExist(): void { assertThat($this->params->value('doesNotExist'), isSameAs(Value::of(null))); } - /** - * @test - */ + #[Test] public function returnsValueIfParamExists(): void { assertThat($this->params->value('foo'), equals(Value::of('bar'))); } - /** - * @test - */ + #[Test] public function returnsListOfParamNames(): void { assertThat($this->params->names(), equals(['foo', 'baz'])); } - /** - * @test - */ + #[Test] public function listOfParamErrorsIsInitiallyEmpty(): void { assertFalse($this->params->errors()->exist()); } - /** - * @test - */ + #[Test] public function paramsCanBeCounted(): void { assertThat($this->params, isOfSize(2)); } - /** - * @test - */ + #[Test] public function canIterateOverParams(): void { $i = 0; diff --git a/src/test/php/ValueReaderTest.php b/src/test/php/ValueReaderTest.php index fec9493..182ffef 100644 --- a/src/test/php/ValueReaderTest.php +++ b/src/test/php/ValueReaderTest.php @@ -8,6 +8,8 @@ */ namespace stubbles\input; use bovigo\callmap\NewInstance; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\input\errors\ParamError; use stubbles\input\filter\FilterTestBase; use stubbles\values\Value; @@ -23,265 +25,218 @@ * Tests for stubbles\input\ValueFilter. * * @since 1.3.0 - * @group filter */ +#[Group('filter')] class ValueReaderTest extends FilterTestBase { - /** - * @test - */ + private const LOCALHOST_IP = '127.0.0.1'; + + #[Test] public function ifIsIpAddressReturnsValidatedValue(): void { assertThat( - $this->readParam('127.0.0.1')->ifIsIpAddress(), - equals('127.0.0.1') + $this->readParam(self::LOCALHOST_IP)->ifIsIpAddress(), + equals(self::LOCALHOST_IP) ); } - /** - * @test - */ + #[Test] public function ifIsIpAddressReturnsDefaultValueIfParamIsNull(): void { assertThat( - $this->readParam(null)->defaultingTo('127.0.0.1')->ifIsIpAddress(), - equals('127.0.0.1') + $this->readParam(null)->defaultingTo(self::LOCALHOST_IP)->ifIsIpAddress(), + equals(self::LOCALHOST_IP) ); } - /** - * @test - */ + #[Test] public function ifIsIpAddressReturnsNullIfValidationFails(): void { assertNull( - $this->readParam('invalid') - ->defaultingTo('127.0.0.1') - ->ifIsIpAddress() + $this->readParam('invalid') + ->defaultingTo(self::LOCALHOST_IP) + ->ifIsIpAddress() ); } - /** - * @test - */ + #[Test] public function ifIsIpAddressReturnsNullIfValidationFailsAndNoDefaultValueGiven(): void { assertNull($this->readParam('invalid')->ifIsIpAddress()); } - /** - * @test - */ + #[Test] public function ifIsIpAddressReturnsNullIfValidationFailsAndDefaultValueGiven(): void { assertNull($this->readParam('invalid')->required()->ifIsIpAddress()); } - /** - * @test - */ + #[Test] public function ifIsIpAddressAddsParamErrorIfValidationFails(): void { $this->readParam('invalid')->ifIsIpAddress(); assertTrue($this->paramErrors->existForWithId('bar', 'INVALID_IP_ADDRESS')); } - /** - * @test - */ + #[Test] public function ifIsIpAddressReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->ifIsIpAddress()); } - /** - * @test - */ + #[Test] public function ifIsIpAddressAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->ifIsIpAddress(); assertTrue($this->paramErrors->existForWithId('bar', 'FIELD_EMPTY')); } - /** - * @test - */ + #[Test] public function ifIsOneOfReturnsValidatedValue(): void { assertThat( - $this->readParam('Hardfloor')->ifIsOneOf(['Hardfloor', 'Dr DNA']), - equals('Hardfloor') + $this->readParam('Hardfloor')->ifIsOneOf(['Hardfloor', 'Dr DNA']), + equals('Hardfloor') ); } - /** - * @test - */ + #[Test] public function ifIsOneOfReturnsDefaultValueIfParamIsNull(): void { assertThat( - $this->readParam(null) - ->defaultingTo('Moby') - ->ifIsOneOf(['Hardfloor', 'Dr DNA']), - equals('Moby') + $this->readParam(null) + ->defaultingTo('Moby') + ->ifIsOneOf(['Hardfloor', 'Dr DNA']), + equals('Moby') ); } - /** - * @test - */ + #[Test] public function ifIsOneOfReturnsNullIfValidationFails(): void { assertNull( - $this->readParam('invalid') - ->defaultingTo('Moby') - ->ifIsOneOf(['Hardfloor', 'Dr DNA']) + $this->readParam('invalid') + ->defaultingTo('Moby') + ->ifIsOneOf(['Hardfloor', 'Dr DNA']) ); } - /** - * @test - */ + #[Test] public function ifIsOneOfReturnsNullIfValidationFailsAndNoDefaultValueGiven(): void { assertNull( - $this->readParam('invalid')->ifIsOneOf(['Hardfloor', 'Dr DNA']) + $this->readParam('invalid')->ifIsOneOf(['Hardfloor', 'Dr DNA']) ); } - /** - * @test - */ + #[Test] public function ifIsOneOfReturnsNullIfValidationFailsAndDefaultValueGiven(): void { assertNull( - $this->readParam('invalid') - ->required() - ->ifIsOneOf(['Hardfloor', 'Dr DNA']) + $this->readParam('invalid') + ->required() + ->ifIsOneOf(['Hardfloor', 'Dr DNA']) ); } - /** - * @test - */ + #[Test] public function ifIsOneOfAddsParamErrorIfValidationFails(): void { $this->readParam('invalid')->ifIsOneOf(['Hardfloor', 'Dr DNA']); assertTrue($this->paramErrors->existForWithId('bar', 'FIELD_NO_SELECT')); } - /** - * @test - */ + #[Test] public function ifIsOneOfReturnsNullIfParamIsNullAndRequired(): void { assertNull( - $this->readParam(null) - ->required() - ->ifIsOneOf(['Hardfloor', 'Dr DNA']) + $this->readParam(null) + ->required() + ->ifIsOneOf(['Hardfloor', 'Dr DNA']) ); } - /** - * @test - */ + #[Test] public function ifIsOneOfAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null) - ->required() - ->ifIsOneOf(['Hardfloor', 'Dr DNA']); + ->required() + ->ifIsOneOf(['Hardfloor', 'Dr DNA']); + assertTrue($this->paramErrors->existForWithId('bar', 'FIELD_EMPTY')); } - /** - * @test - */ + #[Test] public function ifMatchesReturnsValidatedValue(): void { assertThat( - $this->readParam('Hardfloor')->ifMatches('/[a-zA-Z]{9}/'), - equals('Hardfloor') + $this->readParam('Hardfloor')->ifMatches('/[a-zA-Z]{9}/'), + equals('Hardfloor') ); } - /** - * @test - */ + #[Test] public function ifMatchesReturnsDefaultValueIfParamIsNull(): void { assertThat( - $this->readParam(null) - ->defaultingTo('Moby') - ->ifMatches('/[a-zA-Z]{9}/'), - equals('Moby') + $this->readParam(null) + ->defaultingTo('Moby') + ->ifMatches('/[a-zA-Z]{9}/'), + equals('Moby') ); } - /** - * @test - */ + #[Test] public function ifMatchesReturnsNullIfValidationFails(): void { assertNull( - $this->readParam('invalid') - ->defaultingTo('Moby') - ->ifMatches('/[a-zA-Z]{9}/') + $this->readParam('invalid') + ->defaultingTo('Moby') + ->ifMatches('/[a-zA-Z]{9}/') ); } - /** - * @test - */ + #[Test] public function ifMatchesReturnsNullIfValidationFailsAndNoDefaultValueGiven(): void { assertNull( - $this->readParam('invalid')->ifMatches('/[a-zA-Z]{9}/') + $this->readParam('invalid')->ifMatches('/[a-zA-Z]{9}/') ); } - /** - * @test - */ + #[Test] public function ifMatchesReturnsNullIfValidationFailsAndDefaultValueGiven(): void { assertNull( - $this->readParam('invalid') - ->required() - ->ifMatches('/[a-zA-Z]{9}/') + $this->readParam('invalid') + ->required() + ->ifMatches('/[a-zA-Z]{9}/') ); } - /** - * @test - */ + #[Test] public function ifMatchesAddsParamErrorIfValidationFails(): void { $this->readParam('invalid')->ifMatches('/[a-zA-Z]{9}/'); assertTrue($this->paramErrors->existForWithId('bar', 'FIELD_WRONG_VALUE')); } - /** - * @test - */ + #[Test] public function ifMatchesReturnsNullIfParamIsNullAndRequired(): void { assertNull( - $this->readParam(null)->required()->ifMatches('/[a-zA-Z]{9}/') + $this->readParam(null)->required()->ifMatches('/[a-zA-Z]{9}/') ); } - /** - * @test - */ + #[Test] public function ifMatchesAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->ifMatches('/[a-zA-Z]{9}/'); assertTrue($this->paramErrors->existForWithId('bar', 'FIELD_EMPTY')); } - /** - * @test - */ + #[Test] public function ifMatchesAddsParamErrorWithDifferentErrorId(): void { $this->readParam(null)->required('OTHER')->ifMatches('/[a-zA-Z]{9}/'); @@ -289,9 +244,9 @@ public function ifMatchesAddsParamErrorWithDifferentErrorId(): void } /** - * @test * @since 3.0.0 */ + #[Test] public function withFilterReturnsNullIfParameterNotSet(): void { $value = Value::of(null); @@ -301,47 +256,41 @@ public function withFilterReturnsNullIfParameterNotSet(): void } /** - * @test * @since 3.0.0 */ + #[Test] public function withFilterReturnsDefaultValueIfParameterNotSet(): void { $value = Value::of(null); $filter = NewInstance::of(Filter::class); assertThat( - $this->read($value)->defaultingTo('foo')->withFilter($filter), - equals('foo') + $this->read($value)->defaultingTo('foo')->withFilter($filter), + equals('foo') ); verify($filter, 'apply')->wasNeverCalled(); } - /** - * @test - */ + #[Test] public function withFilterReturnsNullIfParamHasErrors(): void { $filter = NewInstance::of(Filter::class)->returns([ - 'apply' => [null, ['SOME_ERROR' => new ParamError('SOME_ERROR')]] + 'apply' => [null, ['SOME_ERROR' => new ParamError('SOME_ERROR')]] ]); assertNull($this->read(Value::of('foo'))->withFilter($filter)); } - /** - * @test - */ + #[Test] public function withFilterErrorListContainsParamError(): void { $value = Value::of('foo'); $filter = NewInstance::of(Filter::class)->returns([ - 'apply' => [null, ['SOME_ERROR' => new ParamError('SOME_ERROR')]] + 'apply' => [null, ['SOME_ERROR' => new ParamError('SOME_ERROR')]] ]); $this->read($value)->withFilter($filter); assertTrue($this->paramErrors->existForWithId('bar', 'SOME_ERROR')); } - /** - * @test - */ + #[Test] public function withFilterReturnsNullIfParamRequiredButNotSet(): void { $value = Value::of(null); @@ -350,9 +299,7 @@ public function withFilterReturnsNullIfParamRequiredButNotSet(): void verify($filter, 'apply')->wasNeverCalled(); } - /** - * @test - */ + #[Test] public function withFilterAddsRequiredErrorWhenRequiredAndParamNotSet(): void { $value = Value::of(null); @@ -362,9 +309,7 @@ public function withFilterAddsRequiredErrorWhenRequiredAndParamNotSet(): void verify($filter, 'apply')->wasNeverCalled(); } - /** - * @test - */ + #[Test] public function withFilterReturnsValueFromFilter(): void { $filter = NewInstance::of(Filter::class)->returns(['apply' => ['foo', []]]); @@ -373,55 +318,46 @@ public function withFilterReturnsValueFromFilter(): void /** * @since 2.0.0 - * @test */ + #[Test] public function canChangeRequiredParamErrorId(): void { $this->readParam(null) - ->required('OTHER') - ->withFilter(NewInstance::of(Filter::class)); + ->required('OTHER') + ->withFilter(NewInstance::of(Filter::class)); assertTrue($this->paramErrors->existForWithId('bar', 'OTHER')); } - /** - * @test - */ + #[Test] public function unsecureReturnsRawValue(): void { assertThat($this->readParam('a value')->unsecure(), equals('a value')); } - /** - * @test - */ + #[Test] public function canBeCreatedWithoutParam(): void { assertThat(ValueReader::forValue('bar'), isInstanceOf(ValueReader::class)); } - /** - * create a simple callable which filters a param value - * - * @return callable - */ private function createCallable(): callable { - return function(Value $value, array &$errors) - { - if ($value->value() == 303) { - return 'Roland TB-303'; - } + return function(Value $value, array &$errors): ?string + { + if ($value->value() == 303) { + return 'Roland TB-303'; + } - $errors['INVALID_303'] = []; - return null; - }; + $errors['INVALID_303'] = []; + return null; + }; } /** * @since 2.2.0 - * @group issue_33 - * @test */ + #[Test] + #[Group('issue_33')] public function withCallableReturnsFilteredValue(): void { assertThat( @@ -432,30 +368,30 @@ public function withCallableReturnsFilteredValue(): void /** * @since 3.0.0 - * @test */ + #[Test] public function withCallableReturnsNullWhenParamNotSet(): void { assertNull( - $this->readParam(null)->withCallable($this->createCallable()) + $this->readParam(null)->withCallable($this->createCallable()) ); } /** * @since 3.0.0 - * @test */ + #[Test] public function withCallableReturnsNullWhenParamNotSetAndRequired(): void { assertNull( - $this->readParam(null)->required()->withCallable($this->createCallable()) + $this->readParam(null)->required()->withCallable($this->createCallable()) ); } /** * @since 3.0.0 - * @test */ + #[Test] public function withCallableAddsErrorWhenParamNotSetAndRequired(): void { $this->readParam(null)->required()->withCallable($this->createCallable()); @@ -464,35 +400,35 @@ public function withCallableAddsErrorWhenParamNotSetAndRequired(): void /** * @since 3.0.0 - * @test */ + #[Test] public function withCallableReturnsDefaultValueWhenParamNotSet(): void { assertThat( - $this->readParam(null) - ->defaultingTo('Roland TB-303 w/ Hardfloor Mod') - ->withCallable($this->createCallable()), - equals('Roland TB-303 w/ Hardfloor Mod') + $this->readParam(null) + ->defaultingTo('Roland TB-303 w/ Hardfloor Mod') + ->withCallable($this->createCallable()), + equals('Roland TB-303 w/ Hardfloor Mod') ); } /** * @since 2.2.0 - * @group issue_33 - * @test */ + #[Test] + #[Group('issue_33')] public function withCallableReturnsNullOnError(): void { assertNull( - $this->readParam('909')->withCallable($this->createCallable()) + $this->readParam('909')->withCallable($this->createCallable()) ); } /** * @since 2.2.0 - * @group issue_33 - * @test */ + #[Test] + #[Group('issue_33')] public function withCallableAddsErrorsToErrorList(): void { $this->readParam('909')->withCallable($this->createCallable()); diff --git a/src/test/php/ValueValidatorTest.php b/src/test/php/ValueValidatorTest.php index 64ab998..34e7af3 100644 --- a/src/test/php/ValueValidatorTest.php +++ b/src/test/php/ValueValidatorTest.php @@ -7,8 +7,8 @@ * file that was distributed with this source code. */ namespace stubbles\input; -use bovigo\callmap\NewCallable; -use bovigo\callmap\NewInstance; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use stubbles\peer\http\HttpUri; use stubbles\values\Value; @@ -17,13 +17,12 @@ use function bovigo\assert\assertFalse; use function bovigo\assert\assertTrue; use function bovigo\assert\predicate\isInstanceOf; -use function bovigo\callmap\verify; /** * Tests for stubbles\input\ValueValidator. * * @since 1.3.0 - * @group validator */ +#[Group('validator')] class ValueValidatorTest extends TestCase { protected function tearDown(): void @@ -31,95 +30,73 @@ protected function tearDown(): void Value::defineCheck('isExistingHttpUri', [HttpUri::class, 'exists']); } - /** - * helper method to create test instances - * - * @param string $value - * @return ValueValidator - */ - private function validate($value): ValueValidator + private function validate(string $value): ValueValidator { return new ValueValidator(Value::of($value)); } - /** - * @test - */ + #[Test] public function containsReturnsTrueIfValidatorSatisfied(): void { assertTrue($this->validate('foo')->contains('o')); } - /** - * @test - */ + #[Test] public function containsReturnsFalseIfValidatorNotSatisfied(): void { assertFalse($this->validate('foo')->contains('u')); } /** - * @test * @since 4.3.0 */ + #[Test] public function containsAnyOfReturnsTrueIfValidatorSatisfied(): void { assertTrue($this->validate('foo')->containsAnyOf(['bar', 'o', 'baz'])); } /** - * @test * @since 4.3.0 */ + #[Test] public function containsAnyOfReturnsFalseIfValidatorNotSatisfied(): void { assertFalse($this->validate('foo')->containsAnyOf(['bar', 'baz'])); } - /** - * @test - */ + #[Test] public function isEqualToReturnsTrueIfValidatorSatisfied(): void { assertTrue($this->validate('foo')->isEqualTo('foo')); } - /** - * @test - */ + #[Test] public function isEqualToReturnsFalseIfValidatorNotSatisfied(): void { assertFalse($this->validate('foo')->isEqualTo('bar')); } - /** - * @test - */ + #[Test] public function isHttpUriReturnsTrueIfValidatorSatisfied(): void { assertTrue($this->validate('http://example.net/')->isHttpUri()); } - /** - * @test - */ + #[Test] public function isHttpUriReturnsFalseIfValidatorNotSatisfied(): void { assertFalse($this->validate('foo')->isHttpUri()); } - /** - * @test - */ + #[Test] public function isExistingHttpUriReturnsTrueIfValidatorSatisfied(): void { Value::defineCheck('isExistingHttpUri', function(): bool { return true; }); assertTrue($this->validate('http://localhost/')->isExistingHttpUri()); } - /** - * @test - */ + #[Test] public function isExistingHttpUriReturnsFalseIfValidatorNotSatisfied(): void { Value::defineCheck('isExistingHttpUri', function(): bool { return false; }); @@ -127,164 +104,148 @@ public function isExistingHttpUriReturnsFalseIfValidatorNotSatisfied(): void } /** - * @test * @since 1.7.0 - * @group bug258 */ + #[Test] + #[Group('bug258')] public function isIpAddressReturnsTrueIfValidatorSatisfiedWithIpV4Address(): void { assertTrue($this->validate('127.0.0.1')->isIpAddress()); } /** - * @test * @since 1.7.0 - * @group bug258 */ + #[Test] + #[Group('bug258')] public function isIpAddressReturnsTrueIfValidatorSatisfiedWithIpV6Address(): void { assertTrue( - $this->validate('2001:8d8f:1fe:5:abba:dbff:fefe:7755') - ->isIpAddress() + $this->validate('2001:8d8f:1fe:5:abba:dbff:fefe:7755') + ->isIpAddress() ); } - /** - * @test - */ + #[Test] public function isIpAddressReturnsFalseIfValidatorNotSatisfied(): void { assertFalse($this->validate('foo')->isIpAddress()); } /** - * @test * @since 1.7.0 - * @group bug258 */ + #[Test] + #[Group('bug258')] public function isIpV4AddressReturnsTrueIfValidatorSatisfied(): void { assertTrue($this->validate('127.0.0.1')->isIpV4Address()); } /** - * @test * @since 1.7.0 - * @group bug258 */ + #[Test] + #[Group('bug258')] public function isIpV4AddressReturnsFalseIfValidatorNotSatisfied(): void { assertFalse($this->validate('foo')->isIpV4Address()); } /** - * @test * @since 1.7.0 - * @group bug258 */ + #[Test] + #[Group('bug258')] public function isIpV4AddressReturnsFalseForIpV6Addresses(): void { assertFalse( - $this->validate('2001:8d8f:1fe:5:abba:dbff:fefe:7755') - ->isIpV4Address() + $this->validate('2001:8d8f:1fe:5:abba:dbff:fefe:7755') + ->isIpV4Address() ); } /** - * @test * @since 1.7.0 - * @group bug258 */ + #[Test] + #[Group('bug258')] public function isIpV6AddressReturnsTrueIfValidatorSatisfied(): void { assertTrue( - $this->validate('2001:8d8f:1fe:5:abba:dbff:fefe:7755') - ->isIpV6Address() + $this->validate('2001:8d8f:1fe:5:abba:dbff:fefe:7755') + ->isIpV6Address() ); } /** - * @test * @since 1.7.0 - * @group bug258 */ + #[Test] + #[Group('bug258')] public function isIpV6AddressReturnsFalseIfValidatorNotSatisfied(): void { assertFalse($this->validate('foo')->isIpV6Address()); } /** - * @test * @since 1.7.0 - * @group bug258 */ + #[Test] + #[Group('bug258')] public function isIpV6AddressReturnsFalseForIpV4Addresses(): void { assertFalse($this->validate('127.0.0.1')->isIpV6Address()); } - /** - * @test - */ + #[Test] public function isMailAddressReturnsTrueIfValidatorSatisfied(): void { assertTrue($this->validate('mail@example.net')->isMailAddress()); } - /** - * @test - */ + #[Test] public function isMailAddressReturnsFalseIfValidatorNotSatisfied(): void { assertFalse($this->validate('foo')->isMailAddress()); } - /** - * @test - */ + #[Test] public function isOneOfReturnsTrueIfValidatorSatisfied(): void { assertTrue($this->validate('foo')->isOneOf(['foo', 'bar', 'baz'])); } - /** - * @test - */ + #[Test] public function isOneOfReturnsFalseIfValidatorNotSatisfied(): void { assertFalse($this->validate('foo')->isOneOf(['bar', 'baz'])); } - /** - * @test - */ + #[Test] public function matchesReturnsTrueIfPatternMatchesValue(): void { assertTrue($this->validate('foo')->matches('/foo/')); } - /** - * @test - */ + #[Test] public function matchesReturnsFalseIfPatternDoesNotMatchValue(): void { assertFalse($this->validate('foo')->matches('/bar/')); } /** - * @test * @since 3.0.0 */ + #[Test] public function withPredicateReturnsPredicateResult(): void { assertTrue($this->validate('foo')->with( - function(Value $value) { return 'foo' === $value->value(); } + fn(Value $value): bool => 'foo' === $value->value() )); } - /** - * @test - */ + #[Test] public function canBeCreatedAsMock(): void { assertThat( diff --git a/src/test/php/broker/ParamBrokersTest.php b/src/test/php/broker/ParamBrokersTest.php index 7190923..0332f7a 100644 --- a/src/test/php/broker/ParamBrokersTest.php +++ b/src/test/php/broker/ParamBrokersTest.php @@ -8,7 +8,12 @@ */ namespace stubbles\input\broker; use bovigo\callmap\NewInstance; +use Generator; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +use RuntimeException; use stubbles\input\broker\param\ParamBroker; use function bovigo\assert\assertThat; @@ -19,118 +24,97 @@ use function stubbles\reflect\annotationsOfConstructor; /** * Tests for stubbles\input\broker\RequestBroker. - * - * @group broker - * @group broker_core */ +#[Group('broker')] +#[Group('broker_core')] class ParamBrokersTest extends TestCase { - /** - * @var \stubbles\input\broker\RequestBroker - */ - private $requestBroker; + private RequestBroker $requestBroker; protected function setUp(): void { $this->requestBroker = new RequestBroker(); } - /** - * @test - */ + #[Test] public function annotationsPresentOnAddParamBrokersMethod(): void { assertTrue( - annotationsOfConstructor($this->requestBroker)->contain('Map') + annotationsOfConstructor($this->requestBroker)->contain('Map') ); } - /** - * @return array - */ - public static function defaultBrokerList(): array + public static function defaultBrokerList(): Generator { - $defaultBroker = []; foreach (RequestBroker::buildInTypes() as $name => $paramBroker) { - $defaultBroker[] = [$name, get_class($paramBroker)]; + yield [$name, get_class($paramBroker)]; } - - return $defaultBroker; } - /** - * @test - * @dataProvider defaultBrokerList - */ + #[Test] + #[DataProvider('defaultBrokerList')] public function returnsBroker(string $key, string $brokerClass): void { assertThat( - $this->requestBroker->paramBroker($key), - isInstanceOf($brokerClass) + $this->requestBroker->paramBroker($key), + isInstanceOf($brokerClass) ); } /** - * @test - * @dataProvider defaultBrokerList * @since 2.3.3 - * @group issue_45 */ + #[Test] + #[DataProvider('defaultBrokerList')] + #[Group('issue_45')] public function returnsBrokerWithLowerCaseKey(string $key, string $brokerClass): void { assertThat( - $this->requestBroker->paramBroker(strtolower($key)), - isInstanceOf($brokerClass) + $this->requestBroker->paramBroker(strtolower($key)), + isInstanceOf($brokerClass) ); } - /** - * @test - */ + #[Test] public function requestUnknownParamBrokerTypeThrowsRuntimeException(): void { - expect(function() { - $this->requestBroker->paramBroker('doesNotExist'); - })->throws(\RuntimeException::class); + expect(fn() => $this->requestBroker->paramBroker('doesNotExist')) + ->throws(RuntimeException::class); } - /** - * @test - * @dataProvider defaultBrokerList - */ - public function addingBrokersDoesNotOverrideDefaultBrokers(string $key, string $brokerClass): void - { + #[Test] + #[DataProvider('defaultBrokerList')] + public function addingBrokersDoesNotOverrideDefaultBrokers( + string $key, + string $brokerClass + ): void { $paramBroker = NewInstance::of(ParamBroker::class); $requestBroker = new RequestBroker(['mock' => $paramBroker]); assertThat( - $requestBroker->paramBroker($key), - isInstanceOf($brokerClass) + $requestBroker->paramBroker($key), + isInstanceOf($brokerClass) ); } - /** - * @test - */ + #[Test] public function returnsAddedBroker(): void { $paramBroker = NewInstance::of(ParamBroker::class); $requestBroker = new RequestBroker(['Mock' => $paramBroker]); assertThat( - $requestBroker->paramBroker('mock'), - isSameAs($paramBroker) + $requestBroker->paramBroker('mock'), + isSameAs($paramBroker) ); } - /** - * @test - */ + #[Test] public function canOverwriteDefaultBroker(): void { $paramBroker = NewInstance::of(ParamBroker::class); $requestBroker = new RequestBroker(['string' => $paramBroker]); assertThat( - $requestBroker->paramBroker('string'), - isSameAs($paramBroker) + $requestBroker->paramBroker('string'), + isSameAs($paramBroker) ); } } diff --git a/src/test/php/broker/RequestBrokerTargetMethodsOfTest.php b/src/test/php/broker/RequestBrokerTargetMethodsOfTest.php index 16b2f4f..a6fef2a 100644 --- a/src/test/php/broker/RequestBrokerTargetMethodsOfTest.php +++ b/src/test/php/broker/RequestBrokerTargetMethodsOfTest.php @@ -7,17 +7,21 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker; + +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +use ReflectionClass; use function bovigo\assert\assertThat; use function bovigo\assert\predicate\equals; use function stubbles\reflect\reflect; /** * Tests for stubbles\input\broker\RequestBroker::targetMethodsOf(). - * - * @group broker - * @group broker_core */ +#[Group('broker')] +#[Group('broker_core')] class RequestBrokerTargetMethodsOfTest extends TestCase { /** @@ -33,31 +37,31 @@ public static function allowedValues(): array } /** - * @param object|class-string|\ReflectionClass $allowedValue - * @test - * @dataProvider allowedValues + * @param object|class-string|ReflectionClass $allowedValue */ - public function returnsListOfAllMethodsWithRequestAnnotation($allowedValue): void - { - $paramNames = []; - foreach (RequestBroker::targetMethodsOf($allowedValue) as $targetMethod) { - $paramNames[] = $targetMethod->paramName(); - } + #[Test] + #[DataProvider('allowedValues')] + public function returnsListOfAllMethodsWithRequestAnnotation( + string|object $allowedValue + ): void { + $paramNames = RequestBroker::targetMethodsOf($allowedValue) + ->map(fn(TargetMethod $targetMethod): string => $targetMethod->paramName()) + ->values(); assertThat($paramNames, equals(['verbose', 'bar', 'baz'])); } /** - * @param object|class-string|\ReflectionClass $allowedValue - * @test - * @dataProvider allowedValues + * @param object|class-string|ReflectionClass $allowedValue */ - public function returnsListOfAllMethodsWithRequestAnnotationInGivenGroup($allowedValue): void - { - $paramNames = []; - foreach (RequestBroker::targetMethodsOf($allowedValue, 'main') as $targetMethod) { - $paramNames[] = $targetMethod->paramName(); - } + #[Test] + #[DataProvider('allowedValues')] + public function returnsListOfAllMethodsWithRequestAnnotationInGivenGroup( + string|object $allowedValue + ): void { + $paramNames = RequestBroker::targetMethodsOf($allowedValue, 'main') + ->map(fn(TargetMethod $targetMethod): string => $targetMethod->paramName()) + ->values(); assertThat($paramNames, equals(['bar'])); } diff --git a/src/test/php/broker/RequestBrokerTest.php b/src/test/php/broker/RequestBrokerTest.php index fa5851f..f995da2 100644 --- a/src/test/php/broker/RequestBrokerTest.php +++ b/src/test/php/broker/RequestBrokerTest.php @@ -7,7 +7,11 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker; + +use bovigo\callmap\ClassProxy; use bovigo\callmap\NewInstance; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use stubbles\input\Request; use stubbles\input\ValueReader; @@ -17,26 +21,18 @@ use function bovigo\assert\assertFalse; use function bovigo\assert\assertNull; use function bovigo\assert\assertTrue; -use function bovigo\assert\expect; use function bovigo\assert\predicate\equals; use function bovigo\callmap\onConsecutiveCalls; use function stubbles\reflect\annotationsOf; /** * Tests for stubbles\input\broker\RequestBroker. - * - * @group broker - * @group broker_core */ +#[Group('broker')] +#[Group('broker_core')] class RequestBrokerTest extends TestCase { - /** - * @var \stubbles\input\broker\RequestBroker - */ - private $requestBroker; - /** - * @var Request&\bovigo\callmap\ClassProxy - */ - private $request; + private RequestBroker $requestBroker; + private Request&ClassProxy $request; protected function setUp(): void { @@ -44,50 +40,45 @@ protected function setUp(): void $this->request = NewInstance::of(Request::class); } - /** - * @test - */ + #[Test] public function annotationsPresentOnClass(): void { assertTrue( - annotationsOf($this->requestBroker)->contain('Singleton') + annotationsOf($this->requestBroker)->contain('Singleton') ); } - /** - * @test - */ + #[Test] public function procuresOnlyThoseInGivenGroup(): void { $this->request->returns( - ['readParam' => ValueReader::forValue('just some string value')] + ['readParam' => ValueReader::forValue('just some string value')] ); $object = new BrokerClass(); $this->requestBroker->procure($this->request, $object, 'main'); + assertFalse($object->isVerbose()); assertThat($object->getBar(), equals('just some string value')); assertNull($object->getBaz()); } - /** - * @test - */ + #[Test] public function procuresAllIfNoGroupGiven(): void { $paramBroker = NewInstance::of(ParamBroker::class) - ->returns(['procure' => 'just another string value']); - $this->request->returns( - ['readParam' => onConsecutiveCalls( - ValueReader::forValue('on'), - ValueReader::forValue('just some string value'), - ValueReader::forValue('just another string value') - ) - ] - ); + ->returns(['procure' => 'just another string value']); + $this->request->returns([ + 'readParam' => onConsecutiveCalls( + ValueReader::forValue('on'), + ValueReader::forValue('just some string value'), + ValueReader::forValue('just another string value') + ) + ]); $requestBroker = new RequestBroker(['Mock' => $paramBroker]); $object = new BrokerClass(); $requestBroker->procure($this->request, $object); + assertTrue($object->isVerbose()); assertThat($object->getBar(), equals('just some string value')); assertThat($object->getBaz(), equals('just another string value')); diff --git a/src/test/php/broker/param/ArrayParamBrokerTest.php b/src/test/php/broker/param/ArrayParamBrokerTest.php index 98764bd..e501db6 100644 --- a/src/test/php/broker/param/ArrayParamBrokerTest.php +++ b/src/test/php/broker/param/ArrayParamBrokerTest.php @@ -8,6 +8,8 @@ */ namespace stubbles\input\broker\param; use bovigo\callmap\NewInstance; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\input\ValueReader; use function bovigo\assert\assertThat; @@ -16,10 +18,9 @@ use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\broker\param\ArrayParamBroker. - * - * @group broker - * @group broker_param */ +#[Group('broker')] +#[Group('broker_param')] class ArrayParamBrokerTest extends MultipleSourceParamBrokerTestBase { protected function setUp(): void @@ -29,8 +30,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -47,119 +46,103 @@ protected function expectedValue(): array return ['foo', 'bar']; } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSet(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['default' => 'foo|bar']) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['default' => 'foo|bar']) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function returnsValueWithDifferentSeparator(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('foo|bar'), - $this->createRequestAnnotation(['separator' => '|']) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $this->createRequest('foo|bar'), + $this->createRequestAnnotation(['separator' => '|']) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => true]) + ) ); } - /** - * @test - */ + #[Test] public function returnsEmptyArrayForEmptyValue(): void { assertEmptyArray( - $this->paramBroker->procure( - $this->createRequest(''), - $this->createRequestAnnotation([]) - ) + $this->paramBroker->procure( + $this->createRequest(''), + $this->createRequestAnnotation([]) + ) ); } - /** - * @test - */ + #[Test] public function usesParamAsDefaultSource(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('foo, bar'), - $this->createRequestAnnotation([]) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $this->createRequest('foo, bar'), + $this->createRequestAnnotation([]) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function usesParamAsSource(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('foo, bar'), - $this->createRequestAnnotation(['source' => 'param']) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $this->createRequest('foo, bar'), + $this->createRequestAnnotation(['source' => 'param']) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function canUseHeaderAsSourceForWebRequest(): void { $request = NewInstance::of(WebRequest::class)->returns([ - 'readHeader' => ValueReader::forValue('foo, bar') + 'readHeader' => ValueReader::forValue('foo, bar') ]); assertThat( - $this->paramBroker->procure( - $request, - $this->createRequestAnnotation(['source' => 'header']) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $request, + $this->createRequestAnnotation(['source' => 'header']) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function canUseCookieAsSourceForWebRequest(): void { $request = NewInstance::of(WebRequest::class)->returns([ - 'readCookie' => ValueReader::forValue('foo, bar') + 'readCookie' => ValueReader::forValue('foo, bar') ]); assertThat( - $this->paramBroker->procure( - $request, - $this->createRequestAnnotation(['source' => 'cookie']) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $request, + $this->createRequestAnnotation(['source' => 'cookie']) + ), + equals($this->expectedValue()) ); } } diff --git a/src/test/php/broker/param/BoolParamBrokerTest.php b/src/test/php/broker/param/BoolParamBrokerTest.php index b53f0d4..17bca2d 100644 --- a/src/test/php/broker/param/BoolParamBrokerTest.php +++ b/src/test/php/broker/param/BoolParamBrokerTest.php @@ -7,13 +7,16 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker\param; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; + use function bovigo\assert\assertTrue; /** * Tests for stubbles\input\broker\param\BoolParamBroker. - * - * @group broker - * @group broker_param */ +#[Group('broker')] +#[Group('broker_param')] class BoolParamBrokerTest extends MultipleSourceParamBrokerTestBase { protected function setUp(): void @@ -23,8 +26,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -33,24 +34,20 @@ protected function getRequestAnnotationName(): string /** * returns expected filtered value - * - * @return bool */ protected function expectedValue(): bool { return true; } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSet(): void { assertTrue( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['default' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['default' => true]) + ) ); } } diff --git a/src/test/php/broker/param/CustomDatespanParamBrokerTest.php b/src/test/php/broker/param/CustomDatespanParamBrokerTest.php index 966f791..db9711b 100644 --- a/src/test/php/broker/param/CustomDatespanParamBrokerTest.php +++ b/src/test/php/broker/param/CustomDatespanParamBrokerTest.php @@ -8,6 +8,8 @@ */ namespace stubbles\input\broker\param; use bovigo\callmap\NewInstance; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use stubbles\date\span\CustomDatespan; use stubbles\input\Request; @@ -20,16 +22,12 @@ use function bovigo\callmap\onConsecutiveCalls; /** * Tests for stubbles\input\broker\param\CustomDatespanParamBroker. - * - * @group broker - * @group broker_param */ +#[Group('broker')] +#[Group('broker_param')] class CustomDatespanParamBrokerTest extends TestCase { - /** - * @var CustomDatespanParamBroker - */ - private $customDatespanParamBroker; + private CustomDatespanParamBroker $customDatespanParamBroker; protected function setUp(): void { @@ -39,8 +37,7 @@ protected function setUp(): void /** * creates filter annotation * - * @param array $values - * @return Annotation + * @param array $values */ private function createRequestAnnotation(array $values = []): Annotation { @@ -52,223 +49,190 @@ private function createRequestAnnotation(array $values = []): Annotation private function createRequest(?string $startValue, ?string $endValue): Request { return NewInstance::of(Request::class) - ->returns(['readParam' => onConsecutiveCalls( - ValueReader::forValue($startValue), - ValueReader::forValue($endValue) - )] + ->returns(['readParam' => onConsecutiveCalls( + ValueReader::forValue($startValue), + ValueReader::forValue($endValue) + )] ); } - /** - * @test - */ + #[Test] public function returnsDatespan(): void { assertThat( - $this->customDatespanParamBroker->procure( - $this->createRequest('2012-02-05', '2012-04-21'), - $this->createRequestAnnotation([]) - ), - equals(new CustomDatespan('2012-02-05', '2012-04-21')) + $this->customDatespanParamBroker->procure( + $this->createRequest('2012-02-05', '2012-04-21'), + $this->createRequestAnnotation([]) + ), + equals(new CustomDatespan('2012-02-05', '2012-04-21')) ); } - /** - * @test - */ + #[Test] public function returnsNullIfStartDateInvalid(): void { assertNull( - $this->customDatespanParamBroker->procure( - $this->createRequest('invalid', '2012-04-21'), - $this->createRequestAnnotation(['required' => 'true']) - ) + $this->customDatespanParamBroker->procure( + $this->createRequest('invalid', '2012-04-21'), + $this->createRequestAnnotation(['required' => 'true']) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfEndDateInvalid(): void { assertNull( - $this->customDatespanParamBroker->procure( - $this->createRequest('2012-02-05', 'invalid'), - $this->createRequestAnnotation(['required' => 'true']) - ) + $this->customDatespanParamBroker->procure( + $this->createRequest('2012-02-05', 'invalid'), + $this->createRequestAnnotation(['required' => 'true']) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfStartDateIsMissing(): void { assertNull( - $this->customDatespanParamBroker->procure( - $this->createRequest(null, '2012-04-21'), - $this->createRequestAnnotation() - ) + $this->customDatespanParamBroker->procure( + $this->createRequest(null, '2012-04-21'), + $this->createRequestAnnotation() + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfEndDateIsMissing(): void { assertNull( - $this->customDatespanParamBroker->procure( - $this->createRequest('2012-02-05', null), - $this->createRequestAnnotation() - ) + $this->customDatespanParamBroker->procure( + $this->createRequest('2012-02-05', null), + $this->createRequestAnnotation() + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfBothDatesAreMissing(): void { assertNull( - $this->customDatespanParamBroker->procure( - $this->createRequest(null, null), - $this->createRequestAnnotation() - ) + $this->customDatespanParamBroker->procure( + $this->createRequest(null, null), + $this->createRequestAnnotation() + ) ); } - /** - * @test - */ + #[Test] public function returnsDefaultStartDateIfStartDateIsMissingAndDefaultGiven(): void { assertThat( - $this->customDatespanParamBroker->procure( - $this->createRequest(null, '2012-04-21'), - $this->createRequestAnnotation(['defaultStart' => 'today']) - ), - equals(new CustomDatespan('today', '2012-04-21')) + $this->customDatespanParamBroker->procure( + $this->createRequest(null, '2012-04-21'), + $this->createRequestAnnotation(['defaultStart' => 'today']) + ), + equals(new CustomDatespan('today', '2012-04-21')) ); } - /** - * @test - */ + #[Test] public function returnsDefaultEndDateIfEndDateIsMissingAndDefaultGiven(): void { assertThat( - $this->customDatespanParamBroker->procure( - $this->createRequest('2012-02-05', null), - $this->createRequestAnnotation(['defaultEnd' => 'today']) - ), - equals(new CustomDatespan('2012-02-05', 'today')) + $this->customDatespanParamBroker->procure( + $this->createRequest('2012-02-05', null), + $this->createRequestAnnotation(['defaultEnd' => 'today']) + ), + equals(new CustomDatespan('2012-02-05', 'today')) ); } - /** - * @test - */ + #[Test] public function returnsDefaultIfBothDatesAreMissingAndDefaultGiven(): void { assertThat( - $this->customDatespanParamBroker->procure( - $this->createRequest(null, null), - $this->createRequestAnnotation( - ['defaultStart' => 'yesterday', - 'defaultEnd' => 'tomorrow' - ] - ) - ), - equals(new CustomDatespan('yesterday', 'tomorrow')) + $this->customDatespanParamBroker->procure( + $this->createRequest(null, null), + $this->createRequestAnnotation([ + 'defaultStart' => 'yesterday', + 'defaultEnd' => 'tomorrow' + ]) + ), + equals(new CustomDatespan('yesterday', 'tomorrow')) ); } - /** - * @test - */ + #[Test] public function returnsNullIfBeforeMinStartDate(): void { assertNull( - $this->customDatespanParamBroker->procure( - $this->createRequest('yesterday', 'today'), - $this->createRequestAnnotation(['minStartDate' => 'today']) - ) + $this->customDatespanParamBroker->procure( + $this->createRequest('yesterday', 'today'), + $this->createRequestAnnotation(['minStartDate' => 'today']) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfAfterMaxStartDate(): void { assertNull( - $this->customDatespanParamBroker->procure( - $this->createRequest('today', 'tomorrow'), - $this->createRequestAnnotation(['maxStartDate' => 'yesterday']) - ) + $this->customDatespanParamBroker->procure( + $this->createRequest('today', 'tomorrow'), + $this->createRequestAnnotation(['maxStartDate' => 'yesterday']) + ) ); } - /** - * @test - */ + #[Test] public function returnsValueIfStartInRange(): void { assertThat( - $this->customDatespanParamBroker->procure( - $this->createRequest('today', 'tomorrow'), - $this->createRequestAnnotation( - ['minStartDate' => 'yesterday', - 'maxStartDate' => 'tomorrow' - ] - ) - ), - equals(new CustomDatespan('today', 'tomorrow')) + $this->customDatespanParamBroker->procure( + $this->createRequest('today', 'tomorrow'), + $this->createRequestAnnotation([ + 'minStartDate' => 'yesterday', + 'maxStartDate' => 'tomorrow' + ]) + ), + equals(new CustomDatespan('today', 'tomorrow')) ); } - /** - * @test - */ + #[Test] public function returnsNullIfBeforeMinEndDate(): void { assertNull( - $this->customDatespanParamBroker->procure( - $this->createRequest('yesterday', 'yesterday'), - $this->createRequestAnnotation(['minEndDate' => 'today']) - ) + $this->customDatespanParamBroker->procure( + $this->createRequest('yesterday', 'yesterday'), + $this->createRequestAnnotation(['minEndDate' => 'today']) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfAfterMaxEndDate(): void { assertNull( - $this->customDatespanParamBroker->procure( - $this->createRequest('yesterday', 'today'), - $this->createRequestAnnotation(['maxEndDate' => 'yesterday']) - ) + $this->customDatespanParamBroker->procure( + $this->createRequest('yesterday', 'today'), + $this->createRequestAnnotation(['maxEndDate' => 'yesterday']) + ) ); } - /** - * @test - */ + #[Test] public function returnsValueIfEndInRange(): void { assertThat( - $this->customDatespanParamBroker->procure( - $this->createRequest('yesterday', 'today'), - $this->createRequestAnnotation( - ['minEndDate' => 'yesterday', - 'maxEndDate' => 'tomorrow' - ] - ) - ), - equals(new CustomDatespan('yesterday', 'today')) + $this->customDatespanParamBroker->procure( + $this->createRequest('yesterday', 'today'), + $this->createRequestAnnotation([ + 'minEndDate' => 'yesterday', + 'maxEndDate' => 'tomorrow' + ]) + ), + equals(new CustomDatespan('yesterday', 'today')) ); } } diff --git a/src/test/php/broker/param/DateParamBrokerTest.php b/src/test/php/broker/param/DateParamBrokerTest.php index 8d2b466..e6360e9 100644 --- a/src/test/php/broker/param/DateParamBrokerTest.php +++ b/src/test/php/broker/param/DateParamBrokerTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker\param; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\date\Date; use function bovigo\assert\assertThat; @@ -14,10 +17,9 @@ use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\broker\param\DateParamBroker. - * - * @group broker - * @group broker_param */ +#[Group('broker')] +#[Group('broker_param')] class DateParamBrokerTest extends MultipleSourceParamBrokerTestBase { protected function setUp(): void @@ -27,8 +29,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -37,80 +37,68 @@ protected function getRequestAnnotationName(): string /** * returns expected filtered value - * - * @return Date */ protected function expectedValue(): Date { return new Date('2012-04-21 00:00:00+02:00'); } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSet(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['default' => '2012-04-21']) - ), - equals(new Date('2012-04-21')) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['default' => '2012-04-21']) + ), + equals(new Date('2012-04-21')) ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => true]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfBeforeMinDate(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('yesterday'), - $this->createRequestAnnotation(['minDate' => 'today']) - ) + $this->paramBroker->procure( + $this->createRequest('yesterday'), + $this->createRequestAnnotation(['minDate' => 'today']) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfAfterMaxDate(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('today'), - $this->createRequestAnnotation(['maxDate' => 'yesterday']) - ) + $this->paramBroker->procure( + $this->createRequest('today'), + $this->createRequestAnnotation(['maxDate' => 'yesterday']) + ) ); } - /** - * @test - */ + #[Test] public function returnsValueIfInRange(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('today'), - $this->createRequestAnnotation( - ['minDate' => 'yesterday', 'maxDate' => 'tomorrow'] - ) - ), - equals(new Date('today')) + $this->paramBroker->procure( + $this->createRequest('today'), + $this->createRequestAnnotation( + ['minDate' => 'yesterday', 'maxDate' => 'tomorrow'] + ) + ), + equals(new Date('today')) ); } } diff --git a/src/test/php/broker/param/DatespanParamBrokerTest.php b/src/test/php/broker/param/DatespanParamBrokerTest.php index cea7b1d..7c3a93e 100644 --- a/src/test/php/broker/param/DatespanParamBrokerTest.php +++ b/src/test/php/broker/param/DatespanParamBrokerTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker\param; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\date\span\Day; use function bovigo\assert\assertThat; @@ -15,10 +18,10 @@ /** * Tests for stubbles\input\broker\param\DatespanParamBroker. * - * @group broker - * @group broker_param * @since 4.3.0 */ +#[Group('broker')] +#[Group('broker_param')] class DatespanParamBrokerTest extends MultipleSourceParamBrokerTestBase { protected function setUp(): void @@ -28,8 +31,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -38,82 +39,69 @@ protected function getRequestAnnotationName(): string /** * returns expected filtered value - * - * @return \stubbles\date\span\Day */ protected function expectedValue(): Day { return new Day('2012-04-21'); } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSet(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['default' => '2012-04-21']) - ), - equals(new Day('2012-04-21')) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['default' => '2012-04-21']) + ), + equals(new Day('2012-04-21')) ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => true]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfBeforeMinStartDate(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('yesterday'), - $this->createRequestAnnotation(['minStartDate' => 'today']) - ) + $this->paramBroker->procure( + $this->createRequest('yesterday'), + $this->createRequestAnnotation(['minStartDate' => 'today']) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfAfterMaxStartDate(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('today'), - $this->createRequestAnnotation(['maxEndDate' => 'yesterday']) - ) + $this->paramBroker->procure( + $this->createRequest('today'), + $this->createRequestAnnotation(['maxEndDate' => 'yesterday']) + ) ); } - /** - * @test - */ + #[Test] public function returnsValueIfInRange(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('today'), - $this->createRequestAnnotation( - ['minStartDate' => 'yesterday', - 'maxEndDate' => 'tomorrow' - ] - ) - ), - equals(new Day('today')) + $this->paramBroker->procure( + $this->createRequest('today'), + $this->createRequestAnnotation([ + 'minStartDate' => 'yesterday', + 'maxEndDate' => 'tomorrow' + ]) + ), + equals(new Day('today')) ); } } diff --git a/src/test/php/broker/param/DayParamBrokerTest.php b/src/test/php/broker/param/DayParamBrokerTest.php index 2d7f61f..4abb26c 100644 --- a/src/test/php/broker/param/DayParamBrokerTest.php +++ b/src/test/php/broker/param/DayParamBrokerTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker\param; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\date\span\Day; use function bovigo\assert\assertThat; @@ -14,10 +17,9 @@ use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\broker\param\DayParamBroker. - * - * @group broker - * @group broker_param */ +#[Group('broker')] +#[Group('broker_param')] class DayParamBrokerTest extends MultipleSourceParamBrokerTestBase { protected function setUp(): void @@ -27,8 +29,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -37,82 +37,69 @@ protected function getRequestAnnotationName(): string /** * returns expected filtered value - * - * @return Day */ protected function expectedValue(): Day { return new Day('2012-04-21'); } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSet(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['default' => '2012-04-21']) - ), - equals(new Day('2012-04-21')) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['default' => '2012-04-21']) + ), + equals(new Day('2012-04-21')) ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => true]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfBeforeMinStartDate(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('yesterday'), - $this->createRequestAnnotation(['minStartDate' => 'today']) - ) + $this->paramBroker->procure( + $this->createRequest('yesterday'), + $this->createRequestAnnotation(['minStartDate' => 'today']) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfAfterMaxStartDate(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('today'), - $this->createRequestAnnotation(['maxEndDate' => 'yesterday']) - ) + $this->paramBroker->procure( + $this->createRequest('today'), + $this->createRequestAnnotation(['maxEndDate' => 'yesterday']) + ) ); } - /** - * @test - */ + #[Test] public function returnsValueIfInRange(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('today'), - $this->createRequestAnnotation( - ['minStartDate' => 'yesterday', - 'maxEndDate' => 'tomorrow' - ] - ) - ), - equals(new Day('today')) + $this->paramBroker->procure( + $this->createRequest('today'), + $this->createRequestAnnotation([ + 'minStartDate' => 'yesterday', + 'maxEndDate' => 'tomorrow' + ]) + ), + equals(new Day('today')) ); } } diff --git a/src/test/php/broker/param/FloatParamBrokerTest.php b/src/test/php/broker/param/FloatParamBrokerTest.php index cf7ace9..b4f1310 100644 --- a/src/test/php/broker/param/FloatParamBrokerTest.php +++ b/src/test/php/broker/param/FloatParamBrokerTest.php @@ -7,15 +7,18 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker\param; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; + use function bovigo\assert\assertThat; use function bovigo\assert\assertNull; use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\broker\param\FloatParamBroker. - * - * @group broker - * @group broker_param */ +#[Group('broker')] +#[Group('broker_param')] class FloatParamBrokerTest extends MultipleSourceParamBrokerTestBase { protected function setUp(): void @@ -25,8 +28,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -35,80 +36,68 @@ protected function getRequestAnnotationName(): string /** * returns expected filtered value - * - * @return float */ protected function expectedValue(): float { return 3.03; } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSet(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['default' => 3.03]) - ), - equals(3.03) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['default' => 3.03]) + ), + equals(3.03) ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => true]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfLowerThanMinValue(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('3.03'), - $this->createRequestAnnotation(['minValue' => 4]) - ) + $this->paramBroker->procure( + $this->createRequest('3.03'), + $this->createRequestAnnotation(['minValue' => 4]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfGreaterThanMaxValue(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('3.03'), - $this->createRequestAnnotation(['maxValue' => 3]) - ) + $this->paramBroker->procure( + $this->createRequest('3.03'), + $this->createRequestAnnotation(['maxValue' => 3]) + ) ); } - /** - * @test - */ + #[Test] public function returnsValueIfInRange(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('3.03'), - $this->createRequestAnnotation( - ['minValue' => 3, 'maxValue' => 4] - ) - ), - equals(3.03) + $this->paramBroker->procure( + $this->createRequest('3.03'), + $this->createRequestAnnotation( + ['minValue' => 3, 'maxValue' => 4] + ) + ), + equals(3.03) ); } } diff --git a/src/test/php/broker/param/HttpUriParamBrokerTest.php b/src/test/php/broker/param/HttpUriParamBrokerTest.php index b9b7bf4..1ea1699 100644 --- a/src/test/php/broker/param/HttpUriParamBrokerTest.php +++ b/src/test/php/broker/param/HttpUriParamBrokerTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker\param; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\peer\http\HttpUri; use function bovigo\assert\assertThat; @@ -14,10 +17,9 @@ use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\broker\param\HttpUriParamBroker. - * - * @group broker - * @group broker_param */ +#[Group('broker')] +#[Group('broker_param')] class HttpUriParamBrokerTest extends MultipleSourceParamBrokerTestBase { protected function setUp(): void @@ -27,8 +29,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -37,65 +37,55 @@ protected function getRequestAnnotationName(): string /** * returns expected filtered value - * - * @return HttpUri */ protected function expectedValue(): HttpUri { return HttpUri::fromString('http://localhost/'); } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSet(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['default' => 'http://localhost/']) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['default' => 'http://localhost/']) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function returnsValueIfDnsCheckEnabledAndSuccessful(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('http://localhost/'), - $this->createRequestAnnotation(['dnsCheck' => true]) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $this->createRequest('http://localhost/'), + $this->createRequestAnnotation(['dnsCheck' => true]) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => true]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullForInvalidHttpUri(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('invalid'), - $this->createRequestAnnotation() - ) + $this->paramBroker->procure( + $this->createRequest('invalid'), + $this->createRequestAnnotation() + ) ); } } diff --git a/src/test/php/broker/param/IntegerParamBrokerTest.php b/src/test/php/broker/param/IntegerParamBrokerTest.php index 050485e..1aa387c 100644 --- a/src/test/php/broker/param/IntegerParamBrokerTest.php +++ b/src/test/php/broker/param/IntegerParamBrokerTest.php @@ -7,15 +7,18 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker\param; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; + use function bovigo\assert\assertThat; use function bovigo\assert\assertNull; use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\broker\param\IntegerParamBroker. - * - * @group broker - * @group broker_param */ +#[Group('broker')] +#[Group('broker_param')] class IntegerParamBrokerTest extends MultipleSourceParamBrokerTestBase { protected function setUp(): void @@ -25,8 +28,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -35,80 +36,68 @@ protected function getRequestAnnotationName(): string /** * returns expected filtered value - * - * @return int */ protected function expectedValue(): int { return 303; } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSet(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['default' => 303]) - ), - equals(303) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['default' => 303]) + ), + equals(303) ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => true]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfLowerThanMinValue(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('303'), - $this->createRequestAnnotation(['minValue' => 400]) - ) + $this->paramBroker->procure( + $this->createRequest('303'), + $this->createRequestAnnotation(['minValue' => 400]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfGreaterThanMaxValue(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('303'), - $this->createRequestAnnotation(['maxValue' => 300]) - ) + $this->paramBroker->procure( + $this->createRequest('303'), + $this->createRequestAnnotation(['maxValue' => 300]) + ) ); } - /** - * @test - */ + #[Test] public function returnsValueIfInRange(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('303'), - $this->createRequestAnnotation( - ['minValue' => 300, 'maxValue' => 400] - ) - ), - equals(303) + $this->paramBroker->procure( + $this->createRequest('303'), + $this->createRequestAnnotation( + ['minValue' => 300, 'maxValue' => 400] + ) + ), + equals(303) ); } } diff --git a/src/test/php/broker/param/JsonParamBrokerTest.php b/src/test/php/broker/param/JsonParamBrokerTest.php index aecbedf..ca2fc22 100644 --- a/src/test/php/broker/param/JsonParamBrokerTest.php +++ b/src/test/php/broker/param/JsonParamBrokerTest.php @@ -8,6 +8,9 @@ */ namespace stubbles\input\broker\param; use bovigo\callmap\NewInstance; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; +use stdClass; use stubbles\input\ValueReader; use function bovigo\assert\assertThat; @@ -15,12 +18,13 @@ use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\broker\param\JsonParamBroker. - * - * @group broker - * @group broker_param */ +#[Group('broker')] +#[Group('broker_param')] class JsonParamBrokerTest extends MultipleSourceParamBrokerTestBase { + private const REQUEST_PARAM_VALUE = '{"method":"add","params":[1,2],"id":1}'; + protected function setUp(): void { $this->paramBroker = new JsonParamBroker(); @@ -28,8 +32,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -38,119 +40,103 @@ protected function getRequestAnnotationName(): string /** * returns expected filtered value - * - * @return \stdClass */ - protected function expectedValue(): \stdClass + protected function expectedValue(): stdClass { - $phpJsonObj = new \stdClass(); + $phpJsonObj = new stdClass(); $phpJsonObj->method = 'add'; $phpJsonObj->params = [1, 2]; $phpJsonObj->id = 1; return $phpJsonObj; } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSet(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation( - ['default' => '{"method":"add","params":[1,2],"id":1}'] - ) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation( + ['default' => self::REQUEST_PARAM_VALUE] + ) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => true]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullForInvalidJson(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('{invalid'), - $this->createRequestAnnotation([]) - ) + $this->paramBroker->procure( + $this->createRequest('{invalid'), + $this->createRequestAnnotation([]) + ) ); } - /** - * @test - */ + #[Test] public function usesParamAsDefaultSource(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('{"method":"add","params":[1,2],"id":1}'), - $this->createRequestAnnotation([]) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $this->createRequest(self::REQUEST_PARAM_VALUE), + $this->createRequestAnnotation([]) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function usesParamAsSource(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('{"method":"add","params":[1,2],"id":1}'), - $this->createRequestAnnotation(['source' => 'param']) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $this->createRequest(self::REQUEST_PARAM_VALUE), + $this->createRequestAnnotation(['source' => 'param']) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function canUseHeaderAsSourceForWebRequest(): void { $request = NewInstance::of(WebRequest::class)->returns([ - 'readHeader' => ValueReader::forValue('{"method":"add","params":[1,2],"id":1}') + 'readHeader' => ValueReader::forValue(self::REQUEST_PARAM_VALUE) ]); assertThat( - $this->paramBroker->procure( - $request, - $this->createRequestAnnotation(['source' => 'header']) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $request, + $this->createRequestAnnotation(['source' => 'header']) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function canUseCookieAsSourceForWebRequest(): void { $request = NewInstance::of(WebRequest::class)->returns([ - 'readCookie' => ValueReader::forValue('{"method":"add","params":[1,2],"id":1}') + 'readCookie' => ValueReader::forValue(self::REQUEST_PARAM_VALUE) ]); assertThat( - $this->paramBroker->procure( - $request, - $this->createRequestAnnotation(['source' => 'cookie']) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $request, + $this->createRequestAnnotation(['source' => 'cookie']) + ), + equals($this->expectedValue()) ); } } diff --git a/src/test/php/broker/param/MailParamBrokerTest.php b/src/test/php/broker/param/MailParamBrokerTest.php index 8a7d93b..a4dc9ef 100644 --- a/src/test/php/broker/param/MailParamBrokerTest.php +++ b/src/test/php/broker/param/MailParamBrokerTest.php @@ -7,13 +7,15 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker\param; + +use PHPUnit\Framework\Attributes\Test; + use function bovigo\assert\assertNull; /** * Tests for stubbles\input\broker\param\MailParamBroker. - * - * @group broker - * @group broker_param */ +#[Group('broker')] +#[Group('broker_param')] class MailParamBrokerTest extends MultipleSourceParamBrokerTestBase { protected function setUp(): void @@ -23,8 +25,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -33,24 +33,20 @@ protected function getRequestAnnotationName(): string /** * returns expected filtered value - * - * @return string */ protected function expectedValue(): string { return 'me@example.com'; } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => true]) + ) ); } } diff --git a/src/test/php/broker/param/MonthParamBrokerTest.php b/src/test/php/broker/param/MonthParamBrokerTest.php index fbbd6d5..e369b80 100644 --- a/src/test/php/broker/param/MonthParamBrokerTest.php +++ b/src/test/php/broker/param/MonthParamBrokerTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker\param; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\date\span\Month; use function bovigo\assert\assertThat; @@ -15,10 +18,10 @@ /** * Tests for stubbles\input\broker\param\MonthParamBroker. * - * @group broker - * @group broker_param * @since 4.3.0 */ +#[Group('broker')] +#[Group('broker_param')] class MonthParamBrokerTest extends MultipleSourceParamBrokerTestBase { protected function setUp(): void @@ -28,8 +31,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -38,84 +39,71 @@ protected function getRequestAnnotationName(): string /** * returns expected filtered value - * - * @return Month */ protected function expectedValue(): Month { return new Month(2012, 04); } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSet(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['default' => '2012-04']) - ), - equals(new Month(2012, 04)) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['default' => '2012-04']) + ), + equals(new Month(2012, 04)) ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => true]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfBeforeMinStartDate(): void { $currentMonth = new Month(); assertNull( - $this->paramBroker->procure( - $this->createRequest($currentMonth->asString()), - $this->createRequestAnnotation(['minStartDate' => 'tomorrow']) - ) + $this->paramBroker->procure( + $this->createRequest($currentMonth->asString()), + $this->createRequestAnnotation(['minStartDate' => 'tomorrow']) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfAfterMaxStartDate(): void { $currentMonth = new Month(); assertNull( - $this->paramBroker->procure( - $this->createRequest($currentMonth->next()->asString()), - $this->createRequestAnnotation(['maxEndDate' => 'yesterday']) - ) + $this->paramBroker->procure( + $this->createRequest($currentMonth->next()->asString()), + $this->createRequestAnnotation(['maxEndDate' => 'yesterday']) + ) ); } - /** - * @test - */ + #[Test] public function returnsValueIfInRange(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('2015-02'), - $this->createRequestAnnotation( - ['minStartDate' => '2015-01-01', - 'maxEndDate' => '2015-03-31' - ] - ) - ), - equals(Month::fromString('2015-02')) + $this->paramBroker->procure( + $this->createRequest('2015-02'), + $this->createRequestAnnotation([ + 'minStartDate' => '2015-01-01', + 'maxEndDate' => '2015-03-31' + ]) + ), + equals(Month::fromString('2015-02')) ); } } diff --git a/src/test/php/broker/param/MultipleSourceParamBrokerTestBase.php b/src/test/php/broker/param/MultipleSourceParamBrokerTestBase.php index a424863..0a72027 100644 --- a/src/test/php/broker/param/MultipleSourceParamBrokerTestBase.php +++ b/src/test/php/broker/param/MultipleSourceParamBrokerTestBase.php @@ -10,6 +10,7 @@ use bovigo\callmap\ClassProxy; use bovigo\callmap\NewInstance; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use stubbles\input\Request; use stubbles\input\ValueReader; @@ -27,8 +28,6 @@ abstract class MultipleSourceParamBrokerTestBase extends TestCase /** * returns name of request annotation - * - * @return string */ abstract protected function getRequestAnnotationName(): string; @@ -64,9 +63,7 @@ protected function createRequest(mixed $value): Request&ClassProxy ]); } - /** - * @test - */ + #[Test] public function failsForUnknownSource(): void { expect(function() { @@ -77,9 +74,7 @@ public function failsForUnknownSource(): void })->throws(\RuntimeException::class); } - /** - * @test - */ + #[Test] public function usesParamAsDefaultSource(): void { assertThat( @@ -91,9 +86,7 @@ public function usesParamAsDefaultSource(): void ); } - /** - * @test - */ + #[Test] public function usesParamAsSource(): void { assertThat( @@ -105,9 +98,7 @@ public function usesParamAsSource(): void ); } - /** - * @test - */ + #[Test] public function canUseHeaderAsSourceForWebRequest(): void { $request = NewInstance::of(WebRequest::class)->returns([ @@ -122,9 +113,7 @@ public function canUseHeaderAsSourceForWebRequest(): void ); } - /** - * @test - */ + #[Test] public function canUseCookieAsSourceForWebRequest(): void { $request = NewInstance::of(WebRequest::class)->returns([ diff --git a/src/test/php/broker/param/OneOfParamBrokerTest.php b/src/test/php/broker/param/OneOfParamBrokerTest.php index 14ecfef..17d4c04 100644 --- a/src/test/php/broker/param/OneOfParamBrokerTest.php +++ b/src/test/php/broker/param/OneOfParamBrokerTest.php @@ -8,6 +8,9 @@ */ namespace stubbles\input\broker\param; use bovigo\callmap\NewInstance; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; +use RuntimeException; use stubbles\input\Request; use stubbles\input\ValueReader; @@ -17,10 +20,9 @@ use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\broker\param\OneOfParamBroker. - * - * @group broker - * @group broker_param */ +#[Group('broker')] +#[Group('broker_param')] class OneOfParamBrokerTest extends MultipleSourceParamBrokerTestBase { /** @@ -38,8 +40,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -48,263 +48,237 @@ protected function getRequestAnnotationName(): string /** * returns expected filtered value - * - * @return string */ protected function expectedValue(): string { return 'foo'; } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSet(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation( - ['allowed' => 'foo|bar', 'default' => 'baz'] - ) - ), - equals('baz') + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation( + ['allowed' => 'foo|bar', 'default' => 'baz'] + ) + ), + equals('baz') ); } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSetWithAllowedSource(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation([ - 'allowedSource' => __CLASS__ . '::allowedSource()', - 'default' => 'baz' - ]) - ), - equals('baz') + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation([ + 'allowedSource' => __CLASS__ . '::allowedSource()', + 'default' => 'baz' + ]) + ), + equals('baz') ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation( - ['allowed' => 'foo|bar', 'required' => true] - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation( + ['allowed' => 'foo|bar', 'required' => true] ) + ) ); } /** - * @test * @since 8.0.0 */ + #[Test] public function throwsRuntimeExceptionWhenAllowedSourceIsNoValidCallback(): void { expect(function() { $this->paramBroker->procure( $this->createRequest(null), $this->createRequestAnnotation([ - 'allowedSource' => __CLASS__ . '::doesNotExist()', - 'required' => true + 'allowedSource' => __CLASS__ . '::doesNotExist()', + 'required' => true ]) ); }) ->throws(\RuntimeException::class); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequiredWithAllowedSource(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation([ - 'allowedSource' => __CLASS__ . '::allowedSource()', - 'required' => true - ]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation([ + 'allowedSource' => __CLASS__ . '::allowedSource()', + 'required' => true + ]) + ) ); } - /** - * @test - */ + #[Test] public function failsForUnknownSource(): void { expect(function() { - $this->paramBroker->procure( - NewInstance::of(Request::class), - $this->createRequestAnnotation(['source' => 'foo']) - ); - })->throws(\RuntimeException::class); + $this->paramBroker->procure( + NewInstance::of(Request::class), + $this->createRequestAnnotation(['source' => 'foo']) + ); + })->throws(RuntimeException::class); } - /** - * @test - */ + #[Test] public function usesParamAsDefaultSource(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(((string) $this->expectedValue())), - $this->createRequestAnnotation(['allowed' => 'foo|bar']) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $this->createRequest(((string) $this->expectedValue())), + $this->createRequestAnnotation(['allowed' => 'foo|bar']) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function usesParamAsDefaultSourceWithAllowedSource(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(((string) $this->expectedValue())), - $this->createRequestAnnotation([ - 'allowedSource' => __CLASS__ . '::allowedSource()' - ]) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $this->createRequest(((string) $this->expectedValue())), + $this->createRequestAnnotation([ + 'allowedSource' => __CLASS__ . '::allowedSource()' + ]) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function usesParamAsSource(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(((string) $this->expectedValue())), - $this->createRequestAnnotation( - ['allowed' => 'foo|bar', 'source' => 'param'] - ) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $this->createRequest(((string) $this->expectedValue())), + $this->createRequestAnnotation( + ['allowed' => 'foo|bar', 'source' => 'param'] + ) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function usesParamAsSourceWithAllowedSource(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(((string) $this->expectedValue())), - $this->createRequestAnnotation([ - 'allowedSource' => __CLASS__ . '::allowedSource()', - 'source' => 'param' - ]) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $this->createRequest(((string) $this->expectedValue())), + $this->createRequestAnnotation([ + 'allowedSource' => __CLASS__ . '::allowedSource()', + 'source' => 'param' + ]) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function canUseHeaderAsSourceForWebRequest(): void { $request = NewInstance::of(WebRequest::class)->returns([ - 'readHeader' => ValueReader::forValue(((string) $this->expectedValue())) + 'readHeader' => ValueReader::forValue(((string) $this->expectedValue())) ]); assertThat( - $this->paramBroker->procure( - $request, - $this->createRequestAnnotation( - ['allowed' => 'foo|bar', 'source' => 'header'] - ) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $request, + $this->createRequestAnnotation( + ['allowed' => 'foo|bar', 'source' => 'header'] + ) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function canUseHeaderAsSourceForWebRequestWithAllowedSource(): void { $request = NewInstance::of(WebRequest::class)->returns([ - 'readHeader' => ValueReader::forValue(((string) $this->expectedValue())) + 'readHeader' => ValueReader::forValue(((string) $this->expectedValue())) ]); assertThat( - $this->paramBroker->procure( - $request, - $this->createRequestAnnotation([ - 'allowedSource' => __CLASS__ . '::allowedSource()', - 'source' => 'header' - ]) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $request, + $this->createRequestAnnotation([ + 'allowedSource' => __CLASS__ . '::allowedSource()', + 'source' => 'header' + ]) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function canUseCookieAsSourceForWebRequest(): void { $request = NewInstance::of(WebRequest::class)->returns([ - 'readCookie' => ValueReader::forValue(((string) $this->expectedValue())) + 'readCookie' => ValueReader::forValue(((string) $this->expectedValue())) ]); assertThat( - $this->paramBroker->procure( - $request, - $this->createRequestAnnotation( - ['allowed' => 'foo|bar', 'source' => 'cookie'] - ) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $request, + $this->createRequestAnnotation( + ['allowed' => 'foo|bar', 'source' => 'cookie'] + ) + ), + equals($this->expectedValue()) ); } - /** - * @test - */ + #[Test] public function canUseCookieAsSourceForWebRequestWithAllowedSource(): void { $request = NewInstance::of(WebRequest::class)->returns([ - 'readCookie' => ValueReader::forValue(((string) $this->expectedValue())) + 'readCookie' => ValueReader::forValue(((string) $this->expectedValue())) ]); assertThat( - $this->paramBroker->procure( - $request, - $this->createRequestAnnotation([ - 'allowedSource' => __CLASS__ . '::allowedSource()', - 'source' => 'cookie' - ]) - ), - equals($this->expectedValue()) + $this->paramBroker->procure( + $request, + $this->createRequestAnnotation([ + 'allowedSource' => __CLASS__ . '::allowedSource()', + 'source' => 'cookie' + ]) + ), + equals($this->expectedValue()) ); } /** - * @test * @since 3.0.0 */ + #[Test] public function throwsRuntimeAnnotationWhenListOfAllowedValuesIsMissing(): void { expect(function() { $this->paramBroker->procure( - $this->createRequest(((string) $this->expectedValue())), - $this->createRequestAnnotation() + $this->createRequest(((string) $this->expectedValue())), + $this->createRequestAnnotation() ); }) - ->throws(\RuntimeException::class) - ->withMessage('No list of allowed values in annotation @Request[OneOf] on SomeClass::someMethod()'); + ->throws(RuntimeException::class) + ->withMessage( + 'No list of allowed values in annotation @Request[OneOf] on SomeClass::someMethod()' + ); } } diff --git a/src/test/php/broker/param/PasswordParamBrokerTest.php b/src/test/php/broker/param/PasswordParamBrokerTest.php index 0f60800..f3206a8 100644 --- a/src/test/php/broker/param/PasswordParamBrokerTest.php +++ b/src/test/php/broker/param/PasswordParamBrokerTest.php @@ -7,7 +7,11 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker\param; + +use bovigo\callmap\ClassProxy; use bovigo\callmap\NewInstance; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use stubbles\input\Request; use stubbles\input\ValueReader; @@ -20,36 +24,26 @@ use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\broker\param\PasswordParamBroker. - * - * @group broker - * @group broker_param */ +#[Group('broker')] +#[Group('broker_param')] class PasswordParamBrokerTest extends TestCase { - /** - * @var PasswordParamBroker - */ - private $paramBroker; + private PasswordParamBroker $paramBroker; protected function setUp(): void { $this->paramBroker = new PasswordParamBroker(); } - - /** - * @param string $expectedPassword - * @param Secret $actualPassword - */ - private function assertPasswordEquals(string $expectedPassword, Secret $actualPassword): void - { + private function assertPasswordEquals( + string $expectedPassword, + Secret $actualPassword + ): void { assertThat($actualPassword->unveil(), equals($expectedPassword)); } /** - * creates request annotation - * - * @param array $values - * @return Annotation + * @param array $values */ protected function createRequestAnnotation(array $values = []): Annotation { @@ -62,144 +56,122 @@ protected function createRequestAnnotation(array $values = []): Annotation ); } - /** - * creates mocked request - * - * @param mixed $value - * @return Request&\bovigo\callmap\ClassProxy - */ - protected function createRequest($value): Request + protected function createRequest(mixed $value): Request&ClassProxy { return NewInstance::of(Request::class)->returns([ - 'readParam' => ValueReader::forValue($value) + 'readParam' => ValueReader::forValue($value) ]); } - /** - * @test - */ + #[Test] public function failsForUnknownSource(): void { expect(function() { - $this->paramBroker->procure( - NewInstance::of(Request::class), - $this->createRequestAnnotation(['source' => 'foo']) - ); + $this->paramBroker->procure( + NewInstance::of(Request::class), + $this->createRequestAnnotation(['source' => 'foo']) + ); })->throws(\RuntimeException::class); } - /** - * @test - */ + #[Test] public function usesParamAsDefaultSource(): void { $this->assertPasswordEquals( - 'topsecret', - $this->paramBroker->procure( - $this->createRequest('topsecret'), - $this->createRequestAnnotation() - ) + 'topsecret', + $this->paramBroker->procure( + $this->createRequest('topsecret'), + $this->createRequestAnnotation() + ) ); } - /** - * @test - */ + #[Test] public function usesParamAsSource(): void { $this->assertPasswordEquals( - 'topsecret', - $this->paramBroker->procure( - $this->createRequest('topsecret'), - $this->createRequestAnnotation(['source' => 'param']) - ) + 'topsecret', + $this->paramBroker->procure( + $this->createRequest('topsecret'), + $this->createRequestAnnotation(['source' => 'param']) + ) ); } - /** - * @test - */ + #[Test] public function canUseHeaderAsSourceForWebRequest(): void { $request = NewInstance::of(WebRequest::class)->returns([ - 'readHeader' => ValueReader::forValue('topsecret') + 'readHeader' => ValueReader::forValue('topsecret') ]); $this->assertPasswordEquals( - 'topsecret', - $this->paramBroker->procure( - $request, - $this->createRequestAnnotation(['source' => 'header']) - ) + 'topsecret', + $this->paramBroker->procure( + $request, + $this->createRequestAnnotation(['source' => 'header']) + ) ); } - /** - * @test - */ + #[Test] public function canUseCookieAsSourceForWebRequest(): void { $request = NewInstance::of(WebRequest::class)->returns([ - 'readCookie' => ValueReader::forValue('topsecret') + 'readCookie' => ValueReader::forValue('topsecret') ]); $this->assertPasswordEquals( - 'topsecret', - $this->paramBroker->procure( - $request, - $this->createRequestAnnotation(['source' => 'cookie']) - ) + 'topsecret', + $this->paramBroker->procure( + $request, + $this->createRequestAnnotation(['source' => 'cookie']) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation() - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation() + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndNotRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => false]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => false]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfTooLessMinDiffChars(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('topsecret'), - $this->createRequestAnnotation(['minDiffChars' => 20]) - ) + $this->paramBroker->procure( + $this->createRequest('topsecret'), + $this->createRequestAnnotation(['minDiffChars' => 20]) + ) ); } /** - * @test * @since 3.0.0 */ + #[Test] public function returnsNullIfTooShort(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('topsecret'), - $this->createRequestAnnotation(['minLength' => 20]) - ) + $this->paramBroker->procure( + $this->createRequest('topsecret'), + $this->createRequestAnnotation(['minLength' => 20]) + ) ); } } diff --git a/src/test/php/broker/param/SecretParamBrokerTest.php b/src/test/php/broker/param/SecretParamBrokerTest.php index ea8548d..92a39ac 100644 --- a/src/test/php/broker/param/SecretParamBrokerTest.php +++ b/src/test/php/broker/param/SecretParamBrokerTest.php @@ -8,7 +8,10 @@ */ namespace stubbles\input\broker\param; use bovigo\callmap\NewInstance; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +use RuntimeException; use stubbles\input\Request; use stubbles\input\ValueReader; use stubbles\reflect\annotation\Annotation; @@ -21,175 +24,143 @@ /** * Tests for stubbles\input\broker\param\SecretParamBroker. * - * @group broker - * @group broker_param * @since 3.0.0 */ +#[Group('broker')] +#[Group('broker_param')] class SecretParamBrokerTest extends TestCase { - /** - * @var SecretParamBroker - */ - private $paramBroker; + private SecretParamBroker $paramBroker; protected function setUp(): void { $this->paramBroker = new SecretParamBroker(); } - /** - * @param string $expected - * @param Secret $actual - */ private function assertSecretEquals(string $expected, Secret $actual): void { assertThat($actual->unveil(), equals($expected)); } /** - * creates request annotation - * - * @param array $values - * @return Annotation + * @param array $values */ protected function createRequestAnnotation(array $values = []): Annotation { $values['paramName'] = 'foo'; return new Annotation( - 'Secret', - 'foo', - array_map(function($value) { return (string) $value; }, $values), - 'Request' + 'Secret', + 'foo', + array_map(fn($value): string => (string) $value, $values), + 'Request' ); } - /** - * creates mocked request - * - * @param mixed $value - * @return Request - */ - protected function createRequest($value): Request + protected function createRequest(mixed $value): Request { return NewInstance::of(Request::class)->returns([ - 'readParam' => ValueReader::forValue($value) + 'readParam' => ValueReader::forValue($value) ]); } - /** - * @test - */ + #[Test] public function failsForUnknownSource(): void { expect(function() { - $this->paramBroker->procure( - NewInstance::of(Request::class), - $this->createRequestAnnotation(['source' => 'foo']) - ); - })->throws(\RuntimeException::class); + $this->paramBroker->procure( + NewInstance::of(Request::class), + $this->createRequestAnnotation(['source' => 'foo']) + ); + })->throws(RuntimeException::class); } - /** - * @test - */ + #[Test] public function usesParamAsDefaultSource(): void { $this->assertSecretEquals( - 'topsecret', - $this->paramBroker->procure( - $this->createRequest('topsecret'), - $this->createRequestAnnotation() - ) + 'topsecret', + $this->paramBroker->procure( + $this->createRequest('topsecret'), + $this->createRequestAnnotation() + ) ); } - /** - * @test - */ + #[Test] public function usesParamAsSource(): void { $this->assertSecretEquals( - 'topsecret', - $this->paramBroker->procure( - $this->createRequest('topsecret'), - $this->createRequestAnnotation(['source' => 'param']) - ) + 'topsecret', + $this->paramBroker->procure( + $this->createRequest('topsecret'), + $this->createRequestAnnotation(['source' => 'param']) + ) ); } - /** - * @test - */ + #[Test] public function canUseHeaderAsSourceForWebRequest(): void { $request = NewInstance::of(WebRequest::class)->returns([ - 'readHeader' => ValueReader::forValue('topsecret') + 'readHeader' => ValueReader::forValue('topsecret') ]); $this->assertSecretEquals( - 'topsecret', - $this->paramBroker->procure( - $request, - $this->createRequestAnnotation(['source' => 'header']) - ) + 'topsecret', + $this->paramBroker->procure( + $request, + $this->createRequestAnnotation(['source' => 'header']) + ) ); } - /** - * @test - */ + #[Test] public function canUseCookieAsSourceForWebRequest(): void { $request = NewInstance::of(WebRequest::class)->returns([ 'readCookie' => ValueReader::forValue('topsecret') ]); $this->assertSecretEquals( - 'topsecret', - $this->paramBroker->procure( - $request, - $this->createRequestAnnotation(['source' => 'cookie']) - ) + 'topsecret', + $this->paramBroker->procure( + $request, + $this->createRequestAnnotation(['source' => 'cookie']) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => true]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfShorterThanMinLength(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('Do you expect me to talk?'), - $this->createRequestAnnotation(['minLength' => 30]) - ) + $this->paramBroker->procure( + $this->createRequest('Do you expect me to talk?'), + $this->createRequestAnnotation(['minLength' => 30]) + ) ); } - /** - * @test - */ + #[Test] public function returnsValueIfLongerThenMinLength(): void { $this->assertSecretEquals( - 'Do you expect me to talk?', - $this->paramBroker->procure( - $this->createRequest('Do you expect me to talk?'), - $this->createRequestAnnotation( - ['minLength' => 10] - ) + 'Do you expect me to talk?', + $this->paramBroker->procure( + $this->createRequest('Do you expect me to talk?'), + $this->createRequestAnnotation( + ['minLength' => 10] ) + ) ); } } diff --git a/src/test/php/broker/param/StringParamBrokerTest.php b/src/test/php/broker/param/StringParamBrokerTest.php index 2b8c238..16fe96a 100644 --- a/src/test/php/broker/param/StringParamBrokerTest.php +++ b/src/test/php/broker/param/StringParamBrokerTest.php @@ -7,15 +7,18 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker\param; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; + use function bovigo\assert\assertThat; use function bovigo\assert\assertNull; use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\broker\param\StringParamBroker. - * - * @group broker - * @group broker_param */ +#[Group('broker')] +#[Group('broker_param')] class StringParamBrokerTest extends MultipleSourceParamBrokerTestBase { protected function setUp(): void @@ -25,8 +28,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -35,82 +36,70 @@ protected function getRequestAnnotationName(): string /** * returns expected filtered value - * - * @return string */ protected function expectedValue(): string { return 'Do you expect me to talk?'; } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSet(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation( - ['default' => 'No Mr Bond, I expect you to die!'] - ) - ), - equals('No Mr Bond, I expect you to die!') + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation( + ['default' => 'No Mr Bond, I expect you to die!'] + ) + ), + equals('No Mr Bond, I expect you to die!') ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => true]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfShorterThanMinLength(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('Do you expect me to talk?'), - $this->createRequestAnnotation(['minLength' => 30]) - ) + $this->paramBroker->procure( + $this->createRequest('Do you expect me to talk?'), + $this->createRequestAnnotation(['minLength' => 30]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfLongerThanMaxLength(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('Do you expect me to talk?'), - $this->createRequestAnnotation(['maxLength' => 10]) - ) + $this->paramBroker->procure( + $this->createRequest('Do you expect me to talk?'), + $this->createRequestAnnotation(['maxLength' => 10]) + ) ); } - /** - * @test - */ + #[Test] public function returnsValueIfInRange(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('Do you expect me to talk?'), - $this->createRequestAnnotation( - ['minLength' => 10, 'maxLength' => 30] - ) - ), - equals('Do you expect me to talk?') + $this->paramBroker->procure( + $this->createRequest('Do you expect me to talk?'), + $this->createRequestAnnotation( + ['minLength' => 10, 'maxLength' => 30] + ) + ), + equals('Do you expect me to talk?') ); } } diff --git a/src/test/php/broker/param/TextParamBrokerTest.php b/src/test/php/broker/param/TextParamBrokerTest.php index a3c881d..d109332 100644 --- a/src/test/php/broker/param/TextParamBrokerTest.php +++ b/src/test/php/broker/param/TextParamBrokerTest.php @@ -7,15 +7,18 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker\param; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; + use function bovigo\assert\assertThat; use function bovigo\assert\assertNull; use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\broker\param\TextParamBroker. - * - * @group broker - * @group broker_param */ +#[Group('broker')] +#[Group('broker_param')] class TextParamBrokerTest extends MultipleSourceParamBrokerTestBase { protected function setUp(): void @@ -25,8 +28,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -35,101 +36,86 @@ protected function getRequestAnnotationName(): string /** * returns expected filtered value - * - * @return string */ protected function expectedValue(): string { return 'Do you expect me to talk?'; } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSet(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation( - ['default' => 'No Mr Bond, I expect you to die!'] - ) - ), - equals('No Mr Bond, I expect you to die!') + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation( + ['default' => 'No Mr Bond, I expect you to die!'] + ) + ), + equals('No Mr Bond, I expect you to die!') ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => true]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfShorterThanMinLength(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('Do you expect me to talk?'), - $this->createRequestAnnotation(['minLength' => 40]) - ) + $this->paramBroker->procure( + $this->createRequest('Do you expect me to talk?'), + $this->createRequestAnnotation(['minLength' => 40]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfLongerThanMaxLength(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest('Do you expect me to talk?'), - $this->createRequestAnnotation(['maxLength' => 10]) - ) + $this->paramBroker->procure( + $this->createRequest('Do you expect me to talk?'), + $this->createRequestAnnotation(['maxLength' => 10]) + ) ); } - /** - * @test - */ + #[Test] public function returnsValueIfInRange(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('Do you expect me to talk?'), - $this->createRequestAnnotation( - ['minLength' => 10, 'maxLength' => 40] - ) - ), - equals('Do you expect me to talk?') + $this->paramBroker->procure( + $this->createRequest('Do you expect me to talk?'), + $this->createRequestAnnotation( + ['minLength' => 10, 'maxLength' => 40] + ) + ), + equals('Do you expect me to talk?') ); } - /** - * @test - */ + #[Test] public function returnsValueWithTagsIfAllowed(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('Do you expect me to talk?'), - $this->createRequestAnnotation( - ['minLength' => 10, - 'maxLength' => 40, - 'allowedTags' => 'b, u' - ] - ) - ), - equals('Do you expect me to talk?') + $this->paramBroker->procure( + $this->createRequest('Do you expect me to talk?'), + $this->createRequestAnnotation([ + 'minLength' => 10, + 'maxLength' => 40, + 'allowedTags' => 'b, u' + ]) + ), + equals('Do you expect me to talk?') ); } } diff --git a/src/test/php/broker/param/WeekParamBrokerTest.php b/src/test/php/broker/param/WeekParamBrokerTest.php index 3f7c56e..44be178 100644 --- a/src/test/php/broker/param/WeekParamBrokerTest.php +++ b/src/test/php/broker/param/WeekParamBrokerTest.php @@ -7,6 +7,8 @@ * file that was distributed with this source code. */ namespace stubbles\input\broker\param; + +use PHPUnit\Framework\Attributes\Test; use stubbles\date\Date; use stubbles\date\span\Week; @@ -16,10 +18,10 @@ /** * Tests for stubbles\input\broker\param\WeekParamBroker. * - * @group broker - * @group broker_param * @since 4.5.0 */ +#[Group('broker')] +#[Group('broker_param')] class WeekParamBrokerTest extends MultipleSourceParamBrokerTestBase { protected function setUp(): void @@ -29,8 +31,6 @@ protected function setUp(): void /** * returns name of request annotation - * - * @return string */ protected function getRequestAnnotationName(): string { @@ -39,84 +39,71 @@ protected function getRequestAnnotationName(): string /** * returns expected filtered value - * - * @return \stubbles\date\span\Week */ protected function expectedValue(): Week { return Week::fromString('2015-W22'); } - /** - * @test - */ + #[Test] public function usesDefaultFromAnnotationIfParamNotSet(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['default' => '2015-W22']) - ), - equals(Week::fromString('2015-W22')) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['default' => '2015-W22']) + ), + equals(Week::fromString('2015-W22')) ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamNotSetAndRequired(): void { assertNull( - $this->paramBroker->procure( - $this->createRequest(null), - $this->createRequestAnnotation(['required' => true]) - ) + $this->paramBroker->procure( + $this->createRequest(null), + $this->createRequestAnnotation(['required' => true]) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfBeforeMinStartDate(): void { $currentWeek = new Week(new Date('previous monday')); assertNull( - $this->paramBroker->procure( - $this->createRequest($currentWeek->asString()), - $this->createRequestAnnotation(['minStartDate' => 'next monday']) - ) + $this->paramBroker->procure( + $this->createRequest($currentWeek->asString()), + $this->createRequestAnnotation(['minStartDate' => 'next monday']) + ) ); } - /** - * @test - */ + #[Test] public function returnsNullIfAfterMaxStartDate(): void { $currentWeek = new Week(new Date('tomorrow')); assertNull( - $this->paramBroker->procure( - $this->createRequest($currentWeek->asString()), - $this->createRequestAnnotation(['maxEndDate' => 'yesterday']) - ) + $this->paramBroker->procure( + $this->createRequest($currentWeek->asString()), + $this->createRequestAnnotation(['maxEndDate' => 'yesterday']) + ) ); } - /** - * @test - */ + #[Test] public function returnsValueIfInRange(): void { assertThat( - $this->paramBroker->procure( - $this->createRequest('2015-W22'), - $this->createRequestAnnotation( - ['minStartDate' => '2015-05-01', - 'maxEndDate' => '2015-05-31' - ] - ) - ), - equals(Week::fromString('2015-W22')) + $this->paramBroker->procure( + $this->createRequest('2015-W22'), + $this->createRequestAnnotation([ + 'minStartDate' => '2015-05-01', + 'maxEndDate' => '2015-05-31' + ]) + ), + equals(Week::fromString('2015-W22')) ); } } diff --git a/src/test/php/errors/ParamErrorTest.php b/src/test/php/errors/ParamErrorTest.php index 36bf538..51f2046 100644 --- a/src/test/php/errors/ParamErrorTest.php +++ b/src/test/php/errors/ParamErrorTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input\errors; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use stubbles\input\errors\messages\LocalizedMessage; @@ -16,90 +19,69 @@ use function stubbles\reflect\annotationsOf; /** * Tests for stubbles\input\errors\ParamError. - * - * @group errors */ +#[Group('errors')] class ParamErrorTest extends TestCase { - /** - * instance to test - * - * @var ParamError - */ - private $paramError; + private const MSG_TEMPLATES = [ + 'en_*' => 'An error of type {foo} occurred.', + 'de_DE' => 'Es ist ein Fehler vom Typ {foo} aufgetreten.' + ]; + + private ParamError $paramError; protected function setUp(): void { $this->paramError = new ParamError('id', ['foo' => 'bar']); } - /** - * @test - */ + #[Test] public function returnsGivenId(): void { assertThat($this->paramError->id(), equals('id')); } /** - * @test * @since 5.1.0 */ + #[Test] public function returnsGivenDetails(): void { assertThat($this->paramError->details(), equals(['foo' => 'bar'])); } - /** - * @test - */ + #[Test] public function replacesPlaceHolderInMessagesWithDetails(): void { assertThat( - $this->paramError->fillMessages( - ['en_*' => 'An error of type {foo} occurred.', - 'de_DE' => 'Es ist ein Fehler vom Typ {foo} aufgetreten.' - ] - ), - equals([ - new LocalizedMessage('en_*', 'An error of type bar occurred.'), - new LocalizedMessage('de_DE', 'Es ist ein Fehler vom Typ bar aufgetreten.') - ]) + $this->paramError->fillMessages(self::MSG_TEMPLATES), + equals([ + new LocalizedMessage('en_*', 'An error of type bar occurred.'), + new LocalizedMessage('de_DE', 'Es ist ein Fehler vom Typ bar aufgetreten.') + ]) ); } - /** - * @test - */ + #[Test] public function replacesPlaceHolderInMessagesWithFlattenedArrayDetails(): void { $this->paramError = new ParamError('id', ['foo' => ['bar', 'baz']]); assertThat( - $this->paramError->fillMessages( - ['en_*' => 'An error of type {foo} occurred.', - 'de_DE' => 'Es ist ein Fehler vom Typ {foo} aufgetreten.' - ] - ), - equals([ - new LocalizedMessage('en_*', 'An error of type bar, baz occurred.'), - new LocalizedMessage('de_DE', 'Es ist ein Fehler vom Typ bar, baz aufgetreten.') - ]) + $this->paramError->fillMessages(self::MSG_TEMPLATES), + equals([ + new LocalizedMessage('en_*', 'An error of type bar, baz occurred.'), + new LocalizedMessage('de_DE', 'Es ist ein Fehler vom Typ bar, baz aufgetreten.') + ]) ); } - /** - * @test - */ + #[Test] public function replacesPlaceHolderInMessagesWithObjectDetails(): void { $this->paramError = new ParamError('id', ['foo' => new \stdClass()]); assertThat( - $this->paramError->fillMessages( - ['en_*' => 'An error of type {foo} occurred.', - 'de_DE' => 'Es ist ein Fehler vom Typ {foo} aufgetreten.' - ] - ), + $this->paramError->fillMessages(self::MSG_TEMPLATES), equals([ new LocalizedMessage('en_*', 'An error of type stdClass occurred.'), new LocalizedMessage('de_DE', 'Es ist ein Fehler vom Typ stdClass aufgetreten.') @@ -107,40 +89,30 @@ public function replacesPlaceHolderInMessagesWithObjectDetails(): void ); } - /** - * @test - */ + #[Test] public function doesNotReplacePlaceHolderInMessagesIfDetailsNotSet(): void { $this->paramError = new ParamError('id'); assertThat( - $this->paramError->fillMessages( - ['en_*' => 'An error of type {foo} occurred.', - 'de_DE' => 'Es ist ein Fehler vom Typ {foo} aufgetreten.' - ] - ), - equals([ - new LocalizedMessage('en_*', 'An error of type {foo} occurred.'), - new LocalizedMessage('de_DE', 'Es ist ein Fehler vom Typ {foo} aufgetreten.') - ]) + $this->paramError->fillMessages(self::MSG_TEMPLATES), + equals([ + new LocalizedMessage('en_*', 'An error of type {foo} occurred.'), + new LocalizedMessage('de_DE', 'Es ist ein Fehler vom Typ {foo} aufgetreten.') + ]) ); } - /** - * @test - */ + #[Test] public function annotationsPresentOnClass(): void { assertTrue(annotationsOf($this->paramError)->contain('XmlTag')); } - /** - * @test - */ + #[Test] public function annotationsPresentOnIdMethod(): void { assertTrue( - annotationsOf($this->paramError, 'id')->contain('XmlAttribute') + annotationsOf($this->paramError, 'id')->contain('XmlAttribute') ); } } diff --git a/src/test/php/errors/ParamErrorsTest.php b/src/test/php/errors/ParamErrorsTest.php index ae0fbcf..9566d17 100644 --- a/src/test/php/errors/ParamErrorsTest.php +++ b/src/test/php/errors/ParamErrorsTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input\errors; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertThat; @@ -19,152 +22,116 @@ use function bovigo\assert\predicate\isSameAs; /** * Tests for stubbles\input\errors\ParamErrors. - * - * @group errors */ +#[Group('errors')] class ParamErrorsTest extends TestCase { - /** - * instance to test - * - * @var ParamErrors - */ - private $paramErrors; + private ParamErrors $paramErrors; protected function setUp(): void { $this->paramErrors = new ParamErrors(); } - /** - * @test - */ + #[Test] public function hasNoErrorsInitially(): void { assertFalse($this->paramErrors->exist()); } - /** - * @test - */ + #[Test] public function initialErrorCountIsZero(): void { assertThat($this->paramErrors, isOfSize(0)); } - /** - * @test - */ + #[Test] public function paramErrorsExistIfOneAppended(): void { $this->paramErrors->append('foo', 'errorid'); assertTrue($this->paramErrors->exist()); } - /** - * @test - */ + #[Test] public function appendedErrorExistsForGivenParamName(): void { $this->paramErrors->append('foo', 'errorid'); assertTrue($this->paramErrors->existFor('foo')); } - /** - * @test - */ + #[Test] public function appendedErrorExistsForGivenParamNameAndErrorId(): void { $this->paramErrors->append('foo', 'errorid'); assertTrue($this->paramErrors->existForWithId('foo', 'errorid')); } - /** - * @test - */ + #[Test] public function appendingAnErrorIncreasesErrorCount(): void { $this->paramErrors->append('foo', 'errorid'); assertThat($this->paramErrors, isOfSize(1)); } - /** - * @test - */ + #[Test] public function appendedErrorIsContainedInListForParam(): void { $paramError = $this->paramErrors->append('foo', 'errorid'); assertThat( - $this->paramErrors->getFor('foo'), - equals(['errorid' => $paramError]) + $this->paramErrors->getFor('foo'), + equals(['errorid' => $paramError]) ); } - /** - * @test - */ + #[Test] public function appendedErrorIsReturnedWhenRequested(): void { $paramError = $this->paramErrors->append('foo', 'errorid'); assertThat( - $this->paramErrors->getForWithId('foo', 'errorid'), - isSameAs($paramError) + $this->paramErrors->getForWithId('foo', 'errorid'), + isSameAs($paramError) ); } - /** - * @test - */ + #[Test] public function existForReturnsFalseIfNoErrorAddedBefore(): void { assertFalse($this->paramErrors->existFor('foo')); } - /** - * @test - */ + #[Test] public function getForReturnsEmptyArrayIfNoErrorAddedBefore(): void { assertEmptyArray($this->paramErrors->getFor('foo')); } - /** - * @test - */ + #[Test] public function existForWithIdReturnsFalseIfNoErrorAddedBefore(): void { assertFalse($this->paramErrors->existForWithId('foo', 'id')); } - /** - * @test - */ + #[Test] public function getForWithIdReturnsNullIfNoErrorAddedBefore(): void { assertNull($this->paramErrors->getForWithId('foo', 'id')); } - /** - * @test - */ + #[Test] public function existForWithIdReturnsFalseIfNoErrorOfThisNameAddedBefore(): void { $this->paramErrors->append('foo', 'errorid'); assertFalse($this->paramErrors->existForWithId('foo', 'baz')); } - /** - * @test - */ + #[Test] public function getForWithIdReturnsNullIfNoErrorOfThisNameAddedBefore(): void { $this->paramErrors->append('foo', 'errorid'); assertNull($this->paramErrors->getForWithId('foo', 'baz')); } - /** - * @test - */ + #[Test] public function canIterateOverParamErrors(): void { $paramError1 = $this->paramErrors->append('foo', 'id1'); @@ -175,8 +142,8 @@ public function canIterateOverParamErrors(): void if (0 === $i) { assertThat($paramName, equals('foo')); assertThat( - $paramErrors, - equals(['id1' => $paramError1, 'id2' => $paramError2]) + $paramErrors, + equals(['id1' => $paramError1, 'id2' => $paramError2]) ); } else { assertThat($paramName, equals('bar')); diff --git a/src/test/php/errors/messages/LocalizedMessageTest.php b/src/test/php/errors/messages/LocalizedMessageTest.php index 687a73c..c0bb99f 100644 --- a/src/test/php/errors/messages/LocalizedMessageTest.php +++ b/src/test/php/errors/messages/LocalizedMessageTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input\errors\messages; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertThat; @@ -17,84 +20,71 @@ * Tests for stubbles\input\errors\messages\LocalizedMessage. * * @since 3.0.0 - * @group errors - * @group errors_message */ +#[Group('errors')] +#[Group('errors_message')] class LocalizedMessageTest extends TestCase { - /** - * instance to test - * - * @var LocalizedMessage - */ - private $localizedMessage; + private const TEST_STRING = 'This is a localized string.'; + + private LocalizedMessage $localizedMessage; protected function setUp(): void { $this->localizedMessage = new LocalizedMessage( - 'en_EN', - 'This is a localized string.' + 'en_EN', + self::TEST_STRING ); } - /** - * @test - */ + #[Test] public function annotationPresentOnClass(): void { assertTrue(annotationsOf($this->localizedMessage)->contain('XmlTag')); } - /** - * @test - */ + #[Test] public function annotationPresentOnGetLocaleMethod(): void { assertTrue( - annotationsOf($this->localizedMessage, 'locale') - ->contain('XmlAttribute') + annotationsOf($this->localizedMessage, 'locale') + ->contain('XmlAttribute') ); } - /** - * @test - */ + #[Test] public function annotationPresentOngetMessageMethod(): void { assertTrue( - annotationsOf($this->localizedMessage, 'message') - ->contain('XmlTag') + annotationsOf($this->localizedMessage, 'message') + ->contain('XmlTag') ); } - /** - * @test - */ + #[Test] public function localeAttributeEqualsGivenLocale(): void { assertThat($this->localizedMessage->locale(), equals('en_EN')); } - /** - * @test - */ + #[Test] public function contentOfStringEqualsGivenString(): void { assertThat( - $this->localizedMessage->message(), - equals('This is a localized string.') + $this->localizedMessage->message(), + equals(self::TEST_STRING) ); } /** * @since 2.0.0 - * @test */ + #[Test] public function conversionToStringYieldsMessage(): void { assertThat( - (string) $this->localizedMessage, - equals('This is a localized string.') + (string) $this->localizedMessage, + equals(self::TEST_STRING) ); } } diff --git a/src/test/php/errors/messages/PropertyBasedParamErrorMessagesTest.php b/src/test/php/errors/messages/PropertyBasedParamErrorMessagesTest.php index 34c7497..817a8bb 100755 --- a/src/test/php/errors/messages/PropertyBasedParamErrorMessagesTest.php +++ b/src/test/php/errors/messages/PropertyBasedParamErrorMessagesTest.php @@ -12,6 +12,8 @@ use stubbles\input\errors\ParamError; use stubbles\values\ResourceLoader; use org\bovigo\vfs\vfsStream; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use function bovigo\assert\assertThat; use function bovigo\assert\assertEmptyArray; @@ -23,22 +25,17 @@ * Tests for stubbles\input\errors\messages\PropertyBasedParamErrorMessages. * * @since 1.3.0 - * @group errors - * @group errors_message */ +#[Group('errors')] +#[Group('errors_message')] class PropertyBasedParamErrorMessagesTest extends TestCase { - /** - * instance to test - * - * @var PropertyBasedParamErrorMessages - */ - private $errorMessages; + private PropertyBasedParamErrorMessages $errorMessages; protected function setUp(): void { $this->errorMessages = new PropertyBasedParamErrorMessages( - $this->createResourceLoader() + $this->createResourceLoader() ); } @@ -52,225 +49,206 @@ private function createResourceLoader(): ResourceLoader /** @var \org\bovigo\vfs\vfsStreamDirectory $dir2 */ $dir2 = $root->getChild('package2/input/error'); vfsStream::newFile('message.ini') - ->withContent('[id] + ->withContent('[id] default = An error of type {foo} occurred. en_* = An error of type {foo} occurred. de_DE = Es ist ein Fehler vom Typ {foo} aufgetreten. ') - ->at($dir1); + ->at($dir1); vfsStream::newFile('message.ini') - ->withContent('[id2] + ->withContent('[id2] default = An error of type {foo} occurred. en_* = An error of type {foo} occurred. de_DE = Es ist ein Fehler vom Typ {foo} aufgetreten. ') - ->at($dir2); + ->at($dir2); return NewInstance::of(ResourceLoader::class) - ->returns(['availableResourceUris' => [ - vfsStream::url('root/package1/input/error/message.ini'), - vfsStream::url('root/package2/input/error/message.ini') - ]] + ->returns(['availableResourceUris' => [ + vfsStream::url('root/package1/input/error/message.ini'), + vfsStream::url('root/package2/input/error/message.ini') + ]] ); } - /** - * @test - */ + #[Test] public function hasMessagesForErrorsFromBothSources(): void { assertTrue( - $this->errorMessages->existFor(new ParamError('id', ['foo' => 'bar'])) + $this->errorMessages->existFor(new ParamError('id', ['foo' => 'bar'])) ); assertTrue( - $this->errorMessages->existFor(new ParamError('id2', ['foo' => 'bar'])) + $this->errorMessages->existFor(new ParamError('id2', ['foo' => 'bar'])) ); } - /** - * @test - */ + #[Test] public function returnsTrueOnCheckForExistingError(): void { assertTrue( - $this->errorMessages->existFor(new ParamError('id', ['foo' => 'bar'])) + $this->errorMessages->existFor(new ParamError('id', ['foo' => 'bar'])) ); } - /** - * @test - */ + #[Test] public function returnsFalseOnCheckForNonExistingError(): void { assertFalse( - $this->errorMessages->existFor(new ParamError('doesNotExist')) + $this->errorMessages->existFor(new ParamError('doesNotExist')) ); } - /** - * @test - */ + #[Test] public function returnsListOfLocalesForExistingError(): void { assertThat( - $this->errorMessages->localesFor( - new ParamError('id', ['foo' => 'bar']) - ), - equals(['default', 'en_*', 'de_DE']) + $this->errorMessages->localesFor( + new ParamError('id', ['foo' => 'bar']) + ), + equals(['default', 'en_*', 'de_DE']) ); } - /** - * @test - */ + #[Test] public function returnsEmptyListOfLocalesForNonExistingError(): void { assertEmptyArray( - $this->errorMessages->localesFor(new ParamError('doesNotExist')) + $this->errorMessages->localesFor(new ParamError('doesNotExist')) ); } - /** - * @test - */ + #[Test] public function returnsListOfLocalizedMessagesForExistingError(): void { assertThat( - $this->errorMessages->messagesFor(new ParamError('id', ['foo' => 'bar'])), - equals([ - new LocalizedMessage('default', 'An error of type bar occurred.'), - new LocalizedMessage('en_*', 'An error of type bar occurred.'), - new LocalizedMessage('de_DE', 'Es ist ein Fehler vom Typ bar aufgetreten.') - ]) + $this->errorMessages->messagesFor(new ParamError('id', ['foo' => 'bar'])), + equals([ + new LocalizedMessage('default', 'An error of type bar occurred.'), + new LocalizedMessage('en_*', 'An error of type bar occurred.'), + new LocalizedMessage('de_DE', 'Es ist ein Fehler vom Typ bar aufgetreten.') + ]) ); } - /** - * @test - */ + #[Test] public function returnsEmptyMessageListForNonExistingError(): void { assertEmptyArray( - $this->errorMessages->messagesFor(new ParamError('doesNotExist')) + $this->errorMessages->messagesFor(new ParamError('doesNotExist')) ); } - /** - * @test - */ + #[Test] public function returnsMessageInExistingLocale(): void { assertThat( - $this->errorMessages->messageFor( - new ParamError('id', ['foo' => 'bar']), - 'de_DE' - ), - equals(new LocalizedMessage('de_DE', 'Es ist ein Fehler vom Typ bar aufgetreten.')) + $this->errorMessages->messageFor( + new ParamError('id', ['foo' => 'bar']), + 'de_DE' + ), + equals(new LocalizedMessage('de_DE', 'Es ist ein Fehler vom Typ bar aufgetreten.')) ); } - /** - * @test - */ + #[Test] public function returnsMessageInExistingBaseLocale(): void { assertThat( - $this->errorMessages->messageFor( - new ParamError('id', ['foo' => 'bar']), - 'en_UK' - ), - equals(new LocalizedMessage('en_*', 'An error of type bar occurred.')) + $this->errorMessages->messageFor( + new ParamError('id', ['foo' => 'bar']), + 'en_UK' + ), + equals(new LocalizedMessage('en_*', 'An error of type bar occurred.')) ); } - /** - * @test - */ + #[Test] public function returnsMessageInDefaultLocale(): void { - $errorMessages = new PropertyBasedParamErrorMessages($this->createResourceLoader(), 'en_*'); + $errorMessages = new PropertyBasedParamErrorMessages( + $this->createResourceLoader(), + 'en_*' + ); assertThat( - $errorMessages->messageFor( - new ParamError('id', ['foo' => 'bar']), - 'fr_FR' - ), - equals(new LocalizedMessage('en_*', 'An error of type bar occurred.')) + $errorMessages->messageFor( + new ParamError('id', ['foo' => 'bar']), + 'fr_FR' + ), + equals(new LocalizedMessage('en_*', 'An error of type bar occurred.')) ); } - /** - * @test - */ + #[Test] public function returnsMessageInDefaultLocaleIfNoLocaleGiven(): void { - $errorMessages = new PropertyBasedParamErrorMessages($this->createResourceLoader(), 'en_*'); + $errorMessages = new PropertyBasedParamErrorMessages( + $this->createResourceLoader(), + 'en_*' + ); assertThat( - $errorMessages->messageFor(new ParamError('id', ['foo' => 'bar'])), - equals(new LocalizedMessage('en_*', 'An error of type bar occurred.')) + $errorMessages->messageFor(new ParamError('id', ['foo' => 'bar'])), + equals(new LocalizedMessage('en_*', 'An error of type bar occurred.')) ); } /** - * @test * @since 8.0.1 */ + #[Test] public function returnsMessageInBaseLocaleIfNoLocaleGivenAndNoSpecificLocaleMessageAvailable(): void { - $errorMessages = new PropertyBasedParamErrorMessages($this->createResourceLoader(), 'en_EN'); + $errorMessages = new PropertyBasedParamErrorMessages( + $this->createResourceLoader(), + 'en_EN' + ); assertThat( - $errorMessages->messageFor(new ParamError('id', ['foo' => 'bar'])), - equals(new LocalizedMessage('en_*', 'An error of type bar occurred.')) + $errorMessages->messageFor(new ParamError('id', ['foo' => 'bar'])), + equals(new LocalizedMessage('en_*', 'An error of type bar occurred.')) ); } - /** - * @test - */ + #[Test] public function returnsMessageInFallbackLocale(): void { assertThat( - $this->errorMessages->messageFor( - new ParamError('id', ['foo' => 'bar']), - 'fr_FR' - ), - equals(new LocalizedMessage('default', 'An error of type bar occurred.')) + $this->errorMessages->messageFor( + new ParamError('id', ['foo' => 'bar']), + 'fr_FR' + ), + equals(new LocalizedMessage('default', 'An error of type bar occurred.')) ); } - /** - * @test - */ + #[Test] public function returnsEmptyMessageForNonExistingError(): void { assertThat( - $this->errorMessages->messageFor( - new ParamError('doesNotExist'), - 'en_*' - ), - equals(new LocalizedMessage('default', '')) + $this->errorMessages->messageFor( + new ParamError('doesNotExist'), + 'en_*' + ), + equals(new LocalizedMessage('default', '')) ); } - /** - * @test - */ + #[Test] public function annotationsPresentOnConstructor(): void { $annotations = annotationsOfConstructorParameter( - 'defaultLocale', - $this->errorMessages + 'defaultLocale', + $this->errorMessages ); assertTrue($annotations->contain('Property')); assertThat( - $annotations->firstNamed('Property')->getValue(), - equals('stubbles.locale') + $annotations->firstNamed('Property')->getValue(), + equals('stubbles.locale') ); } } diff --git a/src/test/php/filter/AcceptFilterTest.php b/src/test/php/filter/AcceptFilterTest.php index 21623c3..accb4de 100644 --- a/src/test/php/filter/AcceptFilterTest.php +++ b/src/test/php/filter/AcceptFilterTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use stubbles\peer\http\AcceptHeader; use stubbles\values\Value; @@ -18,48 +21,34 @@ * Tests for stubbles\input\filter\AcceptFilter. * * @since 2.0.1 - * @group filter */ +#[Group('filter')] class AcceptFilterTest extends TestCase { - /** - * apply filter on given value - * - * @param string $acceptHeader - * @return \stubbles\peer\http\AcceptHeader - */ private function apply(?string $acceptHeader): AcceptHeader { return AcceptFilter::instance()->apply(Value::of($acceptHeader))[0]; } - /** - * @test - */ + #[Test] public function returnsEmptyAcceptHeaderWhenParamValueIsNull(): void { assertEmpty($this->apply(null)); } - /** - * @test - */ + #[Test] public function returnsEmptyAcceptHeaderWhenParamValueIsEmpty(): void { assertEmpty($this->apply('')); } - /** - * @test - */ + #[Test] public function returnsEmptyAcceptHeaderWhenParamValueIsInvalid(): void { assertEmpty($this->apply('text/plain;q=5')); } - /** - * @test - */ + #[Test] public function returnsFilledAcceptHeaderWhenParamValueIsValid(): void { assertThat($this->apply('text/plain;q=0.5'), isOfSize(1)); diff --git a/src/test/php/filter/ArrayFilterTest.php b/src/test/php/filter/ArrayFilterTest.php index 7bad3da..80b54d5 100644 --- a/src/test/php/filter/ArrayFilterTest.php +++ b/src/test/php/filter/ArrayFilterTest.php @@ -8,6 +8,11 @@ */ namespace stubbles\input\filter; +use Generator; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; + use function bovigo\assert\{ assertThat, assertNull, @@ -18,67 +23,51 @@ * Tests for stubbles\input\ValueReader::asArray(). * * @since 2.0.0 - * @group filter */ +#[Group('filter')] class ArrayFilterTest extends FilterTestBase { - /** - * @return array - */ - public static function valueResultTuples(): array + public static function valueResultTuples(): Generator { - return [[null, null], - ['', []], - ['foo', ['foo']], - [' foo ', ['foo']], - ['foo, bar', ['foo', 'bar']], - ]; + yield [null, null]; + yield ['', []]; + yield ['foo', ['foo']]; + yield [' foo ', ['foo']]; + yield ['foo, bar', ['foo', 'bar']]; } - /** - * @param mixed $value - * @param mixed $expected - * @test - * @dataProvider valueResultTuples - */ - public function value($value, $expected): void + #[Test] + #[DataProvider('valueResultTuples')] + public function value(?string $value, ?array $expected): void { assertThat($this->readParam($value)->asArray(), equals($expected)); } - /** - * @test - */ + #[Test] public function usingDifferentSeparator(): void { assertThat($this->readParam('foo|bar')->asArray('|'), equals(['foo', 'bar'])); } - /** - * @test - */ + #[Test] public function asArrayReturnsDefaultIfParamIsNullAndNotRequired(): void { $default = ['foo' => 'bar']; assertThat( - $this->readParam(null) - ->defaultingTo($default) - ->asArray(), - equals($default) + $this->readParam(null) + ->defaultingTo($default) + ->asArray(), + equals($default) ); } - /** - * @test - */ + #[Test] public function asArrayReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asArray()); } - /** - * @test - */ + #[Test] public function asArrayAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asArray(); diff --git a/src/test/php/filter/BoolFilterTest.php b/src/test/php/filter/BoolFilterTest.php index 5825d9c..2c29fef 100644 --- a/src/test/php/filter/BoolFilterTest.php +++ b/src/test/php/filter/BoolFilterTest.php @@ -7,6 +7,10 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; + use function bovigo\assert\assertFalse; use function bovigo\assert\assertNull; use function bovigo\assert\assertTrue; @@ -14,97 +18,79 @@ * Tests for stubbles\input\ValueReader::asBool(). * * @since 1.2.0 - * @group filter */ +#[Group('filter')] class BoolFilterTest extends FilterTestBase { - /** - * @test - */ + #[Test] public function filtering1AsIntReturnsTrue(): void { assertTrue($this->readParam(1)->asBool()); } - /** - * @test - */ + #[Test] public function filtering1AsStringReturnsTrue(): void { assertTrue($this->readParam('1')->asBool()); } - /** - * @test - */ + #[Test] public function filteringTrueAsStringReturnsTrue(): void { assertTrue($this->readParam('true')->asBool()); } - /** - * @test - */ + #[Test] public function filteringTrueAsBoolReturnsTrue(): void { assertTrue($this->readParam(true)->asBool()); } /** - * @test * @since 2.4.1 - * @group issue_49 */ + #[Test] + #[Group('issue_49')] public function filteringYesAsStringReturnsTrue(): void { assertTrue($this->readParam('yes')->asBool()); } - /** - * @test - */ + #[Test] public function filtering0AsIntReturnsFalse(): void { assertFalse($this->readParam(0)->asBool()); } - /** - * @test - */ + #[Test] public function filtering0AsStringReturnsFalse(): void { assertFalse($this->readParam('0')->asBool()); } - /** - * @test - */ + #[Test] public function filteringFalseAsStringReturnsFalse(): void { assertFalse($this->readParam('false')->asBool()); } - /** - * @test - */ + #[Test] public function filteringFalseAsBoolReturnsFalse(): void { assertFalse($this->readParam(false)->asBool()); } /** - * @test * @since 2.4.1 - * @group issue_49 */ + #[Test] + #[Group('issue_49')] public function filteringNoAsStringReturnsFalse(): void { assertFalse($this->readParam('no')->asBool()); } - /** - * @test - */ + #[Test] public function filteringAnyStringReturnsFalse(): void { assertFalse($this->readParam('a string')->asBool()); @@ -112,9 +98,9 @@ public function filteringAnyStringReturnsFalse(): void /** * @since 1.7.0 - * @test - * @group bug266 */ + #[Test] + #[Group('bug266')] public function asBoolReturnsDefaultIfParamIsNullAndDefaultIsNotNull(): void { assertTrue($this->readParam(null)->defaultingTo(true)->asBool()); @@ -122,8 +108,8 @@ public function asBoolReturnsDefaultIfParamIsNullAndDefaultIsNotNull(): void /** * @since 1.7.0 - * @test */ + #[Test] public function asBoolReturnsNullIfParamIsNull(): void { assertNull($this->readParam(null)->asBool()); @@ -131,9 +117,9 @@ public function asBoolReturnsNullIfParamIsNull(): void /** * @since 1.7.0 - * @test - * @group bug266 */ + #[Test] + #[Group('bug266')] public function asBoolWithFalseValueReturnsFalse(): void { assertFalse($this->readParam(0)->asBool()); @@ -141,9 +127,9 @@ public function asBoolWithFalseValueReturnsFalse(): void /** * @since 1.7.0 - * @test - * @group bug266 */ + #[Test] + #[Group('bug266')] public function asBoolWithTrueValueReturnsTrue(): void { assertTrue($this->readParam(1)->asBool()); diff --git a/src/test/php/filter/DateFilterTest.php b/src/test/php/filter/DateFilterTest.php index 6385025..d649689 100644 --- a/src/test/php/filter/DateFilterTest.php +++ b/src/test/php/filter/DateFilterTest.php @@ -7,6 +7,10 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use Generator; +use PHPUnit\Framework\Attributes\DataProviderExternal; +use PHPUnit\Framework\Attributes\Test; use stubbles\date\Date; use stubbles\input\filter\range\DateRange; @@ -21,12 +25,7 @@ */ class DateFilterTest extends FilterTestBase { - /** - * instance to test - * - * @var DateFilter - */ - private $dateFilter; + private DateFilter $dateFilter; protected function setUp(): void { @@ -34,47 +33,30 @@ protected function setUp(): void parent::setUp(); } - /** - * @return array - */ - public static function getEmptyValues(): array - { - return [[''], [null]]; - } - - /** - * @param mixed $value - * @test - * @dataProvider getEmptyValues - */ - public function emptyParamsAreReturnedAsNull($value): void + #[Test] + #[DataProviderExternal(EmptyValuesDataProvider::class, 'provideStrings')] + public function emptyParamsAreReturnedAsNull(?string $value): void { assertNull($this->dateFilter->apply($this->createParam($value))[0]); } - /** - * @test - */ + #[Test] public function validParamsAreReturnedAsDateInstance(): void { assertThat( - $this->dateFilter->apply($this->createParam('2008-09-27'))[0], - equals(new Date('2008-09-27 00:00:00')) + $this->dateFilter->apply($this->createParam('2008-09-27'))[0], + equals(new Date('2008-09-27 00:00:00')) ); } - /** - * @test - */ + #[Test] public function applyReturnsNullForInvalidDate(): void { assertNull($this->dateFilter->apply($this->createParam('invalid date'))[0]); } - /** - * @test - */ + #[Test] public function applyAddsErrorForInvalidDate(): void { $param = $this->createParam('invalid date'); @@ -84,8 +66,8 @@ public function applyAddsErrorForInvalidDate(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asDateReturnsNullIfParamIsNullAndNotRequired(): void { assertNull($this->readParam(null)->asDate()); @@ -93,23 +75,23 @@ public function asDateReturnsNullIfParamIsNullAndNotRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asDateReturnsDefaultIfParamIsNullAndNotRequired(): void { $default = Date::now(); assertThat( - $this->readParam(null) - ->defaultingTo($default) - ->asDate(), - equals($default) + $this->readParam(null) + ->defaultingTo($default) + ->asDate(), + equals($default) ); } /** * @since 2.0.0 - * @test */ + #[Test] public function asDateReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asDate()); @@ -117,8 +99,8 @@ public function asDateReturnsNullIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asDateAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asDate(); @@ -127,8 +109,8 @@ public function asDateAddsParamErrorIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asDateReturnsNullIfParamIsInvalid(): void { assertNull($this->readParam('foo')->asDate()); @@ -136,17 +118,15 @@ public function asDateReturnsNullIfParamIsInvalid(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asDateAddsParamErrorIfParamIsInvalid(): void { $this->readParam('foo')->asDate(); assertTrue($this->paramErrors->existFor('bar')); } - /** - * @test - */ + #[Test] public function asDateReturnsValidValue(): void { $date = $this->readParam('2012-03-11')->asDate(); @@ -159,24 +139,24 @@ public function asDateReturnsValidValue(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asDateReturnsNullIfParamIsOutOfRange(): void { assertNull( - $this->readParam((new Date('yesterday'))->asString()) - ->asDate(new DateRange(Date::now(), null)) + $this->readParam((new Date('yesterday'))->asString()) + ->asDate(new DateRange(Date::now(), null)) ); } /** * @since 2.0.0 - * @test */ + #[Test] public function asDateAddsParamErrorIfParamIsOutOfRange(): void { $this->readParam((new Date('yesterday'))->asString()) - ->asDate(new DateRange(Date::now(), null)); + ->asDate(new DateRange(Date::now(), null)); assertTrue($this->paramErrors->existFor('bar')); } } diff --git a/src/test/php/filter/DatespanFilterTest.php b/src/test/php/filter/DatespanFilterTest.php index e56cc51..bcdde45 100644 --- a/src/test/php/filter/DatespanFilterTest.php +++ b/src/test/php/filter/DatespanFilterTest.php @@ -7,6 +7,10 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use PHPUnit\Framework\Attributes\DataProviderExternal; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\date\Date; use stubbles\date\span\Day; use stubbles\input\filter\range\DatespanRange; @@ -18,17 +22,12 @@ /** * Tests for stubbles\input\filter\DatespanFilter. * - * @group filter * @since 4.3.0 */ +#[Group('filter')] class DatespanFilterTest extends FilterTestBase { - /** - * instance to test - * - * @var \stubbles\input\filter\DatespanFilter - */ - private $datespanFilter; + private DatespanFilter $datespanFilter; protected function setUp(): void { @@ -36,47 +35,29 @@ protected function setUp(): void parent::setUp(); } - /** - * @return array - */ - public static function getEmptyValues(): array - { - return [[''], [null]]; - } - - /** - * @param mixed $value - * @test - * @dataProvider getEmptyValues - */ - public function emptyParamsAreReturnedAsNull($value): void + #[Test] + #[DataProviderExternal(EmptyValuesDataProvider::class, 'provideStrings')] + public function emptyParamsAreReturnedAsNull(?string $value): void { assertNull($this->datespanFilter->apply($this->createParam($value))[0]); } - /** - * @test - */ + #[Test] public function validParamsAreReturnedAsDayInstance(): void { assertThat( - $this->datespanFilter->apply($this->createParam('2008-09-27'))[0], - equals(new Day('2008-09-27')) + $this->datespanFilter->apply($this->createParam('2008-09-27'))[0], + equals(new Day('2008-09-27')) ); } - /** - * @test - */ + #[Test] public function applyReturnsNullForInvalidDay(): void { - assertNull($this->datespanFilter->apply($this->createParam('invalid day'))[0]); } - /** - * @test - */ + #[Test] public function applyAddsErrorForInvalidDay(): void { $param = $this->createParam('invalid day'); @@ -84,65 +65,51 @@ public function applyAddsErrorForInvalidDay(): void assertTrue(isset($errors['DATESPAN_INVALID'])); } - /** - * @test - */ + #[Test] public function asDatespanReturnsNullIfParamIsNullAndNotRequired(): void { assertNull($this->readParam(null)->asDatespan()); } - /** - * @test - */ + #[Test] public function asDatespanReturnsDefaultIfParamIsNullAndNotRequired(): void { $default = new Day(); assertThat( - $this->readParam(null) - ->defaultingTo($default) - ->asDatespan(), - equals($default) + $this->readParam(null) + ->defaultingTo($default) + ->asDatespan(), + equals($default) ); } - /** - * @test - */ + #[Test] public function asDatespanReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asDatespan()); } - /** - * @test - */ + #[Test] public function asDatespanAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asDatespan(); assertTrue($this->paramErrors->existForWithId('bar', 'FIELD_EMPTY')); } - /** - * @test - */ + #[Test] public function asDatespanReturnsNullIfParamIsInvalid(): void { assertNull($this->readParam('foo')->asDatespan()); } - /** - * @test - */ + #[Test] public function asDatespanAddsParamErrorIfParamIsInvalid(): void { $this->readParam('foo')->asDatespan(); assertTrue($this->paramErrors->existFor('bar')); } - /** - * @test - */ + #[Test] public function asDatespanReturnsValidValue(): void { $datespan = $this->readParam('2012-03-11')->asDatespan(); @@ -153,20 +120,16 @@ public function asDatespanReturnsValidValue(): void } - /** - * @test - */ + #[Test] public function asDatespanReturnsNullIfParamIsOutOfRange(): void { assertNull( - $this->readParam('yesterday') - ->asDatespan(new DatespanRange(Date::now(), null)) + $this->readParam('yesterday') + ->asDatespan(new DatespanRange(Date::now(), null)) ); } - /** - * @test - */ + #[Test] public function asDatespanAddsParamErrorIfParamIsOutOfRange(): void { $this->readParam('yesterday') diff --git a/src/test/php/filter/DayFilterTest.php b/src/test/php/filter/DayFilterTest.php index bd7c903..7b26a65 100644 --- a/src/test/php/filter/DayFilterTest.php +++ b/src/test/php/filter/DayFilterTest.php @@ -7,6 +7,10 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use PHPUnit\Framework\Attributes\DataProviderExternal; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\date\Date; use stubbles\date\span\Day; use stubbles\input\filter\range\DatespanRange; @@ -17,17 +21,11 @@ use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\filter\DayFilter. - * - * @group filter */ +#[Group('filter')] class DayFilterTest extends FilterTestBase { - /** - * instance to test - * - * @var DayFilter - */ - private $dayFilter; + private DayFilter $dayFilter; protected function setUp(): void { @@ -35,47 +33,30 @@ protected function setUp(): void parent::setUp(); } - /** - * @return array - */ - public static function getEmptyValues(): array - { - return [[''], [null]]; - } - - /** - * @param mixed $value - * @test - * @dataProvider getEmptyValues - */ + #[Test] + #[DataProviderExternal(EmptyValuesDataProvider::class, 'provideStrings')] public function emptyParamsAreReturnedAsNull($value): void { assertNull($this->dayFilter->apply($this->createParam($value))[0]); } - /** - * @test - */ + #[Test] public function validParamsAreReturnedAsDateInstance(): void { assertThat( - $this->dayFilter->apply($this->createParam('2008-09-27'))[0], - equals(new Day('2008-09-27')) + $this->dayFilter->apply($this->createParam('2008-09-27'))[0], + equals(new Day('2008-09-27')) ); } - /** - * @test - */ + #[Test] public function applyReturnsNullForInvalidDay(): void { assertNull($this->dayFilter->apply($this->createParam('invalid day'))[0]); } - /** - * @test - */ + #[Test] public function applyAddsErrorForInvalidDay(): void { $param = $this->createParam('invalid day'); @@ -85,8 +66,8 @@ public function applyAddsErrorForInvalidDay(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asDayReturnsNullIfParamIsNullAndNotRequired(): void { assertNull($this->readParam(null)->asDay()); @@ -94,8 +75,8 @@ public function asDayReturnsNullIfParamIsNullAndNotRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asDayReturnsDefaultIfParamIsNullAndNotRequired(): void { $default = new Day(); @@ -109,8 +90,8 @@ public function asDayReturnsDefaultIfParamIsNullAndNotRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asDayReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asDay()); @@ -118,8 +99,8 @@ public function asDayReturnsNullIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asDayAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asDay(); @@ -128,8 +109,8 @@ public function asDayAddsParamErrorIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asDayReturnsNullIfParamIsInvalid(): void { assertNull($this->readParam('foo')->asDay()); @@ -137,17 +118,15 @@ public function asDayReturnsNullIfParamIsInvalid(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asDayAddsParamErrorIfParamIsInvalid(): void { $this->readParam('foo')->asDay(); assertTrue($this->paramErrors->existFor('bar')); } - /** - * @test - */ + #[Test] public function asDayReturnsValidValue(): void { $day = $this->readParam('2012-03-11')->asDay(); @@ -160,24 +139,24 @@ public function asDayReturnsValidValue(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asDayReturnsNullIfParamIsOutOfRange(): void { assertNull( - $this->readParam('yesterday') - ->asDay(new DatespanRange(Date::now(), null)) + $this->readParam('yesterday') + ->asDay(new DatespanRange(Date::now(), null)) ); } /** * @since 2.0.0 - * @test */ + #[Test] public function asDayAddsParamErrorIfParamIsOutOfRange(): void { $this->readParam('yesterday') - ->asDay(new DatespanRange(Date::now(), null)); + ->asDay(new DatespanRange(Date::now(), null)); assertTrue($this->paramErrors->existFor('bar')); } } diff --git a/src/test/php/filter/EmptyValuesDataProvider.php b/src/test/php/filter/EmptyValuesDataProvider.php new file mode 100644 index 0000000..156c108 --- /dev/null +++ b/src/test/php/filter/EmptyValuesDataProvider.php @@ -0,0 +1,23 @@ +checkdnsrr->returns(true); assertThat( - $this->readParam('http://example.org')->asExistingHttpUri($this->checkdnsrr), - equals(HttpUri::fromString('http://example.org/')) + $this->readParam('http://example.org')->asExistingHttpUri($this->checkdnsrr), + equals(HttpUri::fromString('http://example.org/')) ); } - /** - * @test - */ + #[Test] public function returnsUriWithPortIfItIsNonDefaultPort(): void { $this->checkdnsrr->returns(true); assertThat( - $this->readParam('http://example.org:45')->asExistingHttpUri($this->checkdnsrr), - equals(HttpUri::fromString('http://example.org:45/')) + $this->readParam('http://example.org:45')->asExistingHttpUri($this->checkdnsrr), + equals(HttpUri::fromString('http://example.org:45/')) ); } - /** - * @test - */ + #[Test] public function returnsNullForNull(): void { assertNull($this->readParam(null)->asExistingHttpUri()); } - /** - * @test - */ + #[Test] public function returnsNullForEmptyValue(): void { assertNull($this->readParam('')->asExistingHttpUri()); } - /** - * @test - */ + #[Test] public function returnsNullForInvalidUri(): void { - assertNull($this->readParam('ftp://foobar.de/')->asExistingHttpUri($this->checkdnsrr)); + assertNull( + $this->readParam('ftp://foobar.de/')->asExistingHttpUri($this->checkdnsrr) + ); } - /** - * @test - */ + #[Test] public function addsErrorToParamForInvalidUri(): void { $this->readParam('ftp://foobar.de/')->asExistingHttpUri($this->checkdnsrr); @@ -96,68 +88,60 @@ public function addsErrorToParamForInvalidUri(): void ); } - /** - * @test - */ + #[Test] public function doesNotActuallyCheckDnsRecordWhenUriIsInvalidAnyway(): void { $this->readParam('ftp://foobar.de/')->asExistingHttpUri($this->checkdnsrr); assertTrue(verify($this->checkdnsrr)->wasNeverCalled()); } - /** - * @test - */ + #[Test] public function returnsHttpUriIfUriHasDnsRecoed(): void { $this->checkdnsrr->returns(true); assertThat( - $this->readParam('http://stubbles.net/')->asExistingHttpUri($this->checkdnsrr), - equals(HttpUri::fromString('http://stubbles.net/')) + $this->readParam('http://stubbles.net/')->asExistingHttpUri($this->checkdnsrr), + equals(HttpUri::fromString('http://stubbles.net/')) ); } - /** - * @test - */ + #[Test] public function returnsNullIfUriHasNoDnsRecord(): void { assertNull( - $this->readParam('http://doesnotexist') - ->asExistingHttpUri($this->checkdnsrr) + $this->readParam('http://doesnotexist') + ->asExistingHttpUri($this->checkdnsrr) ); } - /** - * @test - */ + #[Test] public function addsErrorToParamIfUriHasNoDnsRecoed(): void { $this->readParam('http://doesnotexist')->asExistingHttpUri($this->checkdnsrr); assertTrue( - $this->paramErrors->existForWithId('bar', 'HTTP_URI_NOT_AVAILABLE') + $this->paramErrors->existForWithId('bar', 'HTTP_URI_NOT_AVAILABLE') ); } /** * @since 2.0.0 - * @test */ + #[Test] public function asExistingHttpUriReturnsDefaultIfParamIsNullAndNotRequired(): void { $httpUri = HttpUri::fromString('http://example.com/'); assertThat( - $this->readParam(null) - ->defaultingTo($httpUri) - ->asExistingHttpUri(), - isSameAs($httpUri) + $this->readParam(null) + ->defaultingTo($httpUri) + ->asExistingHttpUri(), + isSameAs($httpUri) ); } /** * @since 2.0.0 - * @test */ + #[Test] public function asExistingHttpUriReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asExistingHttpUri()); @@ -165,8 +149,8 @@ public function asExistingHttpUriReturnsNullIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asExistingHttpUriAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asExistingHttpUri(); diff --git a/src/test/php/filter/FilterTestBase.php b/src/test/php/filter/FilterTestBase.php index 4bef6c1..24c823d 100644 --- a/src/test/php/filter/FilterTestBase.php +++ b/src/test/php/filter/FilterTestBase.php @@ -25,9 +25,6 @@ protected function setUp(): void $this->paramErrors = new ParamErrors(); } - /** - * creates param - */ protected function createParam(mixed $value): Value { return Value::of($value); diff --git a/src/test/php/filter/FloatFilterTest.php b/src/test/php/filter/FloatFilterTest.php index 0129e06..7115581 100644 --- a/src/test/php/filter/FloatFilterTest.php +++ b/src/test/php/filter/FloatFilterTest.php @@ -7,6 +7,10 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use Generator; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use stubbles\input\filter\range\NumberRange; use function bovigo\assert\assertThat; @@ -20,55 +24,43 @@ */ class FloatFilterTest extends FilterTestBase { - /** - * @return array - */ - public static function getValueResultTuples(): array + public static function valueResultTuples(): Generator { - return [['8.4533', 8453], - ['8.4538', 8453], - ['8.45', 8450], - ['8', 8000], - [8.4533, 8453], - [8.4538, 8453], - [8.45, 8450], - [8, 8000], - [null, null], - [true, 1000], - [false, 0] - ]; + yield ['8.4533', 8453]; + yield ['8.4538', 8453]; + yield ['8.45', 8450]; + yield ['8', 8000]; + yield [8.4533, 8453]; + yield [8.4538, 8453]; + yield [8.45, 8450]; + yield [8, 8000]; + yield [null, null]; + yield [true, 1000]; + yield [false, 0]; } - /** - * @param scalar $value - * @param float $expected - * @test - * @dataProvider getValueResultTuples - */ - public function value($value, ?float $expected): void + #[Test] + #[DataProvider('valueResultTuples')] + public function value(mixed $value, ?float $expected): void { $floatFilter = new FloatFilter(); assertThat( - $floatFilter->setDecimals(3)->apply($this->createParam($value))[0], - equals($expected) + $floatFilter->setDecimals(3)->apply($this->createParam($value))[0], + equals($expected) ); } - /** - * @test - */ + #[Test] public function float(): void { $floatFilter = new FloatFilter(); assertThat( - $floatFilter->setDecimals(2)->apply($this->createParam('1.564'))[0], - equals(156) + $floatFilter->setDecimals(2)->apply($this->createParam('1.564'))[0], + equals(156) ); } - /** - * @test - */ + #[Test] public function decimalsNotSet(): void { $floatFilter = new FloatFilter(); @@ -77,8 +69,8 @@ public function decimalsNotSet(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asFloatReturnsNullIfParamIsNullAndNotRequired(): void { assertNull($this->readParam(null)->asFloat()); @@ -86,8 +78,8 @@ public function asFloatReturnsNullIfParamIsNullAndNotRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asFloatReturnsDefaultIfParamIsNullAndNotRequired(): void { assertThat($this->readParam(null)->defaultingTo(3.03)->asFloat(), equals(3.03)); @@ -95,8 +87,8 @@ public function asFloatReturnsDefaultIfParamIsNullAndNotRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asFloatReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asFloat()); @@ -104,8 +96,8 @@ public function asFloatReturnsNullIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asFloatAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asFloat(); @@ -114,8 +106,8 @@ public function asFloatAddsParamErrorIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asFloatReturnsNullIfParamIsInvalid(): void { assertNull($this->readParam(2.5)->asFloat(new NumberRange(5, null))); @@ -123,25 +115,21 @@ public function asFloatReturnsNullIfParamIsInvalid(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asFloatAddsParamErrorIfParamIsInvalid(): void { $this->readParam(2.5)->asFloat(new NumberRange(5, null)); assertTrue($this->paramErrors->existFor('bar')); } - /** - * @test - */ + #[Test] public function asFloatReturnsValidValue(): void { assertThat($this->readParam('3.13')->asFloat(), equals(3.13)); } - /** - * @test - */ + #[Test] public function asFloatReturnsValidValueUsingDecimals(): void { assertThat($this->readParam('3.13')->asFloat(null, 2), equals(313)); diff --git a/src/test/php/filter/HttpUriFilterTest.php b/src/test/php/filter/HttpUriFilterTest.php index 39a21c9..221ef1b 100644 --- a/src/test/php/filter/HttpUriFilterTest.php +++ b/src/test/php/filter/HttpUriFilterTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\peer\http\HttpUri; use function bovigo\assert\assertThat; @@ -15,99 +18,82 @@ use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\filter\HttpUriFilter. - * - * @group filter - */ + */ +#[Group('filter')] class HttpUriFilterTest extends FilterTestBase { - /** - * @test - */ + #[Test] public function returnsUriWithoutPortIfItIsDefaultPort(): void { assertThat( - $this->readParam('http://example.org')->asHttpUri(), - equals(HttpUri::fromString('http://example.org/')) + $this->readParam('http://example.org')->asHttpUri(), + equals(HttpUri::fromString('http://example.org/')) ); } - - - /** - * @test - */ + #[Test] public function returnsUriWithPortIfItIsNonDefaultPort(): void { assertThat( - $this->readParam('http://example.org:45')->asHttpUri(), - equals(HttpUri::fromString('http://example.org:45/')) + $this->readParam('http://example.org:45')->asHttpUri(), + equals(HttpUri::fromString('http://example.org:45/')) ); } - /** - * @test - */ + #[Test] public function returnsNullForNull(): void { assertNull($this->readParam(null)->asHttpUri()); } - /** - * @test - */ + #[Test] public function returnsNullForEmptyValue(): void { assertNull($this->readParam('')->asHttpUri()); } - /** - * @test - */ + #[Test] public function returnsNullForInvalidUri(): void { assertNull($this->readParam('ftp://foobar.de/')->asHttpUri()); } - /** - * @test - */ + #[Test] public function addsErrorToParamForInvalidUri(): void { $this->readParam('ftp://foobar.de/')->asHttpUri(); assertTrue( - $this->paramErrors->existForWithId('bar', 'HTTP_URI_INCORRECT') + $this->paramErrors->existForWithId('bar', 'HTTP_URI_INCORRECT') ); } - /** - * @test - */ + #[Test] public function doesNotPerformDnsCheck(): void { assertThat( - $this->readParam('http://doesnotexist')->asHttpUri(), - equals(HttpUri::fromString('http://doesnotexist')) + $this->readParam('http://doesnotexist')->asHttpUri(), + equals(HttpUri::fromString('http://doesnotexist')) ); } /** * @since 2.0.0 - * @test */ + #[Test] public function asHttpUriReturnsDefaultIfParamIsNullAndNotRequired(): void { assertThat( - $this->readParam(null) - ->defaultingTo(HttpUri::fromString('http://example.com/')) - ->asHttpUri(), - equals(HttpUri::fromString('http://example.com/')) + $this->readParam(null) + ->defaultingTo(HttpUri::fromString('http://example.com/')) + ->asHttpUri(), + equals(HttpUri::fromString('http://example.com/')) ); } /** * @since 2.0.0 - * @test */ + #[Test] public function asHttpUriReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asHttpUri()); @@ -115,8 +101,8 @@ public function asHttpUriReturnsNullIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asHttpUriAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asHttpUri(); @@ -125,8 +111,8 @@ public function asHttpUriAddsParamErrorIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asHttpUriReturnsNullIfParamIsInvalid(): void { assertNull($this->readParam('foo')->asHttpUri()); @@ -134,22 +120,20 @@ public function asHttpUriReturnsNullIfParamIsInvalid(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asHttpUriAddsParamErrorIfParamIsInvalid(): void { $this->readParam('foo')->asHttpUri(); assertTrue($this->paramErrors->existFor('bar')); } - /** - * @test - */ + #[Test] public function asHttpUriReturnsValidValue(): void { assertThat( - $this->readParam('http://example.com/')->asHttpUri(), - equals(HttpUri::fromString('http://example.com/')) + $this->readParam('http://example.com/')->asHttpUri(), + equals(HttpUri::fromString('http://example.com/')) ); } diff --git a/src/test/php/filter/IntegerFilterTest.php b/src/test/php/filter/IntegerFilterTest.php index b1ff635..2f77d7b 100644 --- a/src/test/php/filter/IntegerFilterTest.php +++ b/src/test/php/filter/IntegerFilterTest.php @@ -7,6 +7,11 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use Generator; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\input\filter\range\NumberRange; use function bovigo\assert\assertThat; @@ -15,45 +20,36 @@ use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\filter\IntegerFilter. - * - * @group filter */ +#[Group('filter')] class IntegerFilterTest extends FilterTestBase { - /** - * @return array - */ - public static function getValueResultTuples(): array + public static function valueResultTuples(): Generator { - return [[8, 8], - ['8', 8], - ['', 0], - [null, null], - [true, 1], - [false, 0], - [1.564, 1] - ]; + yield [8, 8]; + yield ['8', 8]; + yield ['', 0]; + yield [null, null]; + yield [true, 1]; + yield [false, 0]; + yield [1.564, 1]; } - /** - * @param scalar $value - * @param int $expected - * @test - * @dataProvider getValueResultTuples - */ - public function value($value, ?int $expected): void + #[Test] + #[DataProvider('valueResultTuples')] + public function value(mixed $value, ?int $expected): void { $integerFilter = IntegerFilter::instance(); assertThat( - $integerFilter->apply($this->createParam($value))[0], - equals($expected) + $integerFilter->apply($this->createParam($value))[0], + equals($expected) ); } /** * @since 2.0.0 - * @test */ + #[Test] public function asIntReturnsNullIfParamIsNullAndNotRequired(): void { assertNull($this->readParam(null)->asInt()); @@ -61,8 +57,8 @@ public function asIntReturnsNullIfParamIsNullAndNotRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asIntReturnsDefaultIfParamIsNullAndNotRequired(): void { assertThat($this->readParam(null)->defaultingTo(303)->asInt(), equals(303)); @@ -70,8 +66,8 @@ public function asIntReturnsDefaultIfParamIsNullAndNotRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asIntReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asInt()); @@ -79,8 +75,8 @@ public function asIntReturnsNullIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asIntAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asInt(); @@ -89,8 +85,8 @@ public function asIntAddsParamErrorIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asIntReturnsNullIfParamIsInvalid(): void { assertNull($this->readParam(4)->asInt(new NumberRange(5, null))); @@ -98,21 +94,17 @@ public function asIntReturnsNullIfParamIsInvalid(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asIntAddsParamErrorIfParamIsInvalid(): void { - $this->readParam(4)->asInt(new NumberRange(5, null) - ); + $this->readParam(4)->asInt(new NumberRange(5, null)); assertTrue($this->paramErrors->existFor('bar')); } - /** - * @test - */ + #[Test] public function asIntReturnsValidValue(): void { assertThat($this->readParam('313')->asInt(), equals(313)); - } } diff --git a/src/test/php/filter/JsonFilterTest.php b/src/test/php/filter/JsonFilterTest.php index a871b7a..fc73edf 100644 --- a/src/test/php/filter/JsonFilterTest.php +++ b/src/test/php/filter/JsonFilterTest.php @@ -8,6 +8,10 @@ */ namespace stubbles\input\filter; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; +use stdClass; + use function bovigo\assert\assertThat; use function bovigo\assert\assertNull; use function bovigo\assert\assertTrue; @@ -15,16 +19,12 @@ use function bovigo\assert\predicate\hasKey; /** * Tests for stubbles\input\filter\JsonFilter. - * - * @group filter - * @group json */ +#[Group('filter')] +#[Group('json')] class JsonFilterTest extends FilterTestBase { - /** - * @var JsonFilter - */ - private $jsonFilter; + private JsonFilter $jsonFilter; protected function setUp(): void { @@ -32,68 +32,56 @@ protected function setUp(): void parent::setUp(); } - /** - * @test - */ + #[Test] public function returnsNullIfParamIsNull(): void { assertNull($this->jsonFilter->apply($this->createParam(null))[0]); } - /** - * @test - */ + #[Test] public function filterValidJsonArray(): void { assertThat($this->jsonFilter->apply($this->createParam('[1]'))[0], equals([1])); } - /** - * @test - */ + #[Test] public function filterValidJsonObject(): void { $obj = new \stdClass(); $obj->id = "abc"; assertThat( - $this->jsonFilter->apply($this->createParam('{"id":"abc"}'))[0], - equals($obj) + $this->jsonFilter->apply($this->createParam('{"id":"abc"}'))[0], + equals($obj) ); } - /** - * @test - */ + #[Test] public function filterValidJsonRpc(): void { - $phpJsonObj = new \stdClass(); + $phpJsonObj = new stdClass(); $phpJsonObj->method = 'add'; $phpJsonObj->params = [1, 2]; $phpJsonObj->id = 1; assertThat( - $this->jsonFilter->apply( - $this->createParam('{"method":"add","params":[1,2],"id":1}') - )[0], - equals($phpJsonObj) + $this->jsonFilter->apply( + $this->createParam('{"method":"add","params":[1,2],"id":1}') + )[0], + equals($phpJsonObj) ); } - /** - * @test - */ + #[Test] public function returnsNullForTooBigValue(): void { assertNull( - $this->jsonFilter->apply( - $this->createParam(str_repeat("a", 20001)) - )[0] + $this->jsonFilter->apply( + $this->createParam(str_repeat("a", 20001)) + )[0] ); } - /** - * @test - */ + #[Test] public function addsErrorToParamForTooBigValue(): void { $param = $this->createParam(str_repeat("a", 20001)); @@ -101,17 +89,13 @@ public function addsErrorToParamForTooBigValue(): void assertTrue(isset($errors['JSON_INPUT_TOO_BIG'])); } - /** - * @test - */ + #[Test] public function returnsNullForInvalidJsonCurlyBraces(): void { assertNull($this->jsonFilter->apply($this->createParam('{foo]'))[0]); } - /** - * @test - */ + #[Test] public function addsErrorToParamForInvalidJsonCurlyBraces(): void { $param = $this->createParam('{foo]'); @@ -119,17 +103,13 @@ public function addsErrorToParamForInvalidJsonCurlyBraces(): void assertTrue(isset($errors['JSON_INVALID'])); } - /** - * @test - */ + #[Test] public function returnsNullForInvalidJsonBrackets(): void { assertNull($this->jsonFilter->apply($this->createParam('[foo}'))[0]); } - /** - * @test - */ + #[Test] public function addsErrorToParamForInvalidJsonBrackets(): void { $param = $this->createParam('[foo}'); @@ -137,21 +117,17 @@ public function addsErrorToParamForInvalidJsonBrackets(): void assertTrue(isset($errors['JSON_INVALID'])); } - /** - * @test - */ + #[Test] public function returnsNullForInvalidJsonStructure(): void { assertNull( - $this->jsonFilter->apply( - $this->createParam('{"foo":"bar","foo","bar"}') - )[0] + $this->jsonFilter->apply( + $this->createParam('{"foo":"bar","foo","bar"}') + )[0] ); } - /** - * @test - */ + #[Test] public function addsErrorToParamForInvalidJsonStructure(): void { $param = $this->createParam('{"foo":"bar","foo","bar"}'); @@ -160,44 +136,40 @@ public function addsErrorToParamForInvalidJsonStructure(): void } /** - * @test * @since 6.0.0 */ + #[Test] public function errorContainsErrorCode(): void { $param = $this->createParam('{"foo":"bar","foo","bar"}'); list($_, $errors) = $this->jsonFilter->apply($param); assertThat( - $errors['JSON_SYNTAX_ERROR']->details(), - hasKey('errorCode') + $errors['JSON_SYNTAX_ERROR']->details(), + hasKey('errorCode') ); } /** - * @test * @since 6.0.0 */ + #[Test] public function errorContainsErrorMessage(): void { $param = $this->createParam('{"foo":"bar","foo","bar"}'); list($_, $errors) = $this->jsonFilter->apply($param); assertThat( - $errors['JSON_SYNTAX_ERROR']->details(), - hasKey('errorMsg') + $errors['JSON_SYNTAX_ERROR']->details(), + hasKey('errorMsg') ); } - /** - * @test - */ + #[Test] public function returnsNullForInvalidJsonAlthoughPhpWouldDecodeItProperly(): void { assertNull($this->jsonFilter->apply($this->createParam('"foo"'))[0]); } - /** - * @test - */ + #[Test] public function addsErrorToParamForInvalidJsonAlthoughPhpWouldDecodeItProperly(): void { $param = $this->createParam('"foo"'); @@ -207,8 +179,8 @@ public function addsErrorToParamForInvalidJsonAlthoughPhpWouldDecodeItProperly() /** * @since 2.0.0 - * @test */ + #[Test] public function asJsonReturnsDefaultIfParamIsNullAndNotRequired(): void { $default = ['foo' => 'bar']; @@ -220,8 +192,8 @@ public function asJsonReturnsDefaultIfParamIsNullAndNotRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asJsonReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asJson()); @@ -229,8 +201,8 @@ public function asJsonReturnsNullIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asJsonAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asJson(); @@ -239,8 +211,8 @@ public function asJsonAddsParamErrorIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asJsonReturnsNullIfParamIsInvalid(): void { assertNull($this->readParam('foo')->asJson()); @@ -248,8 +220,8 @@ public function asJsonReturnsNullIfParamIsInvalid(): void /** * @since 6.0.0 - * @test */ + #[Test] public function asJsonReturnsNullIfParamIsTooLong(): void { assertNull($this->readParam(json_encode('foo'))->asJson(1)); @@ -257,17 +229,15 @@ public function asJsonReturnsNullIfParamIsTooLong(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asJsonAddsParamErrorIfParamIsInvalid(): void { $this->readParam('foo')->asJson(); assertTrue($this->paramErrors->existFor('bar')); } - /** - * @test - */ + #[Test] public function asJsonReturnsValidValue(): void { $value = ['foo', 'bar']; diff --git a/src/test/php/filter/MailFilterTest.php b/src/test/php/filter/MailFilterTest.php index bd2d2c4..6eafe95 100644 --- a/src/test/php/filter/MailFilterTest.php +++ b/src/test/php/filter/MailFilterTest.php @@ -8,6 +8,9 @@ */ namespace stubbles\input\filter; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; + use function bovigo\assert\assertThat; use function bovigo\assert\assertEmptyString; use function bovigo\assert\assertNull; @@ -15,15 +18,11 @@ use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\filter\MailFilter. - * - * @group filter */ +#[Group('filter')] class MailFilterTest extends FilterTestBase { - /** - * @var \stubbles\input\Filter - */ - private $mailFilter; + private MailFilter $mailFilter; protected function setUp(): void { @@ -31,65 +30,51 @@ protected function setUp(): void parent::setUp(); } - /** - * @test - */ + #[Test] public function returnsNullWhenValueIsNull(): void { assertNull($this->mailFilter->apply($this->createParam(null))[0]); } - /** - * @test - */ + #[Test] public function returnsNullWhenValueIsEmpty(): void { assertNull($this->mailFilter->apply($this->createParam(''))[0]); } - /** - * @test - */ + #[Test] public function returnsFilteredValue(): void { assertThat( - $this->mailFilter->apply($this->createParam('example@example.org'))[0], - equals('example@example.org') + $this->mailFilter->apply($this->createParam('example@example.org'))[0], + equals('example@example.org') ); } - /** - * @test - */ + #[Test] public function returnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asMailAddress()); } - /** - * @test - */ + #[Test] public function addsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asMailAddress(); assertTrue( - $this->paramErrors->existForWithId('bar', 'MAILADDRESS_MISSING') + $this->paramErrors->existForWithId('bar', 'MAILADDRESS_MISSING') ); } - /** - * @test - */ + #[Test] public function returnsNullWhenSpaceInValue(): void { assertNull( - $this->mailFilter->apply($this->createParam('space in@mailadre.ss'))[0] + $this->mailFilter->apply($this->createParam('space in@mailadre.ss'))[0] ); } - /** - * @test - */ + #[Test] public function addsErrorToParamWhenSpaceInValue(): void { $param = $this->createParam('space in@mailadre.ss'); @@ -97,19 +82,15 @@ public function addsErrorToParamWhenSpaceInValue(): void assertTrue(isset($errors['MAILADDRESS_CANNOT_CONTAIN_SPACES'])); } - /** - * @test - */ + #[Test] public function returnsNullWhenGermanUmlautInValue(): void { assertNull( - $this->mailFilter->apply($this->createParam('föö@mailadre.ss'))[0] + $this->mailFilter->apply($this->createParam('föö@mailadre.ss'))[0] ); } - /** - * @test - */ + #[Test] public function addsErrorToParamWhenGermanUmlautInValue(): void { $param = $this->createParam('föö@mailadre.ss'); @@ -117,19 +98,15 @@ public function addsErrorToParamWhenGermanUmlautInValue(): void assertTrue(isset($errors['MAILADDRESS_CANNOT_CONTAIN_UMLAUTS'])); } - /** - * @test - */ + #[Test] public function returnsNullWhenMoreThanOneAtCharacterInValue(): void { assertNull( - $this->mailFilter->apply($this->createParam('foo@bar@mailadre.ss'))[0] + $this->mailFilter->apply($this->createParam('foo@bar@mailadre.ss'))[0] ); } - /** - * @test - */ + #[Test] public function addsErrorToParamWhenMoreThanOneAtCharacterInValue(): void { $param = $this->createParam('foo@bar@mailadre.ss'); @@ -137,9 +114,7 @@ public function addsErrorToParamWhenMoreThanOneAtCharacterInValue(): void assertTrue(isset($errors['MAILADDRESS_MUST_CONTAIN_ONE_AT'])); } - /** - * @test - */ + #[Test] public function returnsNullWhenDotBeforeAtSign(): void { assertNull($this->mailFilter->apply( @@ -147,9 +122,7 @@ public function returnsNullWhenDotBeforeAtSign(): void )[0]); } - /** - * @test - */ + #[Test] public function addsErrorToParamWhenDotBeforeAtSign(): void { $param = $this->createParam('foo.@mailadre.ss'); @@ -157,9 +130,7 @@ public function addsErrorToParamWhenDotBeforeAtSign(): void assertTrue(isset($errors['MAILADDRESS_DOT_NEXT_TO_AT_SIGN'])); } - /** - * @test - */ + #[Test] public function returnsNullWhenDotAfterAtSign(): void { assertNull($this->mailFilter->apply( @@ -167,9 +138,7 @@ public function returnsNullWhenDotAfterAtSign(): void )[0]); } - /** - * @test - */ + #[Test] public function addsErrorToParamWhenDotAfterAtSign(): void { $param = $this->createParam('foo@.mailadre.ss'); @@ -177,19 +146,15 @@ public function addsErrorToParamWhenDotAfterAtSign(): void assertTrue(isset($errors['MAILADDRESS_DOT_NEXT_TO_AT_SIGN'])); } - /** - * @test - */ + #[Test] public function returnsNullWhenTwoFollowingDotsInValue(): void { assertNull( - $this->mailFilter->apply($this->createParam('foo..bar@mailadre.ss'))[0] + $this->mailFilter->apply($this->createParam('foo..bar@mailadre.ss'))[0] ); } - /** - * @test - */ + #[Test] public function addsErrorToParamWhenTwoFollowingDotsInValue(): void { $param = $this->createParam('foo..bar@mailadre.ss'); @@ -199,8 +164,8 @@ public function addsErrorToParamWhenTwoFollowingDotsInValue(): void /** * @since 3.0.0 - * @test */ + #[Test] public function asMailAddressReturnsEmptyStringIfParamIsNullAndNotRequired(): void { assertEmptyString($this->readParam(null)->asMailAddress()); @@ -208,22 +173,22 @@ public function asMailAddressReturnsEmptyStringIfParamIsNullAndNotRequired(): vo /** * @since 3.0.0 - * @test */ + #[Test] public function asMailAddressReturnsDefaultIfParamIsNullAndNotRequired(): void { assertThat( - $this->readParam(null) - ->defaultingTo('baz@example.org') - ->asMailAddress(), - equals('baz@example.org') + $this->readParam(null) + ->defaultingTo('baz@example.org') + ->asMailAddress(), + equals('baz@example.org') ); } /** * @since 3.0.0 - * @test */ + #[Test] public function asMailAddressReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asMailAddress()); @@ -231,20 +196,20 @@ public function asMailAddressReturnsNullIfParamIsNullAndRequired(): void /** * @since 3.0.0 - * @test */ + #[Test] public function asMailAddressAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asMailAddress(); assertTrue( - $this->paramErrors->existForWithId('bar', 'MAILADDRESS_MISSING') + $this->paramErrors->existForWithId('bar', 'MAILADDRESS_MISSING') ); } /** * @since 3.0.0 - * @test */ + #[Test] public function asStringReturnsNullIfParamIsInvalid(): void { assertNull($this->readParam('foo')->asMailAddress()); @@ -252,8 +217,8 @@ public function asStringReturnsNullIfParamIsInvalid(): void /** * @since 3.0.0 - * @test */ + #[Test] public function asMailAddressAddsParamErrorIfParamIsInvalid(): void { $this->readParam('foo')->asMailAddress(); @@ -262,13 +227,13 @@ public function asMailAddressAddsParamErrorIfParamIsInvalid(): void /** * @since 3.0.0 - * @test */ + #[Test] public function asMailAddressReturnsValidValue(): void { assertThat( - $this->readParam('foo@example.org')->asMailAddress(), - equals('foo@example.org') + $this->readParam('foo@example.org')->asMailAddress(), + equals('foo@example.org') ); } } diff --git a/src/test/php/filter/MonthFilterTest.php b/src/test/php/filter/MonthFilterTest.php index fd38530..cc17e48 100644 --- a/src/test/php/filter/MonthFilterTest.php +++ b/src/test/php/filter/MonthFilterTest.php @@ -7,6 +7,10 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use PHPUnit\Framework\Attributes\DataProviderExternal; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\date\Date; use stubbles\date\span\Month; use stubbles\input\filter\range\DatespanRange; @@ -18,15 +22,12 @@ /** * Tests for stubbles\input\filter\MonthFilter. * - * @group filter * @since 2.5.1 */ +#[Group('filter')] class MonthFilterTest extends FilterTestBase { - /** - * @var MonthFilter - */ - private $monthFilter; + private MonthFilter $monthFilter; protected function setUp(): void { @@ -34,48 +35,29 @@ protected function setUp(): void parent::setUp(); } - /** - * @return array - */ - public static function getEmptyValues(): array - { - return [[''], - [null] - ]; - } - - /** - * @param scalar $value - * @test - * @dataProvider getEmptyValues - */ - public function emptyParamsAreReturnedAsNull($value): void + #[Test] + #[DataProviderExternal(EmptyValuesDataProvider::class, 'provideStrings')] + public function emptyParamsAreReturnedAsNull(?string $value): void { assertNull($this->monthFilter->apply($this->createParam($value))[0]); } - /** - * @test - */ + #[Test] public function validParamsAreReturnedAsMonthInstance(): void { assertThat( - $this->monthFilter->apply($this->createParam('2008-09-27'))[0], - equals(new Month(2008, 9)) + $this->monthFilter->apply($this->createParam('2008-09-27'))[0], + equals(new Month(2008, 9)) ); } - /** - * @test - */ + #[Test] public function applyReturnsNullForInvalidMonth(): void { assertNull($this->monthFilter->apply($this->createParam('invalid day'))[0]); } - /** - * @test - */ + #[Test] public function applyAddsErrorForInvalidDay(): void { $param = $this->createParam('invalid day'); @@ -83,65 +65,51 @@ public function applyAddsErrorForInvalidDay(): void assertTrue(isset($errors['MONTH_INVALID'])); } - /** - * @test - */ + #[Test] public function asMonthReturnsNullIfParamIsNullAndNotRequired(): void { assertNull($this->readParam(null)->asMonth()); } - /** - * @test - */ + #[Test] public function asMonthReturnsDefaultIfParamIsNullAndNotRequired(): void { $default = new Month(); assertThat( - $this->readParam(null) - ->defaultingTo($default) - ->asMonth(), - equals($default) + $this->readParam(null) + ->defaultingTo($default) + ->asMonth(), + equals($default) ); } - /** - * @test - */ + #[Test] public function asMonthReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asMonth()); } - /** - * @test - */ + #[Test] public function asMonthAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asMonth(); assertTrue($this->paramErrors->existForWithId('bar', 'FIELD_EMPTY')); } - /** - * @test - */ + #[Test] public function asMonthReturnsNullIfParamIsInvalid(): void { assertNull($this->readParam('foo')->asMonth()); } - /** - * @test - */ + #[Test] public function asDayAddsParamErrorIfParamIsInvalid(): void { $this->readParam('foo')->asMonth(); assertTrue($this->paramErrors->existFor('bar')); } - /** - * @test - */ + #[Test] public function asMonthReturnsValidValue(): void { $month = $this->readParam('2012-03-11')->asMonth(); @@ -152,24 +120,20 @@ public function asMonthReturnsValidValue(): void } - /** - * @test - */ + #[Test] public function asMonthReturnsNullIfParamIsOutOfRange(): void { assertNull( - $this->readParam((new Month())->asString()) - ->asMonth(new DatespanRange(new Date('tomorrow'), null)) + $this->readParam((new Month())->asString()) + ->asMonth(new DatespanRange(new Date('tomorrow'), null)) ); } - /** - * @test - */ + #[Test] public function asMonthAddsParamErrorIfParamIsOutOfRange(): void { $this->readParam((new Month())->asString()) - ->asMonth(new DatespanRange(new Date('tomorrow'), null)); + ->asMonth(new DatespanRange(new Date('tomorrow'), null)); assertTrue($this->paramErrors->existFor('bar')); } } diff --git a/src/test/php/filter/PasswordFilterTest.php b/src/test/php/filter/PasswordFilterTest.php index 0ebaca7..d85b786 100644 --- a/src/test/php/filter/PasswordFilterTest.php +++ b/src/test/php/filter/PasswordFilterTest.php @@ -7,7 +7,11 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use BadMethodCallException; +use bovigo\callmap\ClassProxy; use bovigo\callmap\NewInstance; +use PHPUnit\Framework\Attributes\Test; use stubbles\values\Secret; use function bovigo\assert\assertThat; @@ -23,14 +27,8 @@ */ class PasswordFilterTest extends FilterTestBase { - /** - * @var PasswordFilter - */ - private $passwordFilter; - /** - * @var PasswordChecker&\bovigo\callmap\ClassProxy - */ - private $passwordChecker; + private PasswordFilter $passwordFilter; + private PasswordChecker&ClassProxy $passwordChecker; protected function setUp(): void { @@ -39,73 +37,57 @@ protected function setUp(): void parent::setUp(); } - /** - * @param string $expectedPassword - * @param Secret $actualPassword - */ - private function assertPasswordEquals(string $expectedPassword, Secret $actualPassword = null): void - { - $actual = $actualPassword !== null ? $actualPassword->unveil() : null; - assertThat($actual, equals($expectedPassword)); + private function assertPasswordEquals( + string $expectedPassword, + Secret $actualPassword = null + ): void { + assertThat($actualPassword?->unveil(), equals($expectedPassword)); } - /** - * @test - */ + #[Test] public function returnsValueWhenCheckerDoesNotObject(): void { $this->passwordChecker->returns(['check' => []]); $this->assertPasswordEquals( - '425%$%"�$%t 32', - $this->passwordFilter->apply($this->createParam('425%$%"�$%t 32'))[0] + '425%$%"�$%t 32', + $this->passwordFilter->apply($this->createParam('425%$%"�$%t 32'))[0] ); } - /** - * @test - */ + #[Test] public function returnsNullForNull(): void { assertNull($this->passwordFilter->apply($this->createParam(null))[0]); verify($this->passwordChecker, 'check')->wasNeverCalled(); - } - /** - * @test - */ + #[Test] public function returnsNullForEmptyString(): void { assertNull($this->passwordFilter->apply($this->createParam(''))[0]); verify($this->passwordChecker, 'check')->wasNeverCalled(); } - /** - * @test - */ + #[Test] public function returnsPasswordIfBothArrayValuesAreEqual(): void { $this->passwordChecker->returns(['check' => []]); $this->assertPasswordEquals( - 'foo', - $this->passwordFilter->apply($this->createParam(['foo', 'foo']))[0] + 'foo', + $this->passwordFilter->apply($this->createParam(['foo', 'foo']))[0] ); } - /** - * @test - */ + #[Test] public function returnsNullIfBothArrayValuesAreDifferent(): void { assertNull( - $this->passwordFilter->apply($this->createParam(['foo', 'bar']))[0] + $this->passwordFilter->apply($this->createParam(['foo', 'bar']))[0] ); verify($this->passwordChecker, 'check')->wasNeverCalled(); } - /** - * @test - */ + #[Test] public function addsErrorToParamWhenBothArrayValuesAreDifferent(): void { $param = $this->createParam(['foo', 'bar']); @@ -114,24 +96,20 @@ public function addsErrorToParamWhenBothArrayValuesAreDifferent(): void verify($this->passwordChecker, 'check')->wasNeverCalled(); } - /** - * @test - */ + #[Test] public function returnsNullIfCheckerReportsErrors(): void { $this->passwordChecker->returns( - ['check' => ['PASSWORD_TOO_SHORT' => ['minLength' => 8]]] + ['check' => ['PASSWORD_TOO_SHORT' => ['minLength' => 8]]] ); assertNull($this->passwordFilter->apply($this->createParam('bar'))[0]); } - /** - * @test - */ + #[Test] public function addsErrorToParamWhenValueIsNotAllowed(): void { $this->passwordChecker->returns( - ['check' => ['PASSWORD_TOO_SHORT' => ['minLength' => 8]]] + ['check' => ['PASSWORD_TOO_SHORT' => ['minLength' => 8]]] ); $param = $this->createParam('bar'); list($_, $errors) = $this->passwordFilter->apply($param); @@ -140,33 +118,31 @@ public function addsErrorToParamWhenValueIsNotAllowed(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asPasswordReturnsNullIfParamIsNullAndRequired(): void { assertNull( - $this->readParam(null) - ->required() - ->asPassword($this->passwordChecker) + $this->readParam(null) + ->required() + ->asPassword($this->passwordChecker) ); } - /** - * @test - */ + #[Test] public function asPasswordWithDefaultValueThrowsBadMethodCallException(): void { expect(function() { - $this->readParam(null) - ->defaultingTo('secret') - ->asPassword($this->passwordChecker); - })->throws(\BadMethodCallException::class); + $this->readParam(null) + ->defaultingTo('secret') + ->asPassword($this->passwordChecker); + })->throws(BadMethodCallException::class); } /** * @since 2.0.0 - * @test */ + #[Test] public function asPasswordAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asPassword($this->passwordChecker); @@ -175,38 +151,36 @@ public function asPasswordAddsParamErrorIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asPasswordReturnsNullIfParamIsInvalid(): void { $this->passwordChecker->returns( - ['check' => ['PASSWORD_TOO_SHORT' => ['minLength' => 8]]] + ['check' => ['PASSWORD_TOO_SHORT' => ['minLength' => 8]]] ); assertNull($this->readParam('foo')->asPassword($this->passwordChecker)); } /** * @since 2.0.0 - * @test */ + #[Test] public function asPasswordAddsParamErrorIfParamIsInvalid(): void { $this->passwordChecker->returns( - ['check' => ['PASSWORD_TOO_SHORT' => ['minLength' => 8]]] + ['check' => ['PASSWORD_TOO_SHORT' => ['minLength' => 8]]] ); $this->readParam('foo')->asPassword($this->passwordChecker); assertTrue($this->paramErrors->existFor('bar')); } - /** - * @test - */ + #[Test] public function asPasswordReturnsValidValue(): void { $this->passwordChecker->returns(['check' => []]); $this->assertPasswordEquals( - 'abcde', - $this->readParam('abcde')->asPassword($this->passwordChecker) + 'abcde', + $this->readParam('abcde')->asPassword($this->passwordChecker) ); } } diff --git a/src/test/php/filter/RangeFilterTest.php b/src/test/php/filter/RangeFilterTest.php index 4dcacce..e3c8a4e 100644 --- a/src/test/php/filter/RangeFilterTest.php +++ b/src/test/php/filter/RangeFilterTest.php @@ -7,7 +7,11 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use bovigo\callmap\ClassProxy; use bovigo\callmap\NewInstance; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\input\filter\range\Range; use stubbles\values\Value; @@ -18,23 +22,13 @@ use function bovigo\callmap\verify; /** * Tests for stubbles\input\filter\RangeFilter. - * - * @group filter */ +#[Group('filter')] class RangeFilterTest extends FilterTestBase { - /** - * @var RangeFilter - */ - private $rangeFilter; - /** - * @var NumberFilter&\bovigo\callmap\ClassProxy - */ - private $filter; - /** - * @var Range&\bovigo\callmap\ClassProxy - */ - private $range; + private RangeFilter $rangeFilter; + private NumberFilter&ClassProxy $filter; + private Range&ClassProxy $range; protected function setUp(): void { @@ -43,22 +37,14 @@ protected function setUp(): void $this->rangeFilter = new RangeFilter($this->filter, $this->range); } - /** - * creates param - * - * @param mixed $value - * @return Value - */ - protected function createParam($value): Value + protected function createParam(mixed $value): Value { $param = parent::createParam($value); $this->filter->returns(['apply' => [$value, []]]); return $param; } - /** - * @test - */ + #[Test] public function returnsNullIfDecoratedFilterReturnsNull(): void { assertNull($this->rangeFilter->apply($this->createParam(null))[0]); @@ -66,29 +52,25 @@ public function returnsNullIfDecoratedFilterReturnsNull(): void verify($this->range, 'errorsOf')->wasNeverCalled(); } - /** - * @test - */ + #[Test] public function returnsValueIfInRange(): void { $this->range->returns(['contains' => true]); assertThat( - $this->rangeFilter->apply($this->createParam(303))[0], - equals(303) + $this->rangeFilter->apply($this->createParam(303))[0], + equals(303) ); verify($this->range, 'errorsOf')->wasNeverCalled(); } - /** - * @test - */ + #[Test] public function returnsNullIfValueNotInRange(): void { $param = $this->createParam(303); $this->range->returns([ - 'contains' => false, - 'allowsTruncate' => false, - 'errorsOf' => ['LOWER_BORDER_VIOLATION' => []] + 'contains' => false, + 'allowsTruncate' => false, + 'errorsOf' => ['LOWER_BORDER_VIOLATION' => []] ]); list($result, $errors) = $this->rangeFilter->apply($param); assertNull($result); @@ -96,20 +78,20 @@ public function returnsNullIfValueNotInRange(): void } /** - * @test * @since 2.3.1 - * @group issue41 */ + #[Test] + #[Group('issue41')] public function returnsTruncatedValueIfValueAboveMaxBorderAndTruncateAllowed(): void { $this->range->returns([ - 'contains' => false, - 'allowsTruncate' => true, - 'truncateToMaxBorder' => 'foo' + 'contains' => false, + 'allowsTruncate' => true, + 'truncateToMaxBorder' => 'foo' ]); assertThat( - $this->rangeFilter->apply($this->createParam('foobar'))[0], - equals('foo') + $this->rangeFilter->apply($this->createParam('foobar'))[0], + equals('foo') ); verify($this->range, 'errorsOf')->wasNeverCalled(); } diff --git a/src/test/php/filter/SecretFilterTest.php b/src/test/php/filter/SecretFilterTest.php index 0144067..d037125 100644 --- a/src/test/php/filter/SecretFilterTest.php +++ b/src/test/php/filter/SecretFilterTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\input\filter\range\SecretMinLength; use stubbles\values\Secret; @@ -18,15 +21,12 @@ /** * Tests for stubbles\input\filter\SecretFilter. * - * @group filter * @since 3.0.0 */ +#[Group('filter')] class SecretFilterTest extends FilterTestBase { - /** - * @var SecretFilter - */ - private $secretFilter; + private SecretFilter $secretFilter; protected function setUp(): void { @@ -40,98 +40,78 @@ private function assertSecretEquals(?string $expected, Secret $actualSecret = nu assertThat($actual, equals($expected)); } - /** - * @test - */ + #[Test] public function returnsNullSecretWhenParamIsNull(): void { $this->assertSecretEquals( - null, - $this->secretFilter->apply($this->createParam(null))[0] + null, + $this->secretFilter->apply($this->createParam(null))[0] ); } - /** - * @test - */ + #[Test] public function returnsNullSecretWhenParamIsEmptyString(): void { $this->assertSecretEquals( - null, - $this->secretFilter->apply($this->createParam(''))[0] + null, + $this->secretFilter->apply($this->createParam(''))[0] ); } - /** - * @test - */ + #[Test] public function asSecretReturnsNullSecretIfParamIsNullAndNotRequired(): void { assertNull($this->readParam(null)->asSecret()); } - /** - * @test - */ + #[Test] public function asSecretReturnsDefaultIfParamIsNullAndNotRequired(): void { $this->assertSecretEquals( - 'baz', - $this->readParam(null) - ->defaultingTo(Secret::create('baz')) - ->asSecret() + 'baz', + $this->readParam(null) + ->defaultingTo(Secret::create('baz')) + ->asSecret() ); } - /** - * @test - */ + #[Test] public function asSecretThrowsLogicExceptionWhenDefaultValueNoInstanceOfSecret(): void { expect(function() { - $this->readParam(null)->defaultingTo('baz')->asSecret(); + $this->readParam(null)->defaultingTo('baz')->asSecret(); })->throws(\LogicException::class); } - /** - * @test - */ + #[Test] public function asSecretReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asSecret()); } - /** - * @test - */ + #[Test] public function asSecretAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asSecret(); assertTrue($this->paramErrors->existForWithId('bar', 'FIELD_EMPTY')); } - /** - * @test - */ + #[Test] public function asSecretReturnsNullIfParamIsInvalid(): void { assertNull( - $this->readParam('foo')->asSecret(new SecretMinLength(5)) + $this->readParam('foo')->asSecret(new SecretMinLength(5)) ); } - /** - * @test - */ + #[Test] public function asSecretAddsParamErrorIfParamIsInvalid(): void { $this->readParam('foo')->asSecret(new SecretMinLength(5)); assertTrue($this->paramErrors->existFor('bar')); } - /** - * @test - */ + #[Test] public function asSecretReturnsValidValue(): void { $secret = $this->readParam('foo')->asSecret(); diff --git a/src/test/php/filter/SimplePasswordCheckerTest.php b/src/test/php/filter/SimplePasswordCheckerTest.php index 55c9765..c0bcfc6 100644 --- a/src/test/php/filter/SimplePasswordCheckerTest.php +++ b/src/test/php/filter/SimplePasswordCheckerTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use stubbles\values\Secret; @@ -16,70 +19,59 @@ /** * Tests for stubbles\input\filter\SimplePasswordChecker. * - * @group filter * @since 3.0.0 */ +#[Group('filter')] class SimplePasswordCheckerTest extends TestCase { - /** - * @var SimplePasswordChecker - */ - private $simplePasswordChecker; + private SimplePasswordChecker $simplePasswordChecker; protected function setUp(): void { $this->simplePasswordChecker = SimplePasswordChecker::create(); } - /** - * @test - */ + #[Test] public function doesNotReportErrorsWithDefaultValuesAndSatisfyingPassword(): void { assertEmptyArray( - $this->simplePasswordChecker->check(Secret::create('topsecret')) + $this->simplePasswordChecker->check(Secret::create('topsecret')) ); } - /** - * @test - */ + #[Test] public function reportsErrorsWithDefaultValuesAndNonSatisfyingPassword(): void { assertThat( - $this->simplePasswordChecker->check(Secret::create('ooo')), - equals([ - 'PASSWORD_TOO_SHORT' => ['minLength' => SimplePasswordChecker::DEFAULT_MINLENGTH], - 'PASSWORD_TOO_LESS_DIFF_CHARS' => ['minDiff' => SimplePasswordChecker::DEFAULT_MIN_DIFF_CHARS] - ]) + $this->simplePasswordChecker->check(Secret::create('ooo')), + equals([ + 'PASSWORD_TOO_SHORT' => ['minLength' => SimplePasswordChecker::DEFAULT_MINLENGTH], + 'PASSWORD_TOO_LESS_DIFF_CHARS' => ['minDiff' => SimplePasswordChecker::DEFAULT_MIN_DIFF_CHARS] + ]) ); } - /** - * @test - */ + #[Test] public function reportsErrorsWithChangedValuesAndNonSatisfyingPassword(): void { assertThat( - $this->simplePasswordChecker->minLength(10) - ->minDiffChars(8) - ->check(Secret::create('topsecret')), - equals([ - 'PASSWORD_TOO_SHORT' => ['minLength' => 10], - 'PASSWORD_TOO_LESS_DIFF_CHARS' => ['minDiff' => 8] - ]) + $this->simplePasswordChecker->minLength(10) + ->minDiffChars(8) + ->check(Secret::create('topsecret')), + equals([ + 'PASSWORD_TOO_SHORT' => ['minLength' => 10], + 'PASSWORD_TOO_LESS_DIFF_CHARS' => ['minDiff' => 8] + ]) ); } - /** - * @test - */ + #[Test] public function reportsErrorsWithDisallowedValues(): void { assertThat( - $this->simplePasswordChecker->disallowValues(['topsecret']) - ->check(Secret::create('topsecret')), - equals(['PASSWORD_DISALLOWED' => []]) + $this->simplePasswordChecker->disallowValues(['topsecret']) + ->check(Secret::create('topsecret')), + equals(['PASSWORD_DISALLOWED' => []]) ); } } diff --git a/src/test/php/filter/StringFilterTest.php b/src/test/php/filter/StringFilterTest.php index c49e603..d8968f7 100644 --- a/src/test/php/filter/StringFilterTest.php +++ b/src/test/php/filter/StringFilterTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\input\filter\range\StringLength; use function bovigo\assert\assertThat; @@ -16,15 +19,11 @@ use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\filter\StringFilter. - * - * @group filter */ +#[Group('filter')] class StringFilterTest extends FilterTestBase { - /** - * @var StringFilter - */ - private $stringFilter; + private StringFilter $stringFilter; protected function setUp(): void { @@ -32,70 +31,58 @@ protected function setUp(): void parent::setUp(); } - /** - * @test - */ + #[Test] public function returnsEmptyStringWhenParamIsNull(): void { assertEmptyString($this->stringFilter->apply($this->createParam(null))[0]); } - /** - * @test - */ + #[Test] public function returnsEmptyStringWhenParamIsEmptyString(): void { assertEmptyString($this->stringFilter->apply($this->createParam(''))[0]); } - /** - * @test - */ + #[Test] public function removesTags(): void { assertThat( - $this->stringFilter->apply($this->createParam("kkk"))[0], - equals("kkk") + $this->stringFilter->apply($this->createParam("kkk"))[0], + equals("kkk") ); } - /** - * @test - */ + #[Test] public function removesSlashes(): void { assertThat( - $this->stringFilter->apply($this->createParam("\'kkk"))[0], - equals("'kkk") + $this->stringFilter->apply($this->createParam("\'kkk"))[0], + equals("'kkk") ); } - /** - * @test - */ + #[Test] public function removesCarriageReturn(): void { assertThat( - $this->stringFilter->apply($this->createParam("cde\rkkk"))[0], - equals("cdekkk") + $this->stringFilter->apply($this->createParam("cde\rkkk"))[0], + equals("cdekkk") ); } - /** - * @test - */ + #[Test] public function removesLineBreaks(): void { assertThat( - $this->stringFilter->apply($this->createParam("ab\ncde\nkkk"))[0], - equals("abcdekkk") + $this->stringFilter->apply($this->createParam("ab\ncde\nkkk"))[0], + equals("abcdekkk") ); } /** * @since 2.0.0 - * @test */ + #[Test] public function asStringReturnsEmptyStringIfParamIsNullAndNotRequired(): void { assertEmptyString($this->readParam(null)->asString()); @@ -103,20 +90,20 @@ public function asStringReturnsEmptyStringIfParamIsNullAndNotRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asStringReturnsDefaultIfParamIsNullAndNotRequired(): void { assertThat( - $this->readParam(null)->defaultingTo('baz')->asString(), - equals('baz') + $this->readParam(null)->defaultingTo('baz')->asString(), + equals('baz') ); } /** * @since 2.0.0 - * @test */ + #[Test] public function asStringReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asString()); @@ -124,8 +111,8 @@ public function asStringReturnsNullIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asStringAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asString(); @@ -134,8 +121,8 @@ public function asStringAddsParamErrorIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asStringReturnsNullIfParamIsInvalid(): void { assertNull( @@ -145,17 +132,15 @@ public function asStringReturnsNullIfParamIsInvalid(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asStringAddsParamErrorIfParamIsInvalid(): void { $this->readParam('foo')->asString(new StringLength(5, null)); assertTrue($this->paramErrors->existFor('bar')); } - /** - * @test - */ + #[Test] public function asStringReturnsValidValue(): void { assertThat($this->readParam('foo')->asString(), equals('foo')); diff --git a/src/test/php/filter/TextFilterTest.php b/src/test/php/filter/TextFilterTest.php index 33edaf6..921c5f1 100644 --- a/src/test/php/filter/TextFilterTest.php +++ b/src/test/php/filter/TextFilterTest.php @@ -7,6 +7,11 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use Generator; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\input\filter\range\StringLength; use function bovigo\assert\assertThat; @@ -16,15 +21,11 @@ use function bovigo\assert\predicate\equals; /** * Tests for stubbles\input\filter\TextFilter. - * - * @group filter */ +#[Group('filter')] class TextFilterTest extends FilterTestBase { - /** - * @var TextFilter - */ - private $textFilter; + private TextFilter $textFilter; protected function setUp(): void { @@ -32,17 +33,13 @@ protected function setUp(): void parent::setUp(); } - /** - * @test - */ + #[Test] public function returnsEmptyStringWhenParamIsNull(): void { assertEmptyString($this->textFilter->apply($this->createParam(null))[0]); } - /** - * @test - */ + #[Test] public function returnsEmptyStringWhenParamIsEmptyString(): void { assertEmptyString($this->textFilter->apply($this->createParam(''))[0]); @@ -51,69 +48,65 @@ public function returnsEmptyStringWhenParamIsEmptyString(): void /** * @return array */ - public static function allowedTags(): array + public static function allowedTags(): Generator { - return [ - [[], 'this is bold and cursive and underlined with a link'], - [['b', 'i'], 'this is bold and cursive and underlined with a link'], - [['b', 'i', 'a'], 'this is bold and cursive and underlined with a link'] + yield [[], 'bold and cursive and underlined with link']; + yield [ + ['b', 'i'], + 'bold and cursive and underlined with link' + ]; + yield [ + ['b', 'i', 'a'], + 'bold and cursive and underlined with link' ]; } /** * @param string[] $allowedTags - * @param string $expected - * @test - * @dataProvider allowedTags */ + #[Test] + #[DataProvider('allowedTags')] public function removesTags(array $allowedTags, string $expected): void { + $value = 'bold and cursive and underlined with link'; assertThat( - $this->textFilter->allowTags($allowedTags) - ->apply($this->createParam( - 'this is bold and cursive and underlined with a link' - ))[0], - equals($expected) + $this->textFilter->allowTags($allowedTags) + ->apply($this->createParam($value))[0], + equals($expected) ); } - /** - * @test - */ + #[Test] public function removesSlashes(): void { assertThat( - $this->textFilter->apply($this->createParam("\'kkk"))[0], - equals("'kkk") + $this->textFilter->apply($this->createParam("\'kkk"))[0], + equals("'kkk") ); } - /** - * @test - */ + #[Test] public function removesCarriageReturn(): void { assertThat( - $this->textFilter->apply($this->createParam("cde\rkkk"))[0], - equals("cdekkk") + $this->textFilter->apply($this->createParam("cde\rkkk"))[0], + equals("cdekkk") ); } - /** - * @test - */ + #[Test] public function doesNotRemoveLineBreaks(): void { assertThat( - $this->textFilter->apply($this->createParam("ab\ncde\nkkk"))[0], - equals("ab\ncde\nkkk") + $this->textFilter->apply($this->createParam("ab\ncde\nkkk"))[0], + equals("ab\ncde\nkkk") ); } /** * @since 2.0.0 - * @test */ + #[Test] public function asTextReturnsEmptyStringIfParamIsNullAndNotRequired(): void { assertEmptyString($this->readParam(null)->asText()); @@ -121,20 +114,20 @@ public function asTextReturnsEmptyStringIfParamIsNullAndNotRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asTextReturnsDefaultIfParamIsNullAndNotRequired(): void { assertThat( - $this->readParam(null)->defaultingTo('baz')->asText(), - equals('baz') + $this->readParam(null)->defaultingTo('baz')->asText(), + equals('baz') ); } /** * @since 2.0.0 - * @test */ + #[Test] public function asTextReturnsNullIfParamIsNullAndRequired(): void { assertNull($this->readParam(null)->required()->asText()); @@ -142,8 +135,8 @@ public function asTextReturnsNullIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asTextAddsParamErrorIfParamIsNullAndRequired(): void { $this->readParam(null)->required()->asText(); @@ -152,37 +145,33 @@ public function asTextAddsParamErrorIfParamIsNullAndRequired(): void /** * @since 2.0.0 - * @test */ + #[Test] public function asTextReturnsNullIfParamIsInvalid(): void { assertNull( - $this->readParam('foo')->asText(new StringLength(5, null)) + $this->readParam('foo')->asText(new StringLength(5, null)) ); } /** * @since 2.0.0 - * @test */ + #[Test] public function asTextAddsParamErrorIfParamIsInvalid(): void { $this->readParam('foo')->asText(new StringLength(5, null)); assertTrue($this->paramErrors->existFor('bar')); } - /** - * @test - */ + #[Test] public function asTextReturnsValidValue(): void { assertThat($this->readParam('foo')->asText(), equals('foo')); } - /** - * @test - */ + #[Test] public function asTextWithAllowedTagsReturnsValidValue(): void { assertThat($this->readParam('foo')->asText(null, ['b']), equals('foo')); diff --git a/src/test/php/filter/WeekFilterTest.php b/src/test/php/filter/WeekFilterTest.php index 0c1a3e6..356b2e0 100644 --- a/src/test/php/filter/WeekFilterTest.php +++ b/src/test/php/filter/WeekFilterTest.php @@ -7,6 +7,9 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter; + +use PHPUnit\Framework\Attributes\DataProviderExternal; +use PHPUnit\Framework\Attributes\Test; use stubbles\date\Date; use stubbles\date\span\Week; use stubbles\input\filter\range\DatespanRange; @@ -23,10 +26,7 @@ */ class WeekFilterTest extends FilterTestBase { - /** - * @var \stubbles\input\filter\WeekFilter - */ - private $weekFilter; + private WeekFilter $weekFilter; protected function setUp(): void { @@ -34,22 +34,9 @@ protected function setUp(): void parent::setUp(); } - /** - * @return array - */ - public static function getEmptyValues(): array - { - return [[''], - [null] - ]; - } - - /** - * @param scalar $value - * @test - * @dataProvider getEmptyValues - */ - public function emptyParamsAreReturnedAsNull($value): void + #[Test] + #[DataProviderExternal(EmptyValuesDataProvider::class, 'provideStrings')] + public function emptyParamsAreReturnedAsNull(?string $value): void { assertNull($this->weekFilter->apply($this->createParam($value))[0]); } diff --git a/src/test/php/filter/range/DateRangeTest.php b/src/test/php/filter/range/DateRangeTest.php index 2285951..ae91949 100644 --- a/src/test/php/filter/range/DateRangeTest.php +++ b/src/test/php/filter/range/DateRangeTest.php @@ -7,6 +7,11 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter\range; + +use Generator; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use stubbles\date\Date; use PHPUnit\Framework\TestCase; @@ -20,148 +25,120 @@ * Tests for stubbles\input\filter\range\DateRange. * * @since 2.0.0 - * @group filter - * @group filter_range */ +#[Group('filter')] +#[Group('filter_range')] class DateRangeTest extends TestCase { - /** - * instance to test - * - * @var DateRange - */ - private $dateRange; + private DateRange $dateRange; protected function setUp(): void { $this->dateRange = new DateRange('2012-03-17', '2012-03-19'); } - /** - * @return array - */ - public static function outOfRangeValues(): array + public static function outOfRangeValues(): Generator { - return [ - ['2012-03-16'], - ['2012-03-20'] - ]; + yield ['2012-03-16']; + yield ['2012-03-20']; } - /** - * @test - * @dataProvider outOfRangeValues - */ + #[Test] + #[DataProvider('outOfRangeValues')] public function valueOutOfRangeIsNotContainedInRange(string $value): void { assertFalse($this->dateRange->contains($value)); } - /** - * @return array - */ - public static function withinRangeValues(): array + public static function withinRangeValues(): Generator { - return [ - ['2012-03-17'], - ['2012-03-18'], - ['2012-03-19'] - ]; + yield ['2012-03-17']; + yield ['2012-03-18']; + yield ['2012-03-19']; } - /** - * @test - * @dataProvider withinRangeValues - */ + #[Test] + #[DataProvider('withinRangeValues')] public function valueWithinRangeIsContainedInRange(string $value): void { assertTrue($this->dateRange->contains($value)); } - /** - * @test - */ + #[Test] public function rangeContainsLowValuesIfMinValueIsNull(): void { $numberRange = new DateRange(null, '2012-03-19'); assertTrue($numberRange->contains(1)); } - /** - * @test - */ + #[Test] public function rangeContainsHighValuesIfMaxValueIsNull(): void { $numberRange = new DateRange('2012-03-17', null); assertTrue($numberRange->contains(PHP_INT_MAX)); } - /** - * @return array - */ - public static function ranges(): array + public static function ranges(): Generator { - return [ - [new DateRange('2012-03-17', '2012-03-19')], - [new DateRange(null, '2012-03-19')], - [new DateRange('2012-03-17', null)] - ]; + yield [new DateRange('2012-03-17', '2012-03-19')]; + yield [new DateRange(null, '2012-03-19')]; + yield [new DateRange('2012-03-17', null)]; } - /** - * @test - * @dataProvider ranges - */ + #[Test] + #[DataProvider('ranges')] public function rangeDoesNotContainNull(DateRange $range): void { assertFalse($range->contains(null)); } - /** - * @test - */ + #[Test] public function errorListIsEmptyIfValueContainedInRange(): void { assertEmptyArray($this->dateRange->errorsOf('2012-03-17')); } - /** - * @test - */ + #[Test] public function errorListContainsMinBorderErrorWhenValueBelowRange(): void { assertThat( - $this->dateRange->errorsOf('2012-03-16'), - equals(['DATE_TOO_EARLY' => ['earliestDate' => Date::castFrom('2012-03-17')->asString()]]) + $this->dateRange->errorsOf('2012-03-16'), + equals([ + 'DATE_TOO_EARLY' => [ + 'earliestDate' => Date::castFrom('2012-03-17')->asString() + ] + ]) ); } - /** - * @test - */ + #[Test] public function errorListContainsMaxBorderErrorWhenValueAboveRange(): void { assertThat( - $this->dateRange->errorsOf('2012-03-20'), - equals(['DATE_TOO_LATE' => ['latestDate' => Date::castFrom('2012-03-19')->asString()]]) + $this->dateRange->errorsOf('2012-03-20'), + equals([ + 'DATE_TOO_LATE' => [ + 'latestDate' => Date::castFrom('2012-03-19')->asString() + ] + ]) ); } /** - * @test * @since 2.3.1 - * @group issue41 */ + #[Test] + #[Group('issue41')] public function doesNotAllowToTruncate(): void { assertFalse($this->dateRange->allowsTruncate('2012-03-20')); } /** - * @test * @since 2.3.1 - * @group issue41 */ + #[Test] + #[Group('issue41')] public function tryingToTruncateThrowsBadMethodCallException(): void { expect(function() { $this->dateRange->truncateToMaxBorder('2012-03-20'); }) diff --git a/src/test/php/filter/range/DatespanRangeTest.php b/src/test/php/filter/range/DatespanRangeTest.php index 4e783e4..703caa2 100644 --- a/src/test/php/filter/range/DatespanRangeTest.php +++ b/src/test/php/filter/range/DatespanRangeTest.php @@ -7,6 +7,12 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter\range; + +use Generator; +use LogicException; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use stubbles\date\Date; use stubbles\date\span\Day; @@ -23,159 +29,130 @@ * Tests for stubbles\input\filter\range\DatespanRange. * * @since 2.0.0 - * @group filter - * @group filter_range */ +#[Group('filter')] +#[Group('filter_range')] class DatespanRangeTest extends TestCase { - /** - * instance to test - * - * @var DatespanRange - */ - private $datespanRange; + private DatespanRange $datespanRange; protected function setUp(): void { $this->datespanRange = new DatespanRange('2012-03-17', '2012-03-19'); } - /** - * @return array - */ - public static function outOfRangeValues(): array + public static function outOfRangeValues(): Generator { - return [ - [new Day('2012-03-16')], - [new Day('2012-03-20')] - ]; + yield [new Day('2012-03-16')]; + yield [new Day('2012-03-20')]; } - /** - * @test - * @dataProvider outOfRangeValues - */ + #[Test] + #[DataProvider('outOfRangeValues')] public function valueOutOfRangeIsNotContainedInRange(Day $value): void { assertFalse($this->datespanRange->contains($value)); } - /** - * @return array - */ - public static function withinRangeValues(): array + public static function withinRangeValues(): Generator { - return [ - [new Day('2012-03-17')], - [new Day('2012-03-18')], - [new Day('2012-03-19')] - ]; + yield [new Day('2012-03-17')]; + yield [new Day('2012-03-18')]; + yield [new Day('2012-03-19')]; } - /** - * @test - * @dataProvider withinRangeValues - */ + #[Test] + #[DataProvider('withinRangeValues')] public function valueWithinRangeIsContainedInRange(Day $value): void { assertTrue($this->datespanRange->contains($value)); } - /** - * @test - */ + #[Test] public function rangeContainsLowValuesIfMinValueIsNull(): void { $numberRange = new DatespanRange(null, '2012-03-19'); assertTrue($numberRange->contains(new Month('1970-12'))); } - /** - * @test - */ + #[Test] public function rangeContainsHighValuesIfMaxValueIsNull(): void { $numberRange = new DatespanRange('2012-03-17', null); assertTrue($numberRange->contains(new Year(2037))); } - /** - * @return array - */ - public static function ranges(): array + public static function ranges(): Generator { - return [ - [new DatespanRange('2012-03-17', '2012-03-19')], - [new DatespanRange(null, '2012-03-19')], - [new DatespanRange('2012-03-17', null)] - ]; + yield [new DatespanRange('2012-03-17', '2012-03-19')]; + yield [new DatespanRange(null, '2012-03-19')]; + yield [new DatespanRange('2012-03-17', null)]; } - /** - * @test - * @dataProvider ranges - */ + #[Test] + #[DataProvider('ranges')] public function rangeDoesNotContainNull(DatespanRange $range): void { assertFalse($range->contains(null)); } - /** - * @text - * @dataProvider ranges - */ - public function containsThrowsRuntimeExceptionWhenValueIsNoDatespan(DatespanRange $range): void - { + #[Test] + #[DataProvider('ranges')] + public function containsThrowsRuntimeExceptionWhenValueIsNoDatespan( + DatespanRange $range + ): void { expect(function() use ($range) { - $range->contains('foo'); - })->throws(\LogicException::class); + $range->contains('foo'); + })->throws(LogicException::class); } - /** - * @test - */ + #[Test] public function errorListIsEmptyIfValueContainedInRange(): void { assertEmptyArray($this->datespanRange->errorsOf(new Day('2012-03-17'))); } - /** - * @test - */ + #[Test] public function errorListContainsMinBorderErrorWhenValueBelowRange(): void { assertThat( - $this->datespanRange->errorsOf(new Day('2012-03-16')), - equals(['DATE_TOO_EARLY' => ['earliestDate' => Date::castFrom('2012-03-17')->asString()]]) + $this->datespanRange->errorsOf(new Day('2012-03-16')), + equals([ + 'DATE_TOO_EARLY' => [ + 'earliestDate' => Date::castFrom('2012-03-17')->asString() + ] + ]) ); } - /** - * @test - */ + #[Test] public function errorListContainsMaxBorderErrorWhenValueAboveRange(): void { assertThat( - $this->datespanRange->errorsOf(new Day('2012-03-20')), - equals(['DATE_TOO_LATE' => ['latestDate' => Date::castFrom('2012-03-19')->asString()]]) + $this->datespanRange->errorsOf(new Day('2012-03-20')), + equals([ + 'DATE_TOO_LATE' => [ + 'latestDate' => Date::castFrom('2012-03-19')->asString() + ] + ]) ); } /** - * @test * @since 2.3.1 - * @group issue41 */ + #[Test] + #[Group('issue41')] public function doesNotAllowToTruncate(): void { assertFalse($this->datespanRange->allowsTruncate(new Day('2012-03-20'))); } /** - * @test * @since 2.3.1 - * @group issue41 */ + #[Test] + #[Group('issue41')] public function tryingToTruncateThrowsMethodNotSupportedException(): void { expect(function() { $this->datespanRange->truncateToMaxBorder('2012-03-20'); }) diff --git a/src/test/php/filter/range/NumberRangeTest.php b/src/test/php/filter/range/NumberRangeTest.php index 45b88c2..347255d 100644 --- a/src/test/php/filter/range/NumberRangeTest.php +++ b/src/test/php/filter/range/NumberRangeTest.php @@ -7,6 +7,12 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter\range; + +use BadMethodCallException; +use Generator; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertThat; @@ -19,152 +25,116 @@ * Tests for stubbles\input\filter\range\NumberRange. * * @since 2.0.0 - * @group filter - * @group filter_range */ +#[Group('filter')] +#[Group('filter_range')] class NumberRangeTest extends TestCase { - /** - * instance to test - * - * @var NumberRange - */ - private $numberRange; + private NumberRange $numberRange; protected function setUp(): void { $this->numberRange = new NumberRange(1, 10); } - /** - * @return array - */ - public static function outOfRangeValues(): array + public static function outOfRangeValues(): Generator { - return [ - [0], - [11] - ]; + yield [0]; + yield [11]; } - /** - * @test - * @dataProvider outOfRangeValues - */ + #[Test] + #[DataProvider('outOfRangeValues')] public function valueOutOfRangeIsNotContainedInRange(int $value): void { assertFalse($this->numberRange->contains($value)); } - /** - * @return array - */ - public static function withinRangeValues(): array + public static function withinRangeValues(): Generator { - return [ - [1], - [4], - [8], - [10] - ]; + yield [1]; + yield [4]; + yield [8]; + yield [10]; } - /** - * @test - * @dataProvider withinRangeValues - */ + #[Test] + #[DataProvider('withinRangeValues')] public function valueWithinRangeIsContainedInRange(int $value): void { assertTrue($this->numberRange->contains($value)); } - /** - * @test - */ + #[Test] public function rangeContainsLowValuesIfMinValueIsNull(): void { $numberRange = new NumberRange(null, 10); assertTrue($numberRange->contains(PHP_INT_MAX * -1)); } - /** - * @test - */ + #[Test] public function rangeContainsHighValuesIfMaxValueIsNull(): void { $numberRange = new NumberRange(1, null); assertTrue($numberRange->contains(PHP_INT_MAX)); } - /** - * @return array - */ - public static function ranges(): array + public static function ranges(): Generator { - return [ - [new NumberRange(1, 10)], - [new NumberRange(null, 10)], - [new NumberRange(1, null)] - ]; + yield [new NumberRange(1, 10)]; + yield [new NumberRange(null, 10)]; + yield [new NumberRange(1, null)]; } - /** - * @test - * @dataProvider ranges - */ + #[Test] + #[DataProvider('ranges')] public function rangeDoesNotContainNull(NumberRange $range): void { assertFalse($range->contains(null)); } - /** - * @test - */ + #[Test] public function errorListIsEmptyIfValueContainedInRange(): void { assertEmptyArray($this->numberRange->errorsOf(3)); } - /** - * @test - */ + #[Test] public function errorListContainsMinBorderErrorWhenValueBelowRange(): void { assertThat( - $this->numberRange->errorsOf(0), - equals(['VALUE_TOO_SMALL' => ['minNumber' => 1]]) + $this->numberRange->errorsOf(0), + equals(['VALUE_TOO_SMALL' => ['minNumber' => 1]]) ); } - /** - * @test - */ + #[Test] public function errorListContainsMaxBorderErrorWhenValueAboveRange(): void { assertThat( - $this->numberRange->errorsOf(11), - equals(['VALUE_TOO_GREAT' => ['maxNumber' => 10]]) + $this->numberRange->errorsOf(11), + equals(['VALUE_TOO_GREAT' => ['maxNumber' => 10]]) ); } /** - * @test * @since 2.3.1 - * @group issue41 */ + #[Test] + #[Group('issue41')] public function doesNotAllowToTruncate(): void { assertFalse($this->numberRange->allowsTruncate(11)); } /** - * @test * @since 2.3.1 - * @group issue41 */ + #[Test] + #[Group('issue41')] public function tryingToTruncateThrowsMethodNotSupportedException(): void { - expect(function() { $this->numberRange->truncateToMaxBorder('11'); }) - ->throws(\BadMethodCallException::class); + expect(fn(): never => $this->numberRange->truncateToMaxBorder('11')) + ->throws(BadMethodCallException::class); } } diff --git a/src/test/php/filter/range/StringLengthTest.php b/src/test/php/filter/range/StringLengthTest.php index f20b118..ff1f99b 100644 --- a/src/test/php/filter/range/StringLengthTest.php +++ b/src/test/php/filter/range/StringLengthTest.php @@ -7,6 +7,14 @@ * file that was distributed with this source code. */ namespace stubbles\input\filter\range; + +use Generator; +use InvalidArgumentException; +use LogicException; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertThat; @@ -19,217 +27,174 @@ * Tests for stubbles\input\filter\range\StringLength. * * @since 2.0.0 - * @group filter - * @group filter_range */ +#[Group('filter')] +#[Group('filter_range')] class StringLengthTest extends TestCase { - /** - * instance to test - * - * @var StringLength - */ - private $stringLength; + private StringLength $stringLength; protected function setUp(): void { $this->stringLength = new StringLength(1, 10); } - /** - * @return array - */ - public static function outOfRangeValues(): array + public static function outOfRangeValues(): Generator { - return [ - [''], - ['abcdefghijk'] - ]; + yield ['']; + yield ['abcdefghijk']; } - /** - * @test - * @dataProvider outOfRangeValues - */ + #[Test] + #[DataProvider('outOfRangeValues')] public function valueOutOfRangeIsNotContainedInRange(string $value): void { assertFalse($this->stringLength->contains($value)); } - /** - * @return array - */ - public static function withinRangeValues(): array + public static function withinRangeValues(): Generator { - return [ - ['a'], - ['ab'], - ['abcdefghi'], - ['abcdefghij'] - ]; + yield ['a']; + yield ['ab']; + yield ['abcdefghi']; + yield ['abcdefghij']; } - /** - * @test - * @dataProvider withinRangeValues - */ + #[Test] + #[DataProvider('withinRangeValues')] public function valueWithinRangeIsContainedInRange(string $value): void { assertTrue($this->stringLength->contains($value)); } - /** - * @return array - */ - public static function lowValues(): array + public static function lowValues(): Generator { - return [['']]; + yield ['']; } - /** - * @test - * @dataProvider lowValues - */ + #[Test] + #[DataProvider('lowValues')] public function rangeContainsLowValuesIfMinValueIsNull(string $value): void { $numberRange = new StringLength(null, 10); assertTrue($numberRange->contains($value)); } - /** - * @return array - */ - public static function highValues(): array + public static function highValues(): Generator { - return [[str_pad('a', 100)]]; + yield [str_pad('a', 100)]; } - /** - * @test - * @dataProvider highValues - */ + #[Test] + #[DataProvider('highValues')] public function rangeContainsHighValuesIfMaxValueIsNull(string $value): void { $numberRange = new StringLength(1, null); assertTrue($numberRange->contains($value)); } - /** - * @return array - */ - public static function ranges(): array + public static function ranges(): Generator { - return [ - [new StringLength(1, 10)], - [new StringLength(null, 10)], - [new StringLength(1, null)] - ]; + yield [new StringLength(1, 10)]; + yield [new StringLength(null, 10)]; + yield [new StringLength(1, null)]; } - /** - * @test - * @dataProvider ranges - */ + #[Test] + #[DataProvider('ranges')] public function rangeDoesNotContainNull(StringLength $range): void { assertFalse($range->contains(null)); } - /** - * @test - */ + #[Test] public function errorListIsEmptyIfValueContainedInRange(): void { assertEmptyArray($this->stringLength->errorsOf('foo')); } - /** - * @test - */ + #[Test] public function errorListContainsMinBorderErrorWhenValueBelowRange(): void { assertThat( - $this->stringLength->errorsOf(''), - equals(['STRING_TOO_SHORT' => ['minLength' => 1]]) + $this->stringLength->errorsOf(''), + equals(['STRING_TOO_SHORT' => ['minLength' => 1]]) ); } - /** - * @test - */ + #[Test] public function errorListContainsMaxBorderErrorWhenValueAboveRange(): void { assertThat( - $this->stringLength->errorsOf('abcdefghijk'), - equals(['STRING_TOO_LONG' => ['maxLength' => 10]]) + $this->stringLength->errorsOf('abcdefghijk'), + equals(['STRING_TOO_LONG' => ['maxLength' => 10]]) ); } - /** - * @return array - */ - public static function truncateValues(): array + public static function truncateValues(): Generator { - return [['foobar']]; + yield ['foobar']; } /** - * @test * @since 2.3.1 - * @group issue41 - * @dataProvider truncateValues */ + #[Test] + #[Group('issue41')] + #[DataProvider('truncateValues')] public function doesNotAllowTruncateByDefault(string $value): void { assertFalse($this->stringLength->allowsTruncate($value)); } /** - * @test * @since 2.3.1 - * @group issue41 - * @dataProvider truncateValues */ + #[Test] + #[Group('issue41')] + #[DataProvider('truncateValues')] public function truncateValueWhenNotAllowedThrowsLogicException(string $value): void { expect(function() use ($value) { - $this->stringLength->truncateToMaxBorder($value); - })->throws(\LogicException::class); + $this->stringLength->truncateToMaxBorder($value); + })->throws(LogicException::class); } /** - * @test * @since 2.3.1 - * @group issue41 - * @dataProvider truncateValues */ + #[Test] + #[Group('issue41')] + #[DataProvider('truncateValues')] public function allowsTruncateWhenCreatedThisWay(string $value): void { assertTrue(StringLength::truncate(null, 3)->allowsTruncate($value)); } /** - * @test * @since 2.3.1 - * @group issue41 */ - public function createWithTruncateAndNoMaxLengthThrowsIllegalArgumentException(): void - { - expect(function() { - StringLength::truncate(50, null); - })->throws(\InvalidArgumentException::class); + #[Test] + #[TestWith([0])] + #[TestWith([-1])] + #[Group('issue41')] + public function createWithTruncateAndZeroMaxLengthThrowsIllegalArgumentException( + int $maxLength + ): void { + expect(fn() => StringLength::truncate(50, $maxLength)) + ->throws(InvalidArgumentException::class); } /** - * @test * @since 2.3.1 - * @group issue41 */ + #[Test] + #[Group('issue41')] public function truncateToMaxBorderReturnsSubstringWithMaxLength(): void { assertThat( - StringLength::truncate(null, 3)->truncateToMaxBorder('foobar'), - equals('foo') + StringLength::truncate(null, 3)->truncateToMaxBorder('foobar'), + equals('foo') ); } }