Skip to content

Commit

Permalink
Refactored protected property access in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PHLAK committed Mar 20, 2020
1 parent 888b544 commit 34eabed
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions tests/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function test_it_has_a_default_internal_encoding()
{
$string = new Twine\Str();

$this->assertAttributeEquals('UTF-8', 'encoding', $string);
$this->assertEquals('UTF-8', $this->getProperty($string, 'encoding'));
}

public function test_it_can_override_the_default_internal_encoding()
Expand All @@ -104,25 +104,24 @@ public function test_it_can_override_the_default_internal_encoding()
Twine\Config\Str::setEncoding('ASCII');
$ascii = new Twine\Str();

$this->assertAttributeEquals('UTF-8', 'encoding', $utf8);
$this->assertEquals('UTF-8', $this->getProperty($utf8, 'encoding'));
$this->assertEquals('ASCII', mb_detect_encoding($ascii));
}

/**
* Custom attribute assertion.
* Retrieve a protected propert from an object.
*
* @param mixed $expected
* @param string $property
* @param object $object
* @param string $property
*
* @return void
* @return mixed
*/
protected function assertAttributeEquals($expected, string $property, object $object): void
protected function getProperty(object $object, string $property)
{
$reflection = new ReflectionClass($object);
$property = $reflection->getProperty($property);
$property->setAccessible(true);

$this->assertEquals($expected, $property->getValue($object));
return $property->getValue($object);
}
}

0 comments on commit 34eabed

Please sign in to comment.