Skip to content

Commit

Permalink
Improve types and test functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Nov 27, 2024
1 parent a975378 commit 5811d7d
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 33 deletions.
6 changes: 3 additions & 3 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 @@ -146,7 +146,7 @@ public function load_file(string $identifier): void
/**
* Convert an input array recursively into a Configuration class hierarchy.
*
* @param array<int|string,mixed> $array Input array
* @param mixed $array Input array or scalar
*
* @return array<int|string,mixed> An array with sub-arrays converted
*/
Expand Down Expand Up @@ -242,7 +242,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
6 changes: 3 additions & 3 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 Expand Up @@ -105,7 +105,7 @@ public function testForeach(): void
++$iteration;
}

$this->assertEquals($iteration, count($this->class));
$this->assertCount($iteration, $this->class);
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/Lunr/Core/Tests/ConfigurationBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function testNextIncreasesPosition(): void
public function testCountIsZero(): void
{
$this->assertEquals(0, $this->class->count());
$this->assertEquals(0, count($this->class));
$this->assertCount(0, $this->class);
}

}
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
18 changes: 3 additions & 15 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,8 +85,7 @@ public function testLoadFileMergesArrays(): void
*/
public function testLoadInvalidFile(): void
{
$property = $this->reflection->getProperty('config');
$property->setAccessible(TRUE);
$property = $this->get_reflection_property('config');

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

Expand All @@ -122,8 +112,7 @@ public function testLoadNonExistingFile(): void
$this->expectException('\PHPUnit_Framework_Error');
}

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

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

Expand All @@ -139,8 +128,7 @@ public function testLoadNonExistingFile(): void
*/
public function testLoadFileInvalidatesSize(): void
{
$property = $this->reflection->getProperty('size_invalid');
$property->setAccessible(TRUE);
$property = $this->get_reflection_property('size_invalid');

$this->assertFalse($property->getValue($this->class));

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 5811d7d

Please sign in to comment.