diff --git a/components/ILIAS/Refinery/src/String/Transformation/LevenshteinTransformation.php b/components/ILIAS/Refinery/src/String/Transformation/LevenshteinTransformation.php index 9d39207284ec..37fb8a31fd63 100755 --- a/components/ILIAS/Refinery/src/String/Transformation/LevenshteinTransformation.php +++ b/components/ILIAS/Refinery/src/String/Transformation/LevenshteinTransformation.php @@ -81,8 +81,8 @@ protected function levenshtein(string $secondary_string): float $cost_matrix = []; $primary_string_array = $this->stringToCharacterArray($this->primary_string); $secondary_string_array = $this->stringToCharacterArray($secondary_string); - $primary_string_length = count($primary_string_array); - $secondary_string_length = count($secondary_string_array); + $primary_string_length = \count($primary_string_array); + $secondary_string_length = \count($secondary_string_array); // if the difference between string length is bigger than the maximum allowed levenshtein distance // the code can be skipped @@ -146,8 +146,8 @@ private function stringToCharacterArray(string $string_to_convert): array */ public function transform($from): float { - if (!is_string($from)) { - throw new InvalidArgumentException(__METHOD__ . " the argument is not a string."); + if (!\is_string($from)) { + throw new InvalidArgumentException(__METHOD__ . ' the argument is not a string.'); } return $this->levenshtein($from); diff --git a/components/ILIAS/Refinery/tests/Container/Transformation/AddLabelTest.php b/components/ILIAS/Refinery/tests/Container/Transformation/AddLabelTest.php index 7e90de298dc6..8642df9e322e 100755 --- a/components/ILIAS/Refinery/tests/Container/Transformation/AddLabelTest.php +++ b/components/ILIAS/Refinery/tests/Container/Transformation/AddLabelTest.php @@ -22,6 +22,7 @@ use ILIAS\Refinery\Transformation; use PHPUnit\Framework\TestCase; use ILIAS\Data\Factory as DataFactory; +use PHPUnit\Framework\MockObject\Stub; class AddLabelTest extends TestCase { @@ -38,7 +39,8 @@ class AddLabelTest extends TestCase protected function setUp(): void { $dataFactory = new DataFactory(); - $language = $this->createMock(ILIAS\Language\Language::class); + /** @var ILIAS\Language\Language&Stub $language */ + $language = $this->createStub(ILIAS\Language\Language::class); $this->f = new Refinery($dataFactory, $language); $this->add_label = $this->f->container()->addLabels(self::$labels); diff --git a/components/ILIAS/Refinery/tests/Container/Transformation/MapValuesTest.php b/components/ILIAS/Refinery/tests/Container/Transformation/MapValuesTest.php index d5e4f1cb7403..52af8ca209ba 100755 --- a/components/ILIAS/Refinery/tests/Container/Transformation/MapValuesTest.php +++ b/components/ILIAS/Refinery/tests/Container/Transformation/MapValuesTest.php @@ -22,6 +22,7 @@ use ILIAS\Data\Factory as DataFactory; use ILIAS\Refinery\Transformation; use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\MockObject\Stub; class MapValuesTest extends TestCase { @@ -43,7 +44,8 @@ class MapValuesTest extends TestCase protected function setUp(): void { $dataFactory = new DataFactory(); - $language = $this->createMock(ILIAS\Language\Language::class); + /** @var ILIAS\Language\Language&Stub $language */ + $language = $this->createStub(ILIAS\Language\Language::class); $this->f = new Refinery($dataFactory, $language); $this->map_values = $this->f->container()->mapValues($this->f->custom()->transformation(fn($v) => $v * 2)); diff --git a/components/ILIAS/Refinery/tests/Custom/Constraint/CustomTest.php b/components/ILIAS/Refinery/tests/Custom/Constraint/CustomTest.php index c3ed14315c11..c514f0dce80d 100755 --- a/components/ILIAS/Refinery/tests/Custom/Constraint/CustomTest.php +++ b/components/ILIAS/Refinery/tests/Custom/Constraint/CustomTest.php @@ -25,33 +25,20 @@ class CustomTest extends TestCase { private string $txt_id = ''; - private ILIAS\Language\Language $lng; private CustomConstraint $constraint; protected function setUp(): void { - $is_ok = static function ($value): bool { - return false; - }; - $this->txt_id = "TXT_ID"; - $error = function (callable $txt, $value): string { - return $txt($this->txt_id, $value); - }; - $this->lng = $this->createMock(ILIAS\Language\Language::class); - $this->constraint = new class ($is_ok, $error, new DataFactory(), $this->lng) extends CustomConstraint { - public function _getLngClosure(): Closure - { - return $this->getLngClosure(); - } - }; + $this->txt_id = 'TXT_ID'; + $this->constraint = $this->newConstraint($this->createStub(ILIAS\Language\Language::class)); } public function testWithProblemBuilder(): void { $new_constraint = $this->constraint->withProblemBuilder(static function (): string { - return "This was a fault"; + return 'This was a fault'; }); - $this->assertEquals("This was a fault", $new_constraint->problemWith("")); + $this->assertEquals('This was a fault', $new_constraint->problemWith('')); } public function testProblemBuilderRetrievesLngClosure(): void @@ -59,23 +46,24 @@ public function testProblemBuilderRetrievesLngClosure(): void $cls = null; $c = $this->constraint->withProblemBuilder(function ($txt) use (&$cls): string { $cls = $txt; - return ""; + return ''; }); - $c->problemWith(""); + $c->problemWith(''); $this->assertIsCallable($cls); } public function test_use_txt(): void { $txt_out = "'%s'"; - $this->lng + $lng = $this->createMock(ILIAS\Language\Language::class); + $lng ->expects($this->once()) - ->method("txt") + ->method('txt') ->with($this->txt_id) ->willReturn($txt_out); - $value = "VALUE"; - $problem = $this->constraint->problemWith($value); + $value = 'VALUE'; + $problem = $this->newConstraint($lng)->problemWith($value); $this->assertEquals(sprintf($txt_out, $value), $problem); } @@ -91,15 +79,15 @@ public function test_exception_on_no_parameter(): void public function test_no_sprintf_on_one_parameter(): void { - $lng_closure = $this->constraint->_getLngClosure(); - - $txt_out = "txt"; - $this->lng + $txt_out = 'txt'; + $lng = $this->createMock(ILIAS\Language\Language::class); + $lng ->expects($this->once()) - ->method("txt") + ->method('txt') ->with($this->txt_id) ->willReturn($txt_out); + $lng_closure = $this->newConstraint($lng)->_getLngClosure(); $res = $lng_closure($this->txt_id); $this->assertEquals($txt_out, $res); @@ -107,16 +95,29 @@ public function test_no_sprintf_on_one_parameter(): void public function test_gracefully_handle_arrays_and_objects(): void { - $lng_closure = $this->constraint->_getLngClosure(); - - $this->lng + $lng = $this->createMock(ILIAS\Language\Language::class); + $lng ->expects($this->once()) - ->method("txt") - ->with("id") - ->willReturn("%s-%s-%s-%s-"); + ->method('txt') + ->with('id') + ->willReturn('%s-%s-%s-%s-'); + + $lng_closure = $this->newConstraint($lng)->_getLngClosure(); + $res = $lng_closure('id', [], new stdClass(), 'foo', null); - $res = $lng_closure("id", [], new stdClass(), "foo", null); + $this->assertEquals('array-' . stdClass::class . '-foo-null-', $res); + } + + private function newConstraint(ILIAS\Language\Language $lng): CustomConstraint + { + $is_ok = static fn($value): bool => false; + $error = fn(callable $txt, $value): string => $txt($this->txt_id, $value); - $this->assertEquals("array-" . stdClass::class . "-foo-null-", $res); + return new class ($is_ok, $error, $this->createStub(DataFactory::class), $lng) extends CustomConstraint { + public function _getLngClosure(): Closure + { + return $this->getLngClosure(); + } + }; } } diff --git a/components/ILIAS/Refinery/tests/Custom/GroupTest.php b/components/ILIAS/Refinery/tests/Custom/GroupTest.php index aef6ae634434..9b436923be97 100755 --- a/components/ILIAS/Refinery/tests/Custom/GroupTest.php +++ b/components/ILIAS/Refinery/tests/Custom/GroupTest.php @@ -26,19 +26,18 @@ use ILIAS\Refinery\Custom\Constraint as CustomConstraint; use PHPUnit\Framework\TestCase; use ILIAS\Language\Language; +use PHPUnit\Framework\MockObject\Stub; class GroupTest extends TestCase { private CustomGroup $group; private DataFactory $dataFactory; - private Language $language; + private Language&Stub $language; protected function setUp(): void { $this->dataFactory = new DataFactory(); - $this->language = $this->getMockBuilder(Language::class) - ->disableOriginalConstructor() - ->getMock(); + $this->language = $this->createStub(Language::class); $this->group = new CustomGroup($this->dataFactory, $this->language); } diff --git a/components/ILIAS/Refinery/tests/Custom/Transformation/TransformationsCustomTest.php b/components/ILIAS/Refinery/tests/Custom/Transformation/TransformationsCustomTest.php index fbddf4400be6..4b5c0982245b 100755 --- a/components/ILIAS/Refinery/tests/Custom/Transformation/TransformationsCustomTest.php +++ b/components/ILIAS/Refinery/tests/Custom/Transformation/TransformationsCustomTest.php @@ -22,17 +22,19 @@ use ILIAS\Data\Factory as DataFactory; use ILIAS\Refinery\Transformation; use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\MockObject\Stub; class TransformationsCustomTest extends TestCase { - private const TEST_STRING = "Test"; + private const string TEST_STRING = 'Test'; private ?Transformation $custom; private ?Refinery $f; protected function setUp(): void { - $language = $this->createMock(ILIAS\Language\Language::class); + /** @var ILIAS\Language\Language&Stub $language */ + $language = $this->createStub(ILIAS\Language\Language::class); $this->f = new Refinery(new DataFactory(), $language); $this->custom = $this->f->custom()->transformation( diff --git a/components/ILIAS/Refinery/tests/FactoryTest.php b/components/ILIAS/Refinery/tests/FactoryTest.php index 17906afdfabf..993763b3e74c 100755 --- a/components/ILIAS/Refinery/tests/FactoryTest.php +++ b/components/ILIAS/Refinery/tests/FactoryTest.php @@ -39,6 +39,7 @@ use ILIAS\Refinery\Decode\Group as DecodeGroup; use ILIAS\Refinery\Encode\Group as EncodeGroup; use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\MockObject\Stub; class FactoryTest extends TestCase { @@ -46,9 +47,8 @@ class FactoryTest extends TestCase protected function setUp(): void { - $language = $this->getMockBuilder(\ILIAS\Language\Language::class) - ->disableOriginalConstructor() - ->getMock(); + /** @var \ILIAS\Language\Language&Stub $language */ + $language = $this->createStub(\ILIAS\Language\Language::class); $this->basicFactory = new Refinery(new DataFactory(), $language); } diff --git a/components/ILIAS/Refinery/tests/Integer/Constraints/GreaterThanConstraintTest.php b/components/ILIAS/Refinery/tests/Integer/Constraints/GreaterThanConstraintTest.php index e1a0d21e6a1a..01f322cb9b2b 100755 --- a/components/ILIAS/Refinery/tests/Integer/Constraints/GreaterThanConstraintTest.php +++ b/components/ILIAS/Refinery/tests/Integer/Constraints/GreaterThanConstraintTest.php @@ -29,23 +29,17 @@ class GreaterThanConstraintTest extends TestCase { private DataFactory $df; - private Language $lng; private int $greater_than; private Constraint $c; protected function setUp(): void { $this->df = new DataFactory(); - $this->lng = $this->getMockBuilder(Language::class) - ->disableOriginalConstructor() - ->getMock(); - $this->greater_than = 10; - $this->c = new GreaterThan( $this->greater_than, $this->df, - $this->lng + $this->createStub(Language::class) ); } @@ -78,13 +72,16 @@ public function testNoProblemWith(): void public function testProblemWith(): void { - $this->lng + $lng = $this->createMock(Language::class); + $lng ->expects($this->once()) - ->method("txt") - ->with("not_greater_than") - ->willReturn("-%s-%s-"); + ->method('txt') + ->with('not_greater_than') + ->willReturn('-%s-%s-'); + + $constraint = new GreaterThan($this->greater_than, $this->df, $lng); - $this->assertEquals("-2-10-", $this->c->problemWith(2)); + $this->assertEquals('-2-10-', $constraint->problemWith(2)); } public function testRestrictOk(): void @@ -105,7 +102,7 @@ public function testRestrictNotOk(): void public function testRestrictError(): void { - $error = $this->df->error("error"); + $error = $this->df->error('error'); $res = $this->c->applyTo($error); $this->assertSame($error, $res); @@ -114,8 +111,8 @@ public function testRestrictError(): void public function testWithProblemBuilder(): void { $new_c = $this->c->withProblemBuilder(static function (): string { - return "This was a fault"; + return 'This was a fault'; }); - $this->assertEquals("This was a fault", $new_c->problemWith(2)); + $this->assertEquals('This was a fault', $new_c->problemWith(2)); } } diff --git a/components/ILIAS/Refinery/tests/Integer/Constraints/GreaterThanOrEqualConstraintTest.php b/components/ILIAS/Refinery/tests/Integer/Constraints/GreaterThanOrEqualConstraintTest.php index 41a1124d0bd1..f4544a64b8d4 100755 --- a/components/ILIAS/Refinery/tests/Integer/Constraints/GreaterThanOrEqualConstraintTest.php +++ b/components/ILIAS/Refinery/tests/Integer/Constraints/GreaterThanOrEqualConstraintTest.php @@ -30,22 +30,15 @@ class GreaterThanOrEqualConstraintTest extends TestCase { private DataFactory $df; - private Language $lng; private Constraint $c; protected function setUp(): void { $this->df = new DataFactory(); - $this->lng = $this->getMockBuilder(Language::class) - ->disableOriginalConstructor() - ->getMock(); - - $greater_than_or_equal = 10; - $this->c = new GreaterThanOrEqual( - $greater_than_or_equal, + 10, $this->df, - $this->lng + $this->createStub(Language::class) ); } @@ -78,13 +71,16 @@ public function testNoProblemWith(): void public function testProblemWith(): void { - $this->lng + $lng = $this->createMock(Language::class); + $lng ->expects($this->once()) - ->method("txt") - ->with("not_greater_than_or_equal") - ->willReturn("-%s"); + ->method('txt') + ->with('not_greater_than_or_equal') + ->willReturn('-%s'); + + $constraint = new GreaterThanOrEqual(10, $this->df, $lng); - $this->assertEquals("-10", $this->c->problemWith(9)); + $this->assertEquals('-10', $constraint->problemWith(9)); } public function testRestrictOk(): void @@ -105,7 +101,7 @@ public function testRestrictNotOk(): void public function testRestrictError(): void { - $error = $this->df->error("error"); + $error = $this->df->error('error'); $res = $this->c->applyTo($error); $this->assertSame($error, $res); @@ -114,8 +110,8 @@ public function testRestrictError(): void public function testWithProblemBuilder(): void { $new_c = $this->c->withProblemBuilder(static function (): string { - return "This was a fault"; + return 'This was a fault'; }); - $this->assertEquals("This was a fault", $new_c->problemWith(9)); + $this->assertEquals('This was a fault', $new_c->problemWith(9)); } } diff --git a/components/ILIAS/Refinery/tests/Integer/Constraints/LessThanConstraintTest.php b/components/ILIAS/Refinery/tests/Integer/Constraints/LessThanConstraintTest.php index 25c874848587..34f33e7cf4df 100755 --- a/components/ILIAS/Refinery/tests/Integer/Constraints/LessThanConstraintTest.php +++ b/components/ILIAS/Refinery/tests/Integer/Constraints/LessThanConstraintTest.php @@ -30,23 +30,17 @@ class LessThanConstraintTest extends TestCase { private Constraint $c; - private Language $lng; private DataFactory $df; private int $less_than; protected function setUp(): void { $this->df = new DataFactory(); - $this->lng = $this->getMockBuilder(Language::class) - ->disableOriginalConstructor() - ->getMock(); - $this->less_than = 10; - $this->c = new LessThan( $this->less_than, $this->df, - $this->lng + $this->createStub(Language::class) ); } @@ -79,13 +73,16 @@ public function testNoProblemWith(): void public function testProblemWith(): void { - $this->lng + $lng = $this->createMock(Language::class); + $lng ->expects($this->once()) - ->method("txt") - ->with("not_less_than") - ->willReturn("-%s-%s-"); + ->method('txt') + ->with('not_less_than') + ->willReturn('-%s-%s-'); + + $constraint = new LessThan($this->less_than, $this->df, $lng); - $this->assertEquals("-12-{$this->less_than}-", $this->c->problemWith("12")); + $this->assertEquals("-12-{$this->less_than}-", $constraint->problemWith('12')); } public function testRestrictOk(): void @@ -106,7 +103,7 @@ public function testRestrictNotOk(): void public function testRestrictError(): void { - $error = $this->df->error("error"); + $error = $this->df->error('error'); $res = $this->c->applyTo($error); $this->assertSame($error, $res); @@ -115,8 +112,8 @@ public function testRestrictError(): void public function testWithProblemBuilder(): void { $new_c = $this->c->withProblemBuilder(static function (): string { - return "This was a fault"; + return 'This was a fault'; }); - $this->assertEquals("This was a fault", $new_c->problemWith(13)); + $this->assertEquals('This was a fault', $new_c->problemWith(13)); } } diff --git a/components/ILIAS/Refinery/tests/Integer/Constraints/LessThanOrEqualConstraintTest.php b/components/ILIAS/Refinery/tests/Integer/Constraints/LessThanOrEqualConstraintTest.php index 898a8924f3e2..2aa470041a48 100755 --- a/components/ILIAS/Refinery/tests/Integer/Constraints/LessThanOrEqualConstraintTest.php +++ b/components/ILIAS/Refinery/tests/Integer/Constraints/LessThanOrEqualConstraintTest.php @@ -30,22 +30,15 @@ class LessThanOrEqualConstraintTest extends TestCase { private DataFactory $df; - private Language $lng; private Constraint $c; protected function setUp(): void { $this->df = new DataFactory(); - $this->lng = $this->getMockBuilder(Language::class) - ->disableOriginalConstructor() - ->getMock(); - - $less_than_or_equal = 10; - $this->c = new LessThanOrEqual( - $less_than_or_equal, + 10, $this->df, - $this->lng + $this->createStub(Language::class) ); } @@ -78,13 +71,16 @@ public function testNoProblemWith(): void public function testProblemWith(): void { - $this->lng + $lng = $this->createMock(Language::class); + $lng ->expects($this->once()) - ->method("txt") - ->with("not_less_than_or_equal") - ->willReturn("-%s"); + ->method('txt') + ->with('not_less_than_or_equal') + ->willReturn('-%s'); + + $constraint = new LessThanOrEqual(10, $this->df, $lng); - $this->assertEquals("-10", $this->c->problemWith(11)); + $this->assertEquals('-10', $constraint->problemWith(11)); } public function testRestrictOk(): void @@ -105,7 +101,7 @@ public function testRestrictNotOk(): void public function testRestrictError(): void { - $error = $this->df->error("error"); + $error = $this->df->error('error'); $res = $this->c->applyTo($error); $this->assertSame($error, $res); @@ -114,8 +110,8 @@ public function testRestrictError(): void public function testWithProblemBuilder(): void { $new_c = $this->c->withProblemBuilder(static function (): string { - return "This was a fault"; + return 'This was a fault'; }); - $this->assertEquals("This was a fault", $new_c->problemWith(13)); + $this->assertEquals('This was a fault', $new_c->problemWith(13)); } } diff --git a/components/ILIAS/Refinery/tests/Integer/GroupTest.php b/components/ILIAS/Refinery/tests/Integer/GroupTest.php index d8f5089ce290..e02cb80e503d 100755 --- a/components/ILIAS/Refinery/tests/Integer/GroupTest.php +++ b/components/ILIAS/Refinery/tests/Integer/GroupTest.php @@ -30,6 +30,7 @@ use ILIAS\Refinery\In\Group as InGroup; use ILIAS\Refinery\Constraint; use ILIAS\Language\Language; +use PHPUnit\Framework\MockObject\Stub; class GroupTest extends TestCase { @@ -38,12 +39,10 @@ class GroupTest extends TestCase protected function setUp(): void { $dataFactory = new Factory(); - $language = $this->getMockBuilder(Language::class) - ->disableOriginalConstructor() - ->getMock(); - $in = $this->getMockBuilder(InGroup::class) - ->disableOriginalConstructor() - ->getMock(); + /** @var Language&Stub $language */ + $language = $this->createStub(Language::class); + /** @var InGroup&Stub $in */ + $in = $this->createStub(InGroup::class); $this->group = new IntegerGroup($dataFactory, $language, $in); } @@ -73,13 +72,16 @@ public function testLessThanOrEqualInstance(): void public function testIsBetween(): void { - $dataFactory = $this->getMockBuilder(Factory::class)->disableOriginalConstructor()->getMock(); - $language = $this->getMockBuilder(Language::class)->disableOriginalConstructor()->getMock(); + /** @var Factory&Stub $dataFactory */ + $dataFactory = $this->createStub(Factory::class); + /** @var Language&Stub $language */ + $language = $this->createStub(Language::class); $in = $this->getMockBuilder(InGroup::class)->disableOriginalConstructor()->getMock(); - $series = $this->getMockBuilder(Constraint::class)->getMock(); + /** @var Constraint&Stub $series */ + $series = $this->createStub(Constraint::class); - $in->expects(self::once())->method('series')->willReturnCallback(function (array $array) use ($series) { - $this->assertSame(2, count($array)); + $in->expects($this->once())->method('series')->willReturnCallback(function (array $array) use ($series) { + $this->assertCount(2, $array); $this->assertInstanceOf(GreaterThanOrEqual::class, $array[0]); $this->assertInstanceOf(LessThanOrEqual::class, $array[1]); diff --git a/components/ILIAS/Refinery/tests/IsExecutableTransformationTest.php b/components/ILIAS/Refinery/tests/IsExecutableTransformationTest.php index 24a9fd50bf01..8e5b3bb5ab76 100644 --- a/components/ILIAS/Refinery/tests/IsExecutableTransformationTest.php +++ b/components/ILIAS/Refinery/tests/IsExecutableTransformationTest.php @@ -23,18 +23,21 @@ use ILIAS\Refinery\IsExecutableTransformation; use PHPUnit\Framework\TestCase; use ILIAS\Language\Language; +use PHPUnit\Framework\MockObject\Stub; class IsExecutableTransformationTest extends TestCase { public function testConstruct(): void { - $language = $this->getMockBuilder(Language::class)->disableOriginalConstructor()->getMock(); + /** @var Language&Stub $language */ + $language = $this->createStub(Language::class); $this->assertInstanceOf(IsExecutableTransformation::class, new IsExecutableTransformation($language)); } public function testFailingAccept(): void { - $language = $this->getMockBuilder(Language::class)->disableOriginalConstructor()->getMock(); + /** @var Language&Stub $language */ + $language = $this->createStub(Language::class); $transformation = new IsExecutableTransformation($language); $this->assertFalse($transformation->accepts('I hope this string is no valid path...')); @@ -46,7 +49,8 @@ public function testSuccessfulAccept(): void $this->markTestSkipped('Shell /bin/sh is not available.'); return; } - $language = $this->getMockBuilder(Language::class)->disableOriginalConstructor()->getMock(); + /** @var Language&Stub $language */ + $language = $this->createStub(Language::class); $transformation = new IsExecutableTransformation($language); $this->assertTrue($transformation->accepts('/bin/sh')); diff --git a/components/ILIAS/Refinery/tests/KeyValueAccessTest.php b/components/ILIAS/Refinery/tests/KeyValueAccessTest.php index d75eb748a625..c3034a16202d 100755 --- a/components/ILIAS/Refinery/tests/KeyValueAccessTest.php +++ b/components/ILIAS/Refinery/tests/KeyValueAccessTest.php @@ -32,7 +32,7 @@ class KeyValueAccessTest extends PHPUnitTestCase protected function setUp(): void { - $this->refinery = new Refinery(new DataFactory(), $this->createMock(Language::class)); + $this->refinery = new Refinery(new DataFactory(), $this->createStub(Language::class)); } public function testAccess(): void diff --git a/components/ILIAS/Refinery/tests/KindlyTo/Transformation/DictionaryTransformationTest.php b/components/ILIAS/Refinery/tests/KindlyTo/Transformation/DictionaryTransformationTest.php index d68b1ccdbf19..197da75de2f4 100755 --- a/components/ILIAS/Refinery/tests/KindlyTo/Transformation/DictionaryTransformationTest.php +++ b/components/ILIAS/Refinery/tests/KindlyTo/Transformation/DictionaryTransformationTest.php @@ -29,10 +29,6 @@ class DictionaryTransformationTest extends TestCase { - /** - * @param array $originVal - * @param array $expectedVal - */ #[DataProvider('DictionaryTransformationDataProvider')] public function testDictionaryTransformation(array $originVal, array $expectedVal): void { diff --git a/components/ILIAS/Refinery/tests/KindlyTo/Transformation/RecordTransformationTest.php b/components/ILIAS/Refinery/tests/KindlyTo/Transformation/RecordTransformationTest.php index eebc38b505c8..63dcc3d4c99f 100755 --- a/components/ILIAS/Refinery/tests/KindlyTo/Transformation/RecordTransformationTest.php +++ b/components/ILIAS/Refinery/tests/KindlyTo/Transformation/RecordTransformationTest.php @@ -33,10 +33,6 @@ class RecordTransformationTest extends TestCase private const INT_KEY = 'integerKey'; private const SECOND_INT_KEY = 'integerKey2'; - /** - * @param array $originVal - * @param array $expectedVal - */ #[DataProvider('RecordTransformationDataProvider')] public function testRecordTransformationIsValid(array $originVal, array $expectedVal): void { @@ -51,9 +47,6 @@ public function testRecordTransformationIsValid(array $originVal, array $expecte $this->assertEquals($expectedVal, $transformedValue); } - /** - * @param array $origVal - */ #[DataProvider('RecordFailureDataProvider')] public function testRecordTransformationFailures(array $origVal): void { @@ -89,9 +82,6 @@ public function testInvalidArray(): void $this->fail(); } - /** - * @param array $originalValue - */ #[DataProvider('RecordValueInvalidDataProvider')] public function testInvalidValueDoesNotMatch(array $originalValue): void { diff --git a/components/ILIAS/Refinery/tests/KindlyTo/Transformation/TupleTransformationTest.php b/components/ILIAS/Refinery/tests/KindlyTo/Transformation/TupleTransformationTest.php index a494b6471ca6..2da108a306a6 100755 --- a/components/ILIAS/Refinery/tests/KindlyTo/Transformation/TupleTransformationTest.php +++ b/components/ILIAS/Refinery/tests/KindlyTo/Transformation/TupleTransformationTest.php @@ -30,10 +30,6 @@ class TupleTransformationTest extends TestCase { private const TUPLE_KEY = 'hello'; - /** - * @param array $originVal - * @param array $expectedVal - */ #[DataProvider('TupleTransformationDataProvider')] public function testTupleTransformation(array $originVal, array $expectedVal): void { @@ -48,9 +44,6 @@ public function testTupleTransformation(array $originVal, array $expectedVal): v $this->assertEquals($expectedVal, $transformedValue); } - /** - * @param array $failingVal - */ #[DataProvider('TupleFailingTransformationDataProvider')] public function testNewTupleIsIncorrect(array $failingVal): void { @@ -70,9 +63,6 @@ public function testNewTupleIsIncorrect(array $failingVal): void $this->fail(); } - /** - * @param array $tooManyValues - */ #[DataProvider('TupleTooManyValuesDataProvider')] public function testTupleTooManyValues(array $tooManyValues): void { diff --git a/components/ILIAS/Refinery/tests/Logical/Constraint/LogicalOrTest.php b/components/ILIAS/Refinery/tests/Logical/Constraint/LogicalOrTest.php index f7272d3bd394..b40316653a3b 100755 --- a/components/ILIAS/Refinery/tests/Logical/Constraint/LogicalOrTest.php +++ b/components/ILIAS/Refinery/tests/Logical/Constraint/LogicalOrTest.php @@ -90,9 +90,6 @@ public function testWithProblemBuilder(LogicalOr $constraint, mixed $okValue, mi $this->assertEquals("This was a vault", $new_constraint->problemWith($errorValue)); } - /** - * @return array - */ public static function constraintsProvider(): array { $language = new class () implements \ILIAS\Language\Language { diff --git a/components/ILIAS/Refinery/tests/Logical/Constraint/NotConstraintTest.php b/components/ILIAS/Refinery/tests/Logical/Constraint/NotConstraintTest.php index fc9041ed4bb4..914b1f943a15 100755 --- a/components/ILIAS/Refinery/tests/Logical/Constraint/NotConstraintTest.php +++ b/components/ILIAS/Refinery/tests/Logical/Constraint/NotConstraintTest.php @@ -26,32 +26,15 @@ class NotConstraintTest extends TestCase { private DataFactory $df; - private ILIAS\Language\Language $lng; - private Refinery $refinery; private Constraint $not_true; private Constraint $not_false; protected function setUp(): void { $this->df = new DataFactory(); - $this->lng = $this->createMock(ILIAS\Language\Language::class); - $this->refinery = new Refinery($this->df, $this->lng); - - $group = $this->refinery->custom(); - - $this->not_true = $this->refinery->logical()->not($group->constraint( - static function ($v): bool { - return true; - }, - "not_true" - )); - - $this->not_false = $this->refinery->logical()->not($group->constraint( - static function ($v): bool { - return false; - }, - "not_false" - )); + [$this->not_true, $this->not_false] = $this->notConstraints( + $this->createStub(ILIAS\Language\Language::class) + ); } public function testAccepts(): void @@ -83,13 +66,16 @@ public function testNoProblemWith(): void public function testProblemWith(): void { - $this->lng + $lng = $this->createMock(ILIAS\Language\Language::class); + $lng ->expects($this->once()) - ->method("txt") - ->with("not_generic") - ->willReturn("-%s-"); + ->method('txt') + ->with('not_generic') + ->willReturn('-%s-'); - $this->assertEquals("-not_true-", $this->not_true->problemWith(null)); + [$not_true] = $this->notConstraints($lng); + + $this->assertEquals('-not_true-', $not_true->problemWith(null)); } public function testRestrictOk(): void @@ -110,7 +96,7 @@ public function testRestrictNotOk(): void public function testRestrictError(): void { - $error = $this->df->error("error"); + $error = $this->df->error('error'); $res = $this->not_false->applyTo($error); $this->assertSame($error, $res); @@ -118,9 +104,27 @@ public function testRestrictError(): void public function testWithProblemBuilder(): void { - $new_c = $this->not_true->withProblemBuilder(static function (): string { - return "This was a fault"; - }); - $this->assertEquals("This was a fault", $new_c->problemWith(null)); + $new_c = $this->not_true->withProblemBuilder(static fn(): string => 'This was a fault'); + $this->assertEquals('This was a fault', $new_c->problemWith(null)); + } + + /** + * @return array{Constraint, Constraint} + */ + private function notConstraints(ILIAS\Language\Language $lng): array + { + $refinery = new Refinery($this->df, $lng); + $group = $refinery->custom(); + + return [ + $refinery->logical()->not($group->constraint( + static fn($v): bool => true, + 'not_true' + )), + $refinery->logical()->not($group->constraint( + static fn($v): bool => false, + 'not_false' + )), + ]; } } diff --git a/components/ILIAS/Refinery/tests/Logical/Constraint/ParallelConstraintsTest.php b/components/ILIAS/Refinery/tests/Logical/Constraint/ParallelConstraintsTest.php index bb1373e3c79c..595506f6627d 100755 --- a/components/ILIAS/Refinery/tests/Logical/Constraint/ParallelConstraintsTest.php +++ b/components/ILIAS/Refinery/tests/Logical/Constraint/ParallelConstraintsTest.php @@ -26,35 +26,12 @@ class ParallelConstraintsTest extends TestCase { private DataFactory $df; - private ILIAS\Language\Language $lng; private Constraint $c; - private Refinery $refinery; protected function setUp(): void { $this->df = new DataFactory(); - $this->lng = $this->createMock(ILIAS\Language\Language::class); - $this->refinery = new Refinery($this->df, $this->lng); - - $group = $this->refinery->custom(); - - $less_than_3 = $group->constraint( - static function ($value): bool { - return $value < 3; - }, - "not_less_than_3" - ); - - $less_than_5 = $group->constraint( - static function ($value): bool { - return $value < 5; - }, - "not_less_than_5" - ); - - $this->c = $this->refinery - ->logical() - ->parallel([$less_than_3, $less_than_5]); + $this->c = $this->parallelConstraint($this->createStub(ILIAS\Language\Language::class)); } public function testAccepts(): void @@ -86,20 +63,22 @@ public function testNoProblemWith(): void public function testProblemWith1(): void { - $this->lng + $lng = $this->createMock(ILIAS\Language\Language::class); + $lng ->expects($this->never()) - ->method("txt"); + ->method('txt'); - $this->assertEquals("not_less_than_3", $this->c->problemWith(4)); + $this->assertEquals('not_less_than_3', $this->parallelConstraint($lng)->problemWith(4)); } public function testProblemWith2(): void { - $this->lng + $lng = $this->createMock(ILIAS\Language\Language::class); + $lng ->expects($this->never()) - ->method("txt"); + ->method('txt'); - $this->assertEquals("not_less_than_3 not_less_than_5", $this->c->problemWith(6)); + $this->assertEquals('not_less_than_3 not_less_than_5', $this->parallelConstraint($lng)->problemWith(6)); } public function testRestrictOk(): void @@ -120,7 +99,7 @@ public function testRestrictNotOk(): void public function testRestrictError(): void { - $error = $this->df->error("error"); + $error = $this->df->error('error'); $res = $this->c->applyTo($error); $this->assertSame($error, $res); @@ -128,9 +107,25 @@ public function testRestrictError(): void public function testWithProblemBuilder(): void { - $new_c = $this->c->withProblemBuilder(static function (): string { - return "This was a fault"; - }); - $this->assertEquals("This was a fault", $new_c->problemWith(7)); + $new_c = $this->c->withProblemBuilder(static fn(): string => 'This was a fault'); + $this->assertEquals('This was a fault', $new_c->problemWith(7)); + } + + private function parallelConstraint(ILIAS\Language\Language $lng): Constraint + { + $refinery = new Refinery($this->df, $lng); + $group = $refinery->custom(); + + $less_than_3 = $group->constraint( + static fn($value): bool => $value < 3, + 'not_less_than_3' + ); + + $less_than_5 = $group->constraint( + static fn($value): bool => $value < 5, + 'not_less_than_5' + ); + + return $refinery->logical()->parallel([$less_than_3, $less_than_5]); } } diff --git a/components/ILIAS/Refinery/tests/Logical/Constraint/SequentialConstraintTest.php b/components/ILIAS/Refinery/tests/Logical/Constraint/SequentialConstraintTest.php index 94cdb6089344..7418f2b2f04a 100755 --- a/components/ILIAS/Refinery/tests/Logical/Constraint/SequentialConstraintTest.php +++ b/components/ILIAS/Refinery/tests/Logical/Constraint/SequentialConstraintTest.php @@ -26,35 +26,12 @@ class SequentialConstraintTest extends TestCase { private DataFactory $df; - private ILIAS\Language\Language $lng; - private Refinery $refinery; private Constraint $c; protected function setUp(): void { $this->df = new DataFactory(); - $this->lng = $this->createMock(ILIAS\Language\Language::class); - $this->refinery = new Refinery($this->df, $this->lng); - - $group = $this->refinery->custom(); - - $greater_than_3 = $group->constraint( - function ($value) { - return $value > 3; - }, - "not_greater_than_3" - ); - - $less_than_5 = $group->constraint( - function ($value) { - return $value < 5; - }, - "not_less_than_5" - ); - - $this->c = $this->refinery - ->logical() - ->sequential([$greater_than_3, $less_than_5]); + $this->c = $this->sequentialConstraint($this->createStub(ILIAS\Language\Language::class)); } public function testAccepts(): void @@ -86,20 +63,22 @@ public function testNoProblemWith(): void public function testProblemWith1(): void { - $this->lng + $lng = $this->createMock(ILIAS\Language\Language::class); + $lng ->expects($this->never()) - ->method("txt"); + ->method('txt'); - $this->assertEquals("not_greater_than_3", $this->c->problemWith(2)); + $this->assertEquals('not_greater_than_3', $this->sequentialConstraint($lng)->problemWith(2)); } public function testProblemWith2(): void { - $this->lng + $lng = $this->createMock(ILIAS\Language\Language::class); + $lng ->expects($this->never()) - ->method("txt"); + ->method('txt'); - $this->assertEquals("not_less_than_5", $this->c->problemWith(6)); + $this->assertEquals('not_less_than_5', $this->sequentialConstraint($lng)->problemWith(6)); } public function testRestrictOk(): void @@ -120,7 +99,7 @@ public function testRestrictNotOk(): void public function testRestrictError(): void { - $error = $this->df->error("error"); + $error = $this->df->error('error'); $res = $this->c->applyTo($error); $this->assertSame($error, $res); @@ -128,9 +107,25 @@ public function testRestrictError(): void public function testWithProblemBuilder(): void { - $new_c = $this->c->withProblemBuilder(static function (): string { - return "This was a fault"; - }); - $this->assertEquals("This was a fault", $new_c->problemWith(7)); + $new_c = $this->c->withProblemBuilder(static fn(): string => 'This was a fault'); + $this->assertEquals('This was a fault', $new_c->problemWith(7)); + } + + private function sequentialConstraint(ILIAS\Language\Language $lng): Constraint + { + $refinery = new Refinery($this->df, $lng); + $group = $refinery->custom(); + + $greater_than_3 = $group->constraint( + fn($value) => $value > 3, + 'not_greater_than_3' + ); + + $less_than_5 = $group->constraint( + fn($value) => $value < 5, + 'not_less_than_5' + ); + + return $refinery->logical()->sequential([$greater_than_3, $less_than_5]); } } diff --git a/components/ILIAS/Refinery/tests/Logical/GroupTest.php b/components/ILIAS/Refinery/tests/Logical/GroupTest.php index 00b1ee74d86d..27d350672a9f 100755 --- a/components/ILIAS/Refinery/tests/Logical/GroupTest.php +++ b/components/ILIAS/Refinery/tests/Logical/GroupTest.php @@ -31,21 +31,20 @@ use ILIAS\Refinery\Logical\Group as LogicalGroup; use PHPUnit\Framework\TestCase; use ILIAS\Language\Language; +use PHPUnit\Framework\MockObject\Stub; class GroupTest extends TestCase { private LogicalGroup $group; private DataFactory $dataFactory; - private Language $language; + private Language&Stub $language; private Constraint $greaterThanConstraint; private Constraint $lessThanConstaint; protected function setUp(): void { $this->dataFactory = new DataFactory(); - $this->language = $this->getMockBuilder(Language::class) - ->disableOriginalConstructor() - ->getMock(); + $this->language = $this->createStub(Language::class); $this->group = new LogicalGroup($this->dataFactory, $this->language); diff --git a/components/ILIAS/Refinery/tests/Null/Constraint/IsNullConstraintTest.php b/components/ILIAS/Refinery/tests/Null/Constraint/IsNullConstraintTest.php index a53d11475654..d1b2a34cb3b4 100755 --- a/components/ILIAS/Refinery/tests/Null/Constraint/IsNullConstraintTest.php +++ b/components/ILIAS/Refinery/tests/Null/Constraint/IsNullConstraintTest.php @@ -25,17 +25,12 @@ class IsNullConstraintTest extends TestCase { private DataFactory $df; - private ILIAS\Language\Language $lng; - private Refinery $f; private \ILIAS\Refinery\Constraint $c; protected function setUp(): void { $this->df = new DataFactory(); - $this->lng = $this->createMock(ILIAS\Language\Language::class); - $this->f = new Refinery($this->df, $this->lng); - - $this->c = $this->f->null(); + $this->c = $this->nullConstraint($this->createStub(ILIAS\Language\Language::class)); } public function testAccepts(): void @@ -67,13 +62,14 @@ public function testNoProblemWith(): void public function testProblemWith(): void { - $this->lng + $lng = $this->createMock(ILIAS\Language\Language::class); + $lng ->expects($this->once()) - ->method("txt") - ->with("not_a_null") - ->willReturn("-%s-"); + ->method('txt') + ->with('not_a_null') + ->willReturn('-%s-'); - $this->assertEquals("-integer-", $this->c->problemWith(2)); + $this->assertEquals('-integer-', $this->nullConstraint($lng)->problemWith(2)); } public function testRestrictOk(): void @@ -94,7 +90,7 @@ public function testRestrictNotOk(): void public function testRestrictError(): void { - $error = $this->df->error("error"); + $error = $this->df->error('error'); $res = $this->c->applyTo($error); $this->assertSame($error, $res); @@ -102,9 +98,12 @@ public function testRestrictError(): void public function testWithProblemBuilder(): void { - $new_c = $this->c->withProblemBuilder(static function (): string { - return "This was a fault"; - }); - $this->assertEquals("This was a fault", $new_c->problemWith(2)); + $new_c = $this->c->withProblemBuilder(static fn(): string => 'This was a fault'); + $this->assertEquals('This was a fault', $new_c->problemWith(2)); + } + + private function nullConstraint(ILIAS\Language\Language $lng): \ILIAS\Refinery\Constraint + { + return (new Refinery($this->df, $lng))->null(); } } diff --git a/components/ILIAS/Refinery/tests/Numeric/GroupTest.php b/components/ILIAS/Refinery/tests/Numeric/GroupTest.php index ba480429ccf8..1f4dfd9b9ecb 100755 --- a/components/ILIAS/Refinery/tests/Numeric/GroupTest.php +++ b/components/ILIAS/Refinery/tests/Numeric/GroupTest.php @@ -25,19 +25,18 @@ use ILIAS\Refinery\Numeric\Group as NumericGroup; use PHPUnit\Framework\TestCase; use ILIAS\Language\Language; +use PHPUnit\Framework\MockObject\Stub; class GroupTest extends TestCase { private NumericGroup $group; private DataFactory $dataFactory; - private Language $language; + private Language&Stub $language; protected function setUp(): void { $this->dataFactory = new DataFactory(); - $this->language = $this->getMockBuilder(Language::class) - ->disableOriginalConstructor() - ->getMock(); + $this->language = $this->createStub(Language::class); $this->group = new NumericGroup($this->dataFactory, $this->language); } diff --git a/components/ILIAS/Refinery/tests/Numeric/Validation/IsNumericConstraintTest.php b/components/ILIAS/Refinery/tests/Numeric/Validation/IsNumericConstraintTest.php index 773649e46df0..9b9b017daaf4 100755 --- a/components/ILIAS/Refinery/tests/Numeric/Validation/IsNumericConstraintTest.php +++ b/components/ILIAS/Refinery/tests/Numeric/Validation/IsNumericConstraintTest.php @@ -20,23 +20,20 @@ use ILIAS\Refinery\Factory as Refinery; use ILIAS\Data\Factory as DataFactory; +use PHPUnit\Framework\MockObject\Stub; use PHPUnit\Framework\TestCase; class IsNumericConstraintTest extends TestCase { private DataFactory $df; - private ILIAS\Language\Language $lng; - private Refinery $f; + private ILIAS\Language\Language&Stub $lng; private \ILIAS\Refinery\Constraint $c; protected function setUp(): void { $this->df = new DataFactory(); - $this->lng = $this->createMock(ILIAS\Language\Language::class); - - $this->f = new Refinery($this->df, $this->lng); - - $this->c = $this->f->numeric()->isNumeric(); + $this->lng = $this->createStub(ILIAS\Language\Language::class); + $this->c = $this->numericConstraint($this->lng); } public function testAccepts1(): void @@ -46,7 +43,7 @@ public function testAccepts1(): void public function testAccepts2(): void { - $this->assertTrue($this->c->accepts("1")); + $this->assertTrue($this->c->accepts('1')); } public function testAccepts3(): void @@ -86,7 +83,7 @@ public function testNotAccepts1(): void public function testNotAccepts2(): void { - $this->assertFalse($this->c->accepts("foo")); + $this->assertFalse($this->c->accepts('foo')); } public function testCheckSucceed(): void @@ -99,12 +96,9 @@ public function testCheckFails(): void { $this->lng ->method('txt') - ->willReturnCallback(static function (string $value): string { - return $value; - }) - ; + ->willReturnCallback(static fn(string $value): string => $value); $this->expectException(\UnexpectedValueException::class); - $this->c->check(""); + $this->c->check(''); } public function testNoProblemWith(): void @@ -114,13 +108,14 @@ public function testNoProblemWith(): void public function testProblemWith(): void { - $this->lng + $lng = $this->createMock(ILIAS\Language\Language::class); + $lng ->expects($this->once()) - ->method("txt") - ->with("not_numeric") - ->willReturn("-%s-"); + ->method('txt') + ->with('not_numeric') + ->willReturn('-%s-'); - $this->assertEquals("-aa-", $this->c->problemWith("aa")); + $this->assertEquals('-aa-', $this->numericConstraint($lng)->problemWith('aa')); } public function testRestrictOk(): void @@ -133,21 +128,22 @@ public function testRestrictOk(): void public function testRestrictNotOk(): void { - $not_ok = $this->df->ok(""); - - $this->lng + $lng = $this->createMock(ILIAS\Language\Language::class); + $lng ->expects($this->once()) - ->method("txt") - ->with("not_numeric_empty_string") - ->willReturn("-%s-"); + ->method('txt') + ->with('not_numeric_empty_string') + ->willReturn('-%s-'); + + $not_ok = $this->df->ok(''); - $res = $this->c->applyTo($not_ok); + $res = $this->numericConstraint($lng)->applyTo($not_ok); $this->assertFalse($res->isOk()); } public function testRestrictError(): void { - $error = $this->df->error("error"); + $error = $this->df->error('error'); $res = $this->c->applyTo($error); $this->assertSame($error, $res); @@ -155,9 +151,12 @@ public function testRestrictError(): void public function testWithProblemBuilder(): void { - $new_c = $this->c->withProblemBuilder(static function (): string { - return "This was a fault"; - }); - $this->assertEquals("This was a fault", $new_c->problemWith("")); + $new_c = $this->c->withProblemBuilder(static fn(): string => 'This was a fault'); + $this->assertEquals('This was a fault', $new_c->problemWith('')); + } + + private function numericConstraint(ILIAS\Language\Language $lng): \ILIAS\Refinery\Constraint + { + return (new Refinery($this->df, $lng))->numeric()->isNumeric(); } } diff --git a/components/ILIAS/Refinery/tests/Parser/ABNF/BrickTest.php b/components/ILIAS/Refinery/tests/Parser/ABNF/BrickTest.php index 96e5aed025cf..4a007a035023 100755 --- a/components/ILIAS/Refinery/tests/Parser/ABNF/BrickTest.php +++ b/components/ILIAS/Refinery/tests/Parser/ABNF/BrickTest.php @@ -29,6 +29,7 @@ use ILIAS\Refinery\Transformation; use PHPUnit\Framework\TestCase; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\MockObject\Stub; class BrickTest extends TestCase { @@ -40,10 +41,12 @@ public function testConstruct(): void public function testApply(): void { $expected = 'aloha'; - $ok = $this->getMockBuilder(Result::class)->getMock(); + /** @var Result&Stub $ok */ + $ok = $this->createStub(Result::class); $ok->method('isOk')->willReturn(true); $ok->method('value')->willReturn($expected); - $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock(); + /** @var Intermediate&Stub $intermediate */ + $intermediate = $this->createStub(Intermediate::class); $intermediate->method('done')->willReturn(true); $intermediate->method('transform')->willReturnCallback(fn() => $ok); $brick = new Brick(); @@ -259,10 +262,12 @@ public function testTransformation(): void { $brick = new Brick(); - $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock(); + /** @var Intermediate&Stub $intermediate */ + $intermediate = $this->createStub(Intermediate::class); $intermediate->method('done')->willReturn(true); - $ok = $this->getMockBuilder(Result::class)->getMock(); + /** @var Result&Stub $ok */ + $ok = $this->createStub(Result::class); $ok->method('isOk')->willReturn(true); $ok->method('value')->willReturn($intermediate); $ok->method('map')->willReturn($ok); diff --git a/components/ILIAS/Refinery/tests/Parser/ABNF/PrimitivesTest.php b/components/ILIAS/Refinery/tests/Parser/ABNF/PrimitivesTest.php index f0bd9964cf1b..06e466e077a3 100755 --- a/components/ILIAS/Refinery/tests/Parser/ABNF/PrimitivesTest.php +++ b/components/ILIAS/Refinery/tests/Parser/ABNF/PrimitivesTest.php @@ -28,6 +28,7 @@ use ILIAS\Refinery\Parser\ABNF\Primitives; use PHPUnit\Framework\TestCase; use Exception; +use PHPUnit\Framework\MockObject\Stub; class PrimitivesTest extends TestCase { @@ -74,7 +75,8 @@ public function testUntilZero(): void throw new Exception('Should not be called.'); }); - $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock(); + /** @var Intermediate&Stub $intermediate */ + $intermediate = $this->createStub(Intermediate::class); $result = $parser($intermediate, static fn(Result $x): Result => $x); $this->assertTrue($result->isOk()); @@ -93,7 +95,8 @@ public function testUntilN(): void return $cc(new Ok($x)); }); - $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock(); + /** @var Intermediate&Stub $intermediate */ + $intermediate = $this->createStub(Intermediate::class); $result = $parser($intermediate, static function (Result $x) use (&$end_called, $i): Result { $end_called++; @@ -122,7 +125,8 @@ public function testUntilSuccess(): void return $cc(new Ok($x)); }); - $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock(); + /** @var Intermediate&Stub $intermediate */ + $intermediate = $this->createStub(Intermediate::class); $result = $parser($intermediate, static function (Result $x) use (&$end_called, $success_after): Result { $end_called++; @@ -145,7 +149,8 @@ public function testUntilChildFails(): void return $called <= $fail_after ? $cc(new Ok($x)) : $cc(new Error('Failed.')); }); - $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock(); + /** @var Intermediate&Stub $intermediate */ + $intermediate = $this->createStub(Intermediate::class); $result = $parser($intermediate, static function (Result $x) use (&$end_called): Result { $end_called++; @@ -171,7 +176,8 @@ public function testParserFromString(): void { $primitives = new Primitives(); - $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock(); + /** @var Intermediate&Stub $intermediate */ + $intermediate = $this->createStub(Intermediate::class); $intermediate->method('value')->willReturnOnConsecutiveCalls(ord('h'), ord('e'), ord('l'), ord('l'), ord('o')); $intermediate->method('accept')->willReturn(new Ok($intermediate)); diff --git a/components/ILIAS/Refinery/tests/Parser/ABNF/TransformTest.php b/components/ILIAS/Refinery/tests/Parser/ABNF/TransformTest.php index 08c06900f71b..3d1795875f45 100755 --- a/components/ILIAS/Refinery/tests/Parser/ABNF/TransformTest.php +++ b/components/ILIAS/Refinery/tests/Parser/ABNF/TransformTest.php @@ -30,6 +30,7 @@ use Closure; use stdClass; use PHPUnit\Framework\Attributes\Depends; +use PHPUnit\Framework\MockObject\Stub; class TransformTest extends TestCase { @@ -43,7 +44,8 @@ public function testTo(): void { $transform = new Transform(); $transformed = new stdClass(); - $transformation = $this->getMockBuilder(Transformation::class)->getMock(); + /** @var Transformation&Stub $transformation */ + $transformation = $this->createStub(Transformation::class); $transformation->method('applyTo')->willReturnCallback(function (Result $x) use ($transformed): Result { $this->assertEquals('x', $x->value()); return new Ok($transformed); @@ -67,7 +69,8 @@ public function testTo(): void public function testFailedToTransform(): void { $transform = new Transform(); - $transformation = $this->getMockBuilder(Transformation::class)->getMock(); + /** @var Transformation&Stub $transformation */ + $transformation = $this->createStub(Transformation::class); $transformation->method('applyTo')->willReturnCallback(function (Result $x): Result { $this->assertEquals('x', $x->value()); return new Error('Sorryy.'); @@ -92,7 +95,8 @@ public function testFailedToParse(): void { $transform = new Transform(); $transformCalled = false; - $transformation = $this->getMockBuilder(Transformation::class)->getMock(); + /** @var Transformation&Stub $transformation */ + $transformation = $this->createStub(Transformation::class); $transformation->method('applyTo')->willReturnCallback(function (Result $x) use (&$transformCalled): Result { $transformCalled = true; return $x; diff --git a/components/ILIAS/Refinery/tests/Password/Constraint/PasswordContraintsTest.php b/components/ILIAS/Refinery/tests/Password/Constraint/PasswordContraintsTest.php index 8e7ea1b6585a..0ca55f295b7c 100755 --- a/components/ILIAS/Refinery/tests/Password/Constraint/PasswordContraintsTest.php +++ b/components/ILIAS/Refinery/tests/Password/Constraint/PasswordContraintsTest.php @@ -83,7 +83,6 @@ public function toJS($key): void } /** - * @param Constraint $constraint * @param ILIAS\Data\Password[] $ok_values * @param ILIAS\Data\Password[] $error_values */ diff --git a/components/ILIAS/Refinery/tests/Password/GroupTest.php b/components/ILIAS/Refinery/tests/Password/GroupTest.php index f0857bf5b18c..0ff4fd1e0ab5 100755 --- a/components/ILIAS/Refinery/tests/Password/GroupTest.php +++ b/components/ILIAS/Refinery/tests/Password/GroupTest.php @@ -29,19 +29,18 @@ use ILIAS\Refinery\Password\Group as PasswordGroup; use PHPUnit\Framework\TestCase; use ILIAS\Language\Language; +use PHPUnit\Framework\MockObject\Stub; class GroupTest extends TestCase { private PasswordGroup $group; private DataFactory $dataFactory; - private Language $language; + private Language&Stub $language; protected function setUp(): void { $this->dataFactory = new DataFactory(); - $this->language = $this->getMockBuilder(Language::class) - ->disableOriginalConstructor() - ->getMock(); + $this->language = $this->createStub(Language::class); $this->group = new PasswordGroup($this->dataFactory, $this->language); } diff --git a/components/ILIAS/Refinery/tests/String/Constraints/HasMaxLengthConstraintTest.php b/components/ILIAS/Refinery/tests/String/Constraints/HasMaxLengthConstraintTest.php index cb9934f2d083..6d3f97f5a47c 100755 --- a/components/ILIAS/Refinery/tests/String/Constraints/HasMaxLengthConstraintTest.php +++ b/components/ILIAS/Refinery/tests/String/Constraints/HasMaxLengthConstraintTest.php @@ -26,70 +26,69 @@ class HasMaxLengthConstraintTest extends TestCase { private DataFactory $df; - private ILIAS\Language\Language $lng; private int $max_length; private Constraint $c; protected function setUp(): void { $this->df = new DataFactory(); - $this->lng = $this->createMock(ILIAS\Language\Language::class); - $this->max_length = 2; - $this->c = new HasMaxLength( $this->max_length, $this->df, - $this->lng + $this->createStub(ILIAS\Language\Language::class) ); } public function testAccepts1(): void { - $this->assertTrue($this->c->accepts("12")); + $this->assertTrue($this->c->accepts('12')); } public function testAccepts2(): void { - $this->assertTrue($this->c->accepts("1")); + $this->assertTrue($this->c->accepts('1')); } public function testNotAccepts(): void { - $this->assertFalse($this->c->accepts("123")); + $this->assertFalse($this->c->accepts('123')); } public function testCheckSucceed(): void { - $this->c->check("12"); + $this->c->check('12'); $this->assertTrue(true); // does not throw } public function testCheckFails(): void { $this->expectException(\UnexpectedValueException::class); - $this->c->check("123"); + $this->c->check('123'); } public function testNoProblemWith(): void { - $this->assertNull($this->c->problemWith("12")); + $this->assertNull($this->c->problemWith('12')); } public function testProblemWith(): void { - $this->lng + $lng = $this->createMock(ILIAS\Language\Language::class); + $lng ->expects($this->once()) - ->method("txt") - ->with("not_max_length") - ->willReturn("-%s-"); + ->method('txt') + ->with('not_max_length') + ->willReturn('-%s-'); + + $constraint = new HasMaxLength($this->max_length, $this->df, $lng); - $this->assertEquals("-2-", $this->c->problemWith("123")); + $this->assertEquals('-2-', $constraint->problemWith('123')); } public function testRestrictOk(): void { - $ok = $this->df->ok("12"); + $ok = $this->df->ok('12'); $res = $this->c->applyTo($ok); $this->assertTrue($res->isOk()); @@ -97,7 +96,7 @@ public function testRestrictOk(): void public function testRestrictNotOk(): void { - $not_ok = $this->df->ok("123"); + $not_ok = $this->df->ok('123'); $res = $this->c->applyTo($not_ok); $this->assertFalse($res->isOk()); @@ -105,7 +104,7 @@ public function testRestrictNotOk(): void public function testRestrictError(): void { - $error = $this->df->error("error"); + $error = $this->df->error('error'); $res = $this->c->applyTo($error); $this->assertSame($error, $res); @@ -113,9 +112,7 @@ public function testRestrictError(): void public function testWithProblemBuilder(): void { - $new_c = $this->c->withProblemBuilder(static function (): string { - return "This was a fault"; - }); - $this->assertEquals("This was a fault", $new_c->problemWith("123")); + $new_c = $this->c->withProblemBuilder(static fn(): string => 'This was a fault'); + $this->assertEquals('This was a fault', $new_c->problemWith('123')); } } diff --git a/components/ILIAS/Refinery/tests/String/Constraints/HasMinLengthConstraintTest.php b/components/ILIAS/Refinery/tests/String/Constraints/HasMinLengthConstraintTest.php index 6c4d27e9cae9..d592c7f5b4ec 100755 --- a/components/ILIAS/Refinery/tests/String/Constraints/HasMinLengthConstraintTest.php +++ b/components/ILIAS/Refinery/tests/String/Constraints/HasMinLengthConstraintTest.php @@ -26,70 +26,69 @@ class HasMinLengthConstraintTest extends TestCase { private DataFactory $df; - private ILIAS\Language\Language $lng; private int $min_length; private Constraint $c; protected function setUp(): void { $this->df = new DataFactory(); - $this->lng = $this->createMock(ILIAS\Language\Language::class); - $this->min_length = 10; - $this->c = new HasMinLength( $this->min_length, $this->df, - $this->lng + $this->createStub(ILIAS\Language\Language::class) ); } public function testAccepts1(): void { - $this->assertTrue($this->c->accepts("1234567890")); + $this->assertTrue($this->c->accepts('1234567890')); } public function testAccepts2(): void { - $this->assertTrue($this->c->accepts("12345678901")); + $this->assertTrue($this->c->accepts('12345678901')); } public function testNotAccepts(): void { - $this->assertFalse($this->c->accepts("123456789")); + $this->assertFalse($this->c->accepts('123456789')); } public function testCheckSucceed(): void { - $this->c->check("1234567890"); + $this->c->check('1234567890'); $this->assertTrue(true); // does not throw } public function testCheckFails(): void { $this->expectException(UnexpectedValueException::class); - $this->c->check(""); + $this->c->check(''); } public function testNoProblemWith(): void { - $this->assertNull($this->c->problemWith("1234567890")); + $this->assertNull($this->c->problemWith('1234567890')); } public function testProblemWith(): void { - $this->lng + $lng = $this->createMock(ILIAS\Language\Language::class); + $lng ->expects($this->once()) - ->method("txt") - ->with("not_min_length") - ->willReturn("-%s-%s-"); + ->method('txt') + ->with('not_min_length') + ->willReturn('-%s-%s-'); + + $constraint = new HasMinLength($this->min_length, $this->df, $lng); - $this->assertEquals("-3-10-", $this->c->problemWith("123")); + $this->assertEquals('-3-10-', $constraint->problemWith('123')); } public function testRestrictOk(): void { - $ok = $this->df->ok("1234567890"); + $ok = $this->df->ok('1234567890'); $res = $this->c->applyTo($ok); $this->assertTrue($res->isOk()); @@ -97,7 +96,7 @@ public function testRestrictOk(): void public function testRestrictNotOk(): void { - $not_ok = $this->df->ok("1234"); + $not_ok = $this->df->ok('1234'); $res = $this->c->applyTo($not_ok); $this->assertFalse($res->isOk()); @@ -105,7 +104,7 @@ public function testRestrictNotOk(): void public function testRestrictError(): void { - $error = $this->df->error("error"); + $error = $this->df->error('error'); $res = $this->c->applyTo($error); $this->assertSame($error, $res); @@ -113,9 +112,7 @@ public function testRestrictError(): void public function testWithProblemBuilder(): void { - $new_c = $this->c->withProblemBuilder(static function (): string { - return "This was a fault"; - }); - $this->assertEquals("This was a fault", $new_c->problemWith("")); + $new_c = $this->c->withProblemBuilder(static fn(): string => 'This was a fault'); + $this->assertEquals('This was a fault', $new_c->problemWith('')); } } diff --git a/components/ILIAS/Refinery/tests/String/EstimatedReadingTimeTest.php b/components/ILIAS/Refinery/tests/String/EstimatedReadingTimeTest.php index a598016bfdb0..acf7da6e7297 100755 --- a/components/ILIAS/Refinery/tests/String/EstimatedReadingTimeTest.php +++ b/components/ILIAS/Refinery/tests/String/EstimatedReadingTimeTest.php @@ -48,8 +48,8 @@ class EstimatedReadingTimeTest extends TestCase protected function setUp(): void { $this->refinery = new Refinery( - $this->createMock(DataFactory::class), - $this->createMock(Language::class) + $this->createStub(DataFactory::class), + $this->createStub(Language::class) ); parent::setUp(); diff --git a/components/ILIAS/Refinery/tests/String/GroupTest.php b/components/ILIAS/Refinery/tests/String/GroupTest.php index 28c015ab164e..c082ef43ee44 100755 --- a/components/ILIAS/Refinery/tests/String/GroupTest.php +++ b/components/ILIAS/Refinery/tests/String/GroupTest.php @@ -26,6 +26,7 @@ use ILIAS\Refinery\String\HasMaxLength; use PHPUnit\Framework\TestCase; use ILIAS\Refinery\String\MakeClickable; +use PHPUnit\Framework\MockObject\Stub; class GroupTest extends TestCase { @@ -34,9 +35,8 @@ class GroupTest extends TestCase protected function setUp(): void { $dataFactory = new DataFactory(); - $language = $this->getMockBuilder(\ILIAS\Language\Language::class) - ->disableOriginalConstructor() - ->getMock(); + /** @var \ILIAS\Language\Language&Stub $language */ + $language = $this->createStub(\ILIAS\Language\Language::class); $this->group = new StringGroup($dataFactory, $language); } diff --git a/components/ILIAS/Refinery/tests/String/LevenshteinTest.php b/components/ILIAS/Refinery/tests/String/LevenshteinTest.php index dd20d1fb5bcb..31a8e35ed3b0 100755 --- a/components/ILIAS/Refinery/tests/String/LevenshteinTest.php +++ b/components/ILIAS/Refinery/tests/String/LevenshteinTest.php @@ -24,133 +24,126 @@ use ILIAS\Refinery\String\Group; use PHPUnit\Framework\TestCase; use InvalidArgumentException; +use PHPUnit\Framework\MockObject\Stub; class LevenshteinTest extends TestCase { - /** - * @var Group - */ - private $group; - - /** - * @var Factory - */ - private $factory; + private Group $group; + private Factory $factory; /** * @var string multibyte string with three bytes per character */ - private $test_multibyte_string = "你好"; + private string $test_multibyte_string = '你好'; /** * @var string containing emoji */ - private $test_emoji = "😮‍💨"; + private string $test_emoji = '😮‍💨'; - public function setUp(): void + protected function setUp(): void { $this->factory = new Factory(); - $language = $this->getMockBuilder(\ILIAS\Language\Language::class) - ->disableOriginalConstructor() - ->getMock(); + /** @var \ILIAS\Language\Language&Stub $language */ + $language = $this->createStub(\ILIAS\Language\Language::class); $this->group = new Group($this->factory, $language); } // Code paths - public function testSizeReturn() + public function testSizeReturn(): void { - $transformation = $this->group->levenshtein()->standard("book", 3); + $transformation = $this->group->levenshtein()->standard('book', 3); - $this->assertEquals(-1.0, $transformation->transform("bookshelf")); + $this->assertEquals(-1.0, $transformation->transform('bookshelf')); } - public function testExceedMaximumDistance() + public function testExceedMaximumDistance(): void { - $transformation = $this->group->levenshtein()->standard("book", 1); + $transformation = $this->group->levenshtein()->standard('book', 1); - $this->assertEquals(-1.0, $transformation->transform("back")); + $this->assertEquals(-1.0, $transformation->transform('back')); } - public function testSuccessfulReturn() + public function testSuccessfulReturn(): void { - $transformation = $this->group->levenshtein()->standard("book", 1); + $transformation = $this->group->levenshtein()->standard('book', 1); - $this->assertEquals(0.0, $transformation->transform("book")); + $this->assertEquals(0.0, $transformation->transform('book')); } - public function testNoMaximumDistance() + public function testNoMaximumDistance(): void { - $transformation = $this->group->levenshtein()->standard("book", 0); + $transformation = $this->group->levenshtein()->standard('book', 0); - $this->assertEquals(2.0, $transformation->transform("back")); + $this->assertEquals(2.0, $transformation->transform('back')); } - public function testException() + public function testException(): void { - $transformation = $this->group->levenshtein()->standard("book", 0); + $transformation = $this->group->levenshtein()->standard('book', 0); $this->expectException(InvalidArgumentException::class); $this->assertEquals(2.0, $transformation->transform(496)); } - public function testNoMaxEscapeButOverTheLimit() + public function testNoMaxEscapeButOverTheLimit(): void { - $transformation = $this->group->levenshtein()->standard("Juni", 2); + $transformation = $this->group->levenshtein()->standard('Juni', 2); - $this->assertEquals(-1, $transformation->transform("Januar")); + $this->assertEquals(-1, $transformation->transform('Januar')); } // Numerical - public function testCustomCostsMixed() + public function testCustomCostsMixed(): void { - $transformation = $this->group->levenshtein()->custom("back", 20, 2.0, 1.0, 1.5); + $transformation = $this->group->levenshtein()->custom('back', 20, 2.0, 1.0, 1.5); - $this->assertEquals(12, $transformation->transform("bookshelf")); + $this->assertEquals(12, $transformation->transform('bookshelf')); } - public function testCustomCostsInsert() + public function testCustomCostsInsert(): void { - $transformation = $this->group->levenshtein()->custom("book", 5, 2.0, 1.0, 1.5); + $transformation = $this->group->levenshtein()->custom('book', 5, 2.0, 1.0, 1.5); - $this->assertEquals(2, $transformation->transform("books")); + $this->assertEquals(2, $transformation->transform('books')); } - public function testCustomCostsDeletion() + public function testCustomCostsDeletion(): void { - $transformation = $this->group->levenshtein()->custom("bookshelf", 10, 2.0, 1.0, 1.5); + $transformation = $this->group->levenshtein()->custom('bookshelf', 10, 2.0, 1.0, 1.5); - $this->assertEquals(7.5, $transformation->transform("book")); + $this->assertEquals(7.5, $transformation->transform('book')); } - public function testCustomCostsReplacement() + public function testCustomCostsReplacement(): void { - $transformation = $this->group->levenshtein()->custom("bookshelf", 10, 2.0, 1.0, 1.5); + $transformation = $this->group->levenshtein()->custom('bookshelf', 10, 2.0, 1.0, 1.5); - $this->assertEquals(4.0, $transformation->transform("bookstore")); + $this->assertEquals(4.0, $transformation->transform('bookstore')); } // test apply to - public function testApplyToSuccessfulDefault() + public function testApplyToSuccessfulDefault(): void { - $transformation = $this->group->levenshtein()->standard("bookshelf", 10); - $value_object = $this->factory->ok("book"); + $transformation = $this->group->levenshtein()->standard('bookshelf', 10); + $value_object = $this->factory->ok('book'); $result_object = $transformation->applyTo($value_object); $this->assertEquals(5, $result_object->value()); } - public function testApplyToSuccessfulCustomCost() + public function testApplyToSuccessfulCustomCost(): void { - $transformation = $this->group->levenshtein()->custom("bookshelf", 10, 2.0, 1.0, 1.5); - $value_object = $this->factory->ok("book"); + $transformation = $this->group->levenshtein()->custom('bookshelf', 10, 2.0, 1.0, 1.5); + $value_object = $this->factory->ok('book'); $result_object = $transformation->applyTo($value_object); $this->assertEquals(7.5, $result_object->value()); } - public function testApplyToWrongType() + public function testApplyToWrongType(): void { - $transformation = $this->group->levenshtein()->custom("bookshelf", 10, 2.0, 1.0, 1.5); + $transformation = $this->group->levenshtein()->custom('bookshelf', 10, 2.0, 1.0, 1.5); $value_object = $this->factory->ok(496); $result_object = $transformation->applyTo($value_object); @@ -158,23 +151,23 @@ public function testApplyToWrongType() } // test Multibyte strings: - public function testMultibyteStringThreeByte() + public function testMultibyteStringThreeByte(): void { $transformation = $this->group->levenshtein()->custom($this->test_multibyte_string, 10, 2.0, 1.0, 1.5); $this->assertEquals(0, $transformation->transform($this->test_multibyte_string)); } - public function testEmoji() + public function testEmoji(): void { - $transformation = $this->group->levenshtein()->custom("book", 20, 2.0, 1.0, 1.5); + $transformation = $this->group->levenshtein()->custom('book', 20, 2.0, 1.0, 1.5); $this->assertEquals(4.5, $transformation->transform($this->test_emoji)); } - public function testApplyToMultibyteString() + public function testApplyToMultibyteString(): void { - $transformation = $this->group->levenshtein()->custom("bookshelf", 20, 2.0, 1.0, 1.5); + $transformation = $this->group->levenshtein()->custom('bookshelf', 20, 2.0, 1.0, 1.5); $value_object = $this->factory->ok($this->test_multibyte_string); $result_object = $transformation->applyTo($value_object); diff --git a/components/ILIAS/Refinery/tests/String/MarkdownFormattingToHTMLTest.php b/components/ILIAS/Refinery/tests/String/MarkdownFormattingToHTMLTest.php index 007349503b51..fe7912ff6b85 100755 --- a/components/ILIAS/Refinery/tests/String/MarkdownFormattingToHTMLTest.php +++ b/components/ILIAS/Refinery/tests/String/MarkdownFormattingToHTMLTest.php @@ -26,6 +26,7 @@ use ilLanguage; use ILIAS\Refinery\Transformation; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\MockObject\Stub; class MarkdownFormattingToHTMLTest extends TestCase { @@ -33,9 +34,8 @@ class MarkdownFormattingToHTMLTest extends TestCase protected function setUp(): void { - $language = $this->getMockBuilder(ilLanguage::class) - ->disableOriginalConstructor() - ->getMock(); + /** @var ilLanguage&Stub $language */ + $language = $this->createStub(ilLanguage::class); $group = new Group(new Factory(), $language); $this->markdown = $group->markdown()->toHTML(); @@ -44,15 +44,15 @@ protected function setUp(): void public static function stringProvider(): array { return [ - ["lorem", "

lorem

\n"], - ["lorem **ipsum**", "

lorem ipsum

\n"], - ["_lorem_ **ipsum**", "

lorem ipsum

\n"], - ["# Headline", "

Headline

\n"], - ["## Headline", "

Headline

\n"], - ["### Headline", "

Headline

\n"], + ['lorem', "

lorem

\n"], + ['lorem **ipsum**', "

lorem ipsum

\n"], + ['_lorem_ **ipsum**', "

lorem ipsum

\n"], + ['# Headline', "

Headline

\n"], + ['## Headline', "

Headline

\n"], + ['### Headline', "

Headline

\n"], ["1. Lorem\n2. Ipsum", "
    \n
  1. Lorem
  2. \n
  3. Ipsum
  4. \n
\n"], ["- Lorem\n- Ipsum", "\n"], - ["[Link Titel](https://www.ilias.de)", "

Link Titel

\n"], + ['[Link Titel](https://www.ilias.de)', "

Link Titel

\n"], ]; } diff --git a/components/ILIAS/Refinery/tests/String/StripTagsTest.php b/components/ILIAS/Refinery/tests/String/StripTagsTest.php index b531e46937df..5a652e19708a 100755 --- a/components/ILIAS/Refinery/tests/String/StripTagsTest.php +++ b/components/ILIAS/Refinery/tests/String/StripTagsTest.php @@ -22,11 +22,12 @@ use ILIAS\Data\Factory as DataFactory; use ILIAS\Refinery\Transformation; use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\MockObject\Stub; class StripTagsTest extends TestCase { - private const STRING_TO_STRIP = "I