Skip to content

Commit

Permalink
Even more unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
guelzow committed Dec 27, 2024
1 parent f92da98 commit 4b325f5
Show file tree
Hide file tree
Showing 17 changed files with 276 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/Analyse/Callback/Iterate/ThroughLargeArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function callMe(): string
// Meh, the only reason for the recursion marker
// in arrays is because of the $GLOBAL array, which
// we will only render once.
// @deprecated Will be removed when we drop 8.0 support
if ($key === $recursionMarker) {
continue;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Analyse/Callback/Iterate/ThroughProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ protected function retrieveValueStatus(ReflectionProperty $refProperty, Reflecti
// In a rather buggy state. When the property is not readonly, this may
// trigger an
// "Error : Internal error: Failed to retrieve the reflection object".
// It was later fixed in PHP 8.1.?.
// @deprecated The try catch will be removed.
try {
if ($refProperty->isReadOnly()) {
$additional .= $messages->getHelp('readonly') . ' ';
Expand Down
9 changes: 6 additions & 3 deletions src/Analyse/Routing/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public function __construct(Pool $pool)
$this->processors[ProcessFloat::class] = $pool->createClass(ProcessFloat::class);
$this->processors[ProcessNull::class] = $pool->createClass(ProcessNull::class);
$this->processors[ProcessResource::class] = $pool->createClass(ProcessResource::class);
$this->processors[ProcessOther::class] = $pool->createClass(ProcessOther::class);

$pool->routing = $this;
}
Expand Down Expand Up @@ -111,7 +110,11 @@ public function analysisHub(Model $model): string
}
}

// The ProcessOther should prevent this.
return '';
// Looks like we ran out of processors.
/** @var ProcessOther $processOther */
$processOther = $this->pool->createClass(ProcessOther::class);
$processOther->canHandle($model);

return $processOther->handle();
}
}
11 changes: 3 additions & 8 deletions src/Analyse/Scalar/String/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,9 @@ public static function isActive(): bool
*/
public function canHandle($string, Model $model): bool
{
try {
if (is_callable($string)) {
$this->handledValue = $string;
return true;
}
} catch (Throwable $exception) {
// Do nothing.
// The autoloading just failed.
if (is_callable($string)) {
$this->handledValue = $string;
return true;
}

return false;
Expand Down
3 changes: 2 additions & 1 deletion src/Analyse/Scalar/String/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function canHandle($string, Model $model): bool
return false;
}

// @deprecated Will be removed once we drop 8.3 support.
if (function_exists('json_validate') && json_validate($string)) {
// Doing it the PHP 8.3 way.
$this->model = $model;
Expand Down Expand Up @@ -122,7 +123,7 @@ protected function handle(): array
{
if (empty($this->decodedJson)) {
// We will not decode it again, if we already have a result.
// The "if" will be removed in PHP 8.3.
// @deprecated The "if" will be removed in PHP 8.3.
$this->decodedJson = json_decode($this->handledValue);
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Fixtures/DeepGetterFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ class DeepGetterFixture
*/
protected $false = false;

/**
* This is clearly uppercase.
*
* @var int
*/
protected $Uppercase = 1;

/**
* Things!
*
* @return int
*/
public function Theuppercase(): int
{
return $this->Uppercase;
}

/**
* @return string
*/
Expand Down
4 changes: 4 additions & 0 deletions tests/Scripts/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@
$callbackScalar = '\\Brainworxx\\Krexx\\Analyse\\Scalar\\String\\';
$caller = '\\Brainworxx\\Krexx\\Analyse\\Caller\\';
$factory = '\\Brainworxx\\Krexx\\Service\\Factory\\';
$scalaString = '\\Brainworxx\\Krexx\\Analyse\\Scalar\String\\';

AbstractHelper::defineFunctionMock($analyseRoutingProcess, 'class_exists');
AbstractHelper::defineFunctionMock($analyseRoutingProcess, 'is_object');
AbstractHelper::defineFunctionMock($analyseRoutingProcess, 'is_resource');
AbstractHelper::defineFunctionMock($analyseRoutingProcess, 'debug_backtrace');
AbstractHelper::defineFunctionMock($serviceFlow, 'ini_get');
AbstractHelper::defineFunctionMock($serviceFlow, 'time');
AbstractHelper::defineFunctionMock($serviceFlow, 'memory_get_usage');
Expand All @@ -75,6 +77,8 @@
AbstractHelper::defineFunctionMock($view, 'defined');
AbstractHelper::defineFunctionMock($caller, 'time');
AbstractHelper::defineFunctionMock($factory, 'getmypid');
AbstractHelper::defineFunctionMock($scalaString, 'class_exists');


// Register a shutdown method to die, so we get no output on the shell.
register_shutdown_function(function () {
Expand Down
31 changes: 31 additions & 0 deletions tests/Unit/Analyse/Callback/Iterate/ThroughPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Brainworxx\Krexx\Analyse\Callback\Iterate\ThroughProperties;
use Brainworxx\Krexx\Analyse\Declaration\PropertyDeclaration;
use Brainworxx\Krexx\Analyse\Model;
use Brainworxx\Krexx\Service\Flow\Emergency;
use Brainworxx\Krexx\Service\Reflection\ReflectionClass;
use Brainworxx\Krexx\Service\Reflection\UndeclaredProperty;
use Brainworxx\Krexx\Tests\Fixtures\AttributesFixture;
Expand Down Expand Up @@ -519,6 +520,36 @@ public function testCallMeAttributes()
);
}

public function testCallMeEmergency()
{
// Test the events.
// We do not expect a stop event.
$this->mockEventService([$this->startEvent, $this->throughProperties]);

// Make sure to stop everything in its tracks.
$emergencyHandlerMock = $this->createMock(Emergency::class);
$emergencyHandlerMock->expects($this->once())
->method('checkEmergencyBreak')
->willReturn(true);
Krexx::$pool->emergencyHandler = $emergencyHandlerMock;

// Create a fixture.
$subject = new AttributesFixture();
$fixture = [
$this->throughProperties::PARAM_REF => new ReflectionClass($subject),
$this->throughProperties::PARAM_DATA => [
new ReflectionProperty(AttributesFixture::class, static::PROPERTY),
]
];

// Run the test
$this->assertEquals(
'',
$this->throughProperties->setParameters($fixture)->callMe(),
'We use the normal routing. If there is any HTML in there, the break has failed!'
);
}

/**
* Special tests for PHP 8, actually with some 7.4'er stuff.
*/
Expand Down
59 changes: 59 additions & 0 deletions tests/Unit/Analyse/Caller/CallerFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

namespace Brainworxx\Krexx\Tests\Unit\Analyse\Caller;

use Brainworxx\Krexx\Analyse\Callback\CallbackConstInterface;
use Brainworxx\Krexx\Analyse\Caller\AbstractCaller;
use Brainworxx\Krexx\Analyse\Caller\BacktraceConstInterface;
use Brainworxx\Krexx\Analyse\Caller\CallerFinder;
Expand Down Expand Up @@ -254,6 +255,64 @@ public function testFindCallerUnreadableSource()
$this->assertArrayHasKey(BacktraceConstInterface::TRACE_DATE, $result);
}

/**
* Test the finding without a valid url.
*/
public function testFindCallerNoUrl()
{
$this->mockDebugBacktrace()
->expects($this->once())
->willReturn($this->createFixture(75));

// We need a different pool mock.
$poolMock = $this->createMock(Pool::class);
$poolMock->expects($this->any())
->method('getServer')
->willReturn([
'SERVER_PROTOCOL' => 'abcd/',
'SERVER_PORT' => 123,
'SERVER_NAME' => 'localhorst',
'HTTPS' => 'on'
]);
$poolMock->fileService = Krexx::$pool->fileService;
$poolMock->encodingService = Krexx::$pool->encodingService;
$poolMock->config = Krexx::$pool->config;
$poolMock->emergencyHandler = Krexx::$pool->emergencyHandler;
$poolMock->messages = Krexx::$pool->messages;
$poolMock->expects($this->any())
->method('createClass')
->willReturnCallback(function ($classname) {
return Krexx::$pool->createClass($classname);
});
$this->callerFinder = new CallerFinder($poolMock);

// Run the test
$result = $this->callerFinder->findCaller('', $this->subjectVar);
$this->assertEquals('n/a', $result[BacktraceConstInterface::TRACE_URL]);
}

/**
* Manipulate the lookup array to prevent finding anything.
*/
public function testFindCallerNoResult()
{
$this->mockDebugBacktrace()
->expects($this->once())
->willReturn($this->createFixture(75));
$this->setValueByReflection('callPattern', [], $this->callerFinder);

// Run the test
$result = $this->callerFinder->findCaller('', $this->subjectVar);
$this->assertEquals(
CallbackConstInterface::UNKNOWN_VALUE,
$result[BacktraceConstInterface::TRACE_VARNAME]
);
$this->assertEquals(
'Analysis of ' . CallbackConstInterface::UNKNOWN_VALUE . ', string',
$result[BacktraceConstInterface::TRACE_TYPE]
);
}

/**
* Test the caller finder with the forced logger.
*/
Expand Down
15 changes: 14 additions & 1 deletion tests/Unit/Analyse/Code/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use Brainworxx\Krexx\Analyse\Code\Scope;
use Brainworxx\Krexx\Analyse\Model;
use Brainworxx\Krexx\Service\Flow\Emergency;
use Brainworxx\Krexx\Tests\Fixtures\PublicFixture;
use Brainworxx\Krexx\Tests\Helpers\AbstractHelper;
use Brainworxx\Krexx\Krexx;
use stdClass;
Expand Down Expand Up @@ -121,8 +122,9 @@ public function testTestModelForCodegen()
$object = new stdClass();
$array = [];
$string = 'whatever';
$objectWithPrivate = new PublicFixture();

// No genereation for 'some' scope.
// No generation for 'some' scope.
$this->setNestingLevel(1);
$this->scope->setScope('some');
$model = new Model(Krexx::$pool);
Expand All @@ -144,6 +146,17 @@ public function testTestModelForCodegen()
$model->setData($object);
$this->assertTrue($this->scope->testModelForCodegen($model));

// Code generation with a real class on level 1
$messages = Krexx::$pool->messages;
$this->setNestingLevel(1);
$model->setData($objectWithPrivate)
->setType(
$messages->getHelp('private') . ' ' . $messages->getHelp('inherited')
);
$this->scope->setScope('$this');
$this->assertFalse($this->scope->testModelForCodegen($model));
$model->setType('');

// Code generation for a level 2 array.
$this->setNestingLevel(2);
$model->setData($array);
Expand Down
9 changes: 8 additions & 1 deletion tests/Unit/Analyse/Getter/ByMethodNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,14 @@ public function testRetrieveItDeep()
'expectation' => true,
'propertyName' => 'myPropertyOne',
'hasResult' => true
]
],
[
'reflection' => $classReflection->getMethod('Theuppercase'),
'prefix' => 'get',
'expectation' => null,
'propertyName' => 'Uppercase',
'hasResult' => false
],
];
$this->validateResults($fixture, $classReflection);
}
Expand Down
31 changes: 26 additions & 5 deletions tests/Unit/Analyse/Routing/Process/ProcessBacktraceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
#[CoversMethod(ProcessBacktrace::class, 'getBacktrace')]
class ProcessBacktraceTest extends AbstractHelper
{

protected function setUp(): void
/**
* Mock the debug backtrace, to provide a fixture.
*/
protected function mockDebugBacktrace()
{
parent::setUp();

$data = 'data';
$someFile = 'some file';
$debugBacktrace = $this->getFunctionMock('\\Brainworxx\\Krexx\\Analyse\\Routing\\Process\\', 'debug_backtrace');
Expand Down Expand Up @@ -95,6 +95,7 @@ public function testConstruct()
public function testProcessNormal()
{
$this->mockEmergencyHandler();
$this->mockDebugBacktrace();

// Inject the RenderNothing.
$renderNothing = new RenderNothing(Krexx::$pool);
Expand Down Expand Up @@ -159,6 +160,7 @@ public function testProcessNormal()
public function testProcessEmpty()
{
$this->mockEmergencyHandler();
$this->mockDebugBacktrace();

// Inject the RenderNothing.
$renderNothing = new RenderNothing(Krexx::$pool);
Expand All @@ -182,7 +184,7 @@ public function testProcessEmpty()

// Check the parameters
$data = 'data';
$someFile = $orgPath = 'some file';
$orgPath = 'some file';
for ($i = 0; $i <= 2; $i++) {
/** @var \Brainworxx\Krexx\Analyse\Model $model */
$model = $renderNothing->model['renderExpandableChild'][$i];
Expand Down Expand Up @@ -213,4 +215,23 @@ public function testProcessEmpty()
);
}
}

/**
* We mock the debug_backtrace, and make it return an empty value to
* simulate a tiny backtrace.
*/
public function testProcessReallyEmpty()
{
$this->mockEmergencyHandler();

$backtraceMock = $this->getFunctionMock(
'\\Brainworxx\\Krexx\\Analyse\\Routing\\Process\\',
'debug_backtrace'
);
$backtraceMock->expects($this->once())
->willReturn([]);

$processBacktrace = new ProcessBacktrace(Krexx::$pool);
$this->assertEquals('', $processBacktrace->handle());
}
}
Loading

0 comments on commit 4b325f5

Please sign in to comment.