Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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));
Expand Down
73 changes: 37 additions & 36 deletions components/ILIAS/Refinery/tests/Custom/Constraint/CustomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,57 +25,45 @@
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
{
$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);
}
Expand All @@ -91,32 +79,45 @@ 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);
}

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();
}
};
}
}
7 changes: 3 additions & 4 deletions components/ILIAS/Refinery/tests/Custom/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions components/ILIAS/Refinery/tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@
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
{
private Refinery $basicFactory;

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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));
}
}
Loading
Loading