Skip to content

Commit

Permalink
General: Add types and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev authored and pprkut committed Dec 22, 2024
1 parent 307501e commit 2f250dc
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/Lunr/Core/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Configuration implements ArrayAccess, Iterator, Countable
* @param array<int|string,mixed>|bool $bootstrap Bootstrap config values, aka config values used before
* the class has been instantiated.
*/
public function __construct($bootstrap = FALSE)
public function __construct(array|bool $bootstrap = FALSE)
{
if (!is_array($bootstrap))
{
Expand Down Expand Up @@ -248,7 +248,7 @@ public function offsetUnset(mixed $offset): void
*/
public function offsetGet(mixed $offset): mixed
{
return isset($this->config[$offset]) ? $this->config[$offset] : NULL;
return $this->config[$offset] ?? NULL;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Lunr/Core/Tests/ConfigurationArrayAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function setUp(): void
*
* @dataProvider existingConfigPairProvider
*/
public function testOffsetExists($offset): void
public function testOffsetExists(mixed $offset): void
{
$this->assertTrue($this->class->offsetExists($offset));
}
Expand All @@ -46,7 +46,7 @@ public function testOffsetExists($offset): void
*
* @dataProvider nonExistingConfigPairProvider
*/
public function testOffsetDoesNotExist($offset): void
public function testOffsetDoesNotExist(mixed $offset): void
{
$this->assertFalse($this->class->offsetExists($offset));
}
Expand All @@ -58,7 +58,7 @@ public function testOffsetDoesNotExist($offset): void
*
* @dataProvider existingConfigPairProvider
*/
public function testOffsetGetWithExistingOffset($offset): void
public function testOffsetGetWithExistingOffset(mixed $offset): void
{
$this->assertEquals($this->config[$offset], $this->class->offsetGet($offset));
}
Expand All @@ -70,7 +70,7 @@ public function testOffsetGetWithExistingOffset($offset): void
*
* @dataProvider nonExistingConfigPairProvider
*/
public function testOffsetGetWithNonExistingOffset($offset): void
public function testOffsetGetWithNonExistingOffset(mixed $offset): void
{
$this->assertNull($this->class->offsetGet($offset));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lunr/Core/Tests/ConfigurationArrayUsageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testArrayWriteAccess(): void
* @dataProvider existingConfigPairProvider
* @depends Lunr\Core\Tests\ConfigurationArrayAccessTest::testOffsetExists
*/
public function testIssetOnExistingOffset($offset): void
public function testIssetOnExistingOffset(mixed $offset): void
{
$this->assertTrue(isset($this->class[$offset]));
}
Expand All @@ -74,7 +74,7 @@ public function testIssetOnExistingOffset($offset): void
* @dataProvider nonExistingConfigPairProvider
* @depends Lunr\Core\Tests\ConfigurationArrayAccessTest::testOffsetDoesNotExist
*/
public function testIssetOnNonExistingOffset($offset): void
public function testIssetOnNonExistingOffset(mixed $offset): void
{
$this->assertFalse(isset($this->class[$offset]));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lunr/Core/Tests/ConfigurationBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function setUp(): void
*/
public function testConfigIsEmpty(): void
{
$this->assertEmpty($this->get_reflection_property_value('config'));
$this->assertPropertyEmpty('config');
}

/**
Expand Down Expand Up @@ -78,7 +78,7 @@ public function testToString(): void
*/
public function testToArrayIsEmpty(): void
{
$this->assertEquals([], $this->class->toArray());
$this->assertArrayEmpty($this->class->toArray());
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Lunr/Core/Tests/ConfigurationConvertArrayToClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function setUp(): void
*/
public function testConvertArrayToClassWithEmptyArrayValue(): void
{
$method = $this->get_accessible_reflection_method('convert_array_to_class');
$method = $this->get_reflection_method('convert_array_to_class');
$output = $method->invokeArgs($this->class, [ [] ]);

$this->assertArrayEmpty($output);
Expand All @@ -51,7 +51,7 @@ public function testConvertArrayToClassWithArrayValue(): void
$input['test'] = 'String';
$input['test1'] = 1;

$method = $this->get_accessible_reflection_method('convert_array_to_class');
$method = $this->get_reflection_method('convert_array_to_class');
$output = $method->invokeArgs($this->class, [ $input ]);

$this->assertEquals($input, $output);
Expand All @@ -71,17 +71,17 @@ public function testConvertArrayToClassWithMultidimensionalArrayValue(): void
$config['test2']['test3'] = 1;
$config['test2']['test4'] = FALSE;

$method = $this->get_accessible_reflection_method('convert_array_to_class');
$method = $this->get_reflection_method('convert_array_to_class');
$output = $method->invokeArgs($this->class, [ $config ]);

$this->assertTrue(is_array($output));

$this->assertInstanceOf('Lunr\Core\Configuration', $output['test2']);

$property = $this->get_accessible_reflection_property('size');
$property = $this->get_reflection_property('size');
$this->assertEquals(2, $property->getValue($output['test2']));

$property = $this->get_accessible_reflection_property('config');
$property = $this->get_reflection_property('config');
$this->assertEquals($config['test2'], $property->getValue($output['test2']));
}

Expand Down
43 changes: 8 additions & 35 deletions src/Lunr/Core/Tests/ConfigurationLoadFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ public function testLoadCorrectFile(): void
$this->config['load']['one'] = 'Value';
$this->config['load']['two'] = 'String';

$property = $this->reflection->getProperty('config');
$property->setAccessible(TRUE);

$this->assertEquals($this->config, $this->class->toArray());
}

Expand All @@ -53,9 +50,6 @@ public function testLoadCorrectFile(): void
*/
public function testLoadFileOverwritesValues(): void
{
$property = $this->reflection->getProperty('config');
$property->setAccessible(TRUE);

$this->class->load_file('overwrite');

$config = [];
Expand All @@ -74,9 +68,6 @@ public function testLoadFileOverwritesValues(): void
*/
public function testLoadFileMergesArrays(): void
{
$property = $this->reflection->getProperty('config');
$property->setAccessible(TRUE);

$this->class->load_file('merge');

$config = [];
Expand All @@ -94,14 +85,11 @@ public function testLoadFileMergesArrays(): void
*/
public function testLoadInvalidFile(): void
{
$property = $this->reflection->getProperty('config');
$property->setAccessible(TRUE);

$before = $property->getValue($this->class);
$before = $this->get_reflection_property_value('config');

$this->class->load_file('not_array');

$after = $property->getValue($this->class);
$after = $this->get_reflection_property_value('config');

$this->assertEquals($before, $after);
}
Expand All @@ -111,25 +99,13 @@ public function testLoadInvalidFile(): void
*/
public function testLoadNonExistingFile(): void
{
if (class_exists('\PHPUnit\Framework\Error\Error'))
{
// PHPUnit 6
$this->expectException('\PHPUnit\Framework\Error\Error');
}
else
{
// PHPUnit 5
$this->expectException('\PHPUnit_Framework_Error');
}

$property = $this->reflection->getProperty('config');
$property->setAccessible(TRUE);

$before = $property->getValue($this->class);
$this->expectException('\PHPUnit\Framework\Error\Error');

$before = $this->get_reflection_property_value('config');

$this->class->load_file('not_exists');

$after = $property->getValue($this->class);
$after = $this->get_reflection_property_value('config');

$this->assertEquals($before, $after);
}
Expand All @@ -139,14 +115,11 @@ public function testLoadNonExistingFile(): void
*/
public function testLoadFileInvalidatesSize(): void
{
$property = $this->reflection->getProperty('size_invalid');
$property->setAccessible(TRUE);

$this->assertFalse($property->getValue($this->class));
$this->assertPropertyEquals('size_invalid', FALSE);

$this->class->load_file('correct');

$this->assertTrue($property->getValue($this->class));
$this->assertPropertyEquals('size_invalid', TRUE);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function setUp(): void
*/
public function testConfigIsEmpty(): void
{
$this->assertEmpty($this->get_reflection_property_value('config'));
$this->assertPropertyEmpty('config');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Lunr/Core/Tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class ConfigurationTest extends LunrBaseTest
* Default config values.
* @var array
*/
protected $config;
protected array $config;

/**
* Instance of the tested class.
Expand All @@ -53,7 +53,7 @@ protected function setUpNonArray(): void
*
* @return void
*/
protected function setUpArray($config): void
protected function setUpArray(array $config): void
{
$this->config = $config;
$this->class = new Configuration($config);
Expand Down

0 comments on commit 2f250dc

Please sign in to comment.