Skip to content

Commit

Permalink
Merge branch 'feature/unit_test_grinding' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
guelzow committed Dec 23, 2024
2 parents 3501d78 + 526ae55 commit 7268c75
Show file tree
Hide file tree
Showing 20 changed files with 340 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Service/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public function loadConfigValue(string $name): Config
*/
public function getChunkDir(): string
{
return $this->directories[static::CHUNKS_FOLDER];
return $this->directories[static::CHUNKS_FOLDER] ?? '';
}

/**
Expand All @@ -272,7 +272,7 @@ public function getChunkDir(): string
*/
public function getLogDir(): string
{
return $this->directories[static::LOG_FOLDER];
return $this->directories[static::LOG_FOLDER] ?? '';
}

/**
Expand All @@ -283,7 +283,7 @@ public function getLogDir(): string
*/
public function getPathToConfigFile(): string
{
return $this->directories[static::CONFIG_FOLDER];
return $this->directories[static::CONFIG_FOLDER] ?? '';
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Service/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function __construct($data)
* @param \ReflectionProperty $refProperty
* The reflection of the property we are analysing.
*
* @return mixed;
* @return mixed
* The retrieved value.
*/
public function retrieveValue(ReflectionProperty $refProperty)
Expand Down Expand Up @@ -154,7 +154,7 @@ protected function retrieveEsotericValue(ReflectionProperty $refProperty)
// accessing the object directly.
return array_values($this->objectArray)[
array_search($propName, array_keys($this->objectArray))
];
] ?? null;
}

if ($refProperty instanceof HiddenProperty) {
Expand Down
2 changes: 1 addition & 1 deletion src/View/Output/Chunks.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public function addMetadata(array $caller): void
*/
public function __destruct()
{
if (!isset($this->chunkDir)) {
if (empty($this->chunkDir)) {
return;
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Fixtures/MagicMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Brainworxx\Krexx\Tests\Fixtures;

class MagicMethods
{
public function __get($something)
{

}
}
1 change: 0 additions & 1 deletion tests/Fixtures/SerializableFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

class SerializableFixture implements SerializableInterface
{

protected $data;

public function __construct()
Expand Down
51 changes: 51 additions & 0 deletions tests/Helpers/OutputNothing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* kreXX: Krumo eXXtended
*
* kreXX is a debugging tool, which displays structured information
* about any PHP object. It is a nice replacement for print_r() or var_dump()
* which are used by a lot of PHP developers.
*
* kreXX is a fork of Krumo, which was originally written by:
* Kaloyan K. Tsvetkov <[email protected]>
*
* @author
* brainworXX GmbH <[email protected]>
*
* @license
* http://opensource.org/licenses/LGPL-2.1
*
* GNU Lesser General Public License Version 2.1
*
* kreXX Copyright (C) 2014-2024 Brainworxx GmbH
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

namespace Brainworxx\Krexx\Tests\Helpers;

use Brainworxx\Krexx\View\Output\AbstractOutput;

class OutputNothing extends AbstractOutput
{
public function finalize(): void
{
// Do nothing.
}

public function getChunkStrings(): array
{
return $this->chunkStrings;
}
}
1 change: 1 addition & 0 deletions tests/Scripts/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
AbstractHelper::defineFunctionMock($serviceMisc, 'glob');
AbstractHelper::defineFunctionMock($serviceMisc, 'time');
AbstractHelper::defineFunctionMock($serviceMisc, 'fopen');
AbstractHelper::defineFunctionMock($serviceMisc, 'mb_convert_encoding');
AbstractHelper::defineFunctionMock($callbackScalar, 'class_exists');
AbstractHelper::defineFunctionMock($callbackScalar, 'is_file');
AbstractHelper::defineFunctionMock($callbackScalar, 'function_exists');
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/Controller/BacktraceControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Brainworxx\Krexx\Service\Flow\Emergency;
use Brainworxx\Krexx\Tests\Helpers\CallbackNothing;
use Brainworxx\Krexx\View\Output\Browser;
use Brainworxx\Krexx\View\Output\Chunks;
use PHPUnit\Framework\Attributes\CoversMethod;

#[CoversMethod(BacktraceController::class, 'backtraceAction')]
Expand Down Expand Up @@ -75,6 +76,31 @@ public function testBacktraceActionWithMaxCall()
$this->assertEquals($backtraceController, $backtraceController->backtraceAction());
}

/**
* Test it with a triggered emergency break.
*/
public function testBacktraceActionWithEmergency()
{
$backtraceController = new BacktraceController(Krexx::$pool);

// Mix it, this time with a different kind of mox.
$emergencyMock = $this->createMock(Emergency::class);
$emergencyMock->expects($this->once())
->method('checkMaxCall')
->willReturn(false);
$emergencyMock->expects($this->any())
->method('checkEmergencyBreak')
->willReturn(true);
Krexx::$pool->emergencyHandler = $emergencyMock;

$chunksMock = $this->createMock(Chunks::class);
$chunksMock->expects($this->never())
->method('addMetadata');
Krexx::$pool->chunks = $chunksMock;

$this->assertEquals($backtraceController, $backtraceController->backtraceAction());
}

/**
* Testing a simple backtrace.
*/
Expand Down
34 changes: 34 additions & 0 deletions tests/Unit/Controller/DumpControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
use Brainworxx\Krexx\Krexx;
use Brainworxx\Krexx\Service\Factory\Event;
use Brainworxx\Krexx\Service\Flow\Emergency;
use Brainworxx\Krexx\Service\Misc\File;
use Brainworxx\Krexx\Tests\Helpers\CallbackNothing;
use Brainworxx\Krexx\Tests\Helpers\OutputNothing;
use Brainworxx\Krexx\View\Output\Browser;
use Brainworxx\Krexx\View\Output\Chunks;
use PHPUnit\Framework\Attributes\CoversMethod;
Expand Down Expand Up @@ -157,6 +159,38 @@ public function testDumpActionSpecialCases()
Krexx::$pool->chunks = $chunkMock;

$dumpController->dumpAction($fixture, $message);
}

/**
* Test if the outputFooter can handle
* - No configuration file
* - No minimized JS files
* - No minimized CSS files.
*/
public function testDumpActionWithFooterHandling()
{
$dumpController = new DumpController(Krexx::$pool);
$fixture = null;
$expectation = Krexx::$pool->messages->getHelp('configFileNotFound');

$fileServiceMock = $this->createMock(File::class);
$fileServiceMock->expects($this->any())
->method('fileIsReadable')
->willReturn(false);
Krexx::$pool->fileService = $fileServiceMock;

$outputService = new OutputNothing(Krexx::$pool);
$this->setValueByReflection('outputService', $outputService, $dumpController);
$emergencyMock = $this->createMock(Emergency::class);
$emergencyMock->expects($this->any())
->method('checkEmergencyBreak')
->willReturn(false);
Krexx::$pool->emergencyHandler = $emergencyMock;
$this->setValueByReflection('jsCssSend', [], $dumpController);

$dumpController->dumpAction($fixture);

$result = $outputService->getChunkStrings()[2];
$this->assertStringContainsString($expectation, $result);
}
}
12 changes: 12 additions & 0 deletions tests/Unit/Service/Flow/RecursionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ public function testIsInHive()
$this->assertFalse($this->recursion->isInHive($GLOBALS));
$this->assertTrue($this->recursion->isInHive($GLOBALS), 'Render them a second time');
}

// And now the same thing with an array.
$fixture = [];
$this->assertFalse($this->recursion->isInHive($fixture));
$this->assertFalse($this->recursion->isInHive($fixture), 'We do not track arrays');
$fixture[$this->recursion->getMarker()] = true;

if (version_compare(phpversion(), '8.1.0', '>=')) {
// 8.1.0 does not have globals anymore.
$this->assertFalse($this->recursion->isInHive($fixture), 'Pretend that this is the global array.');
$this->assertTrue($this->recursion->isInHive($fixture), 'We did track it.');
}
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Service/Misc/EncodingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ public function testEncodeStringNormal()
$fixture = 'just another string <div> { @' . chr(9);
$expected = 'just another string &lt;div&gt; &#123; &#64;&nbsp;&nbsp;';
$this->assertEquals($expected, $this->encoding->encodeString($fixture, true));

$fixture = random_bytes(102401);
$expected = Krexx::$pool->messages->getHelp('stringTooLarge');
$this->assertEquals($expected, $this->encoding->encodeString($fixture));
}

/**
* We test it with a completely broken string.
*/
public function testEncodeStringCompletelyBroken()
{
$mbConvertEncodingMock = $this->getFunctionMock(
'\\Brainworxx\\Krexx\\Service\\Misc\\',
'mb_convert_encoding'
);
$mbConvertEncodingMock->expects($this->once())->willReturn('');
$fixture = random_bytes(50);
$expected = '';
$this->assertEquals($expected, $this->encoding->encodeString($fixture));
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Service/Misc/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@ public function testReadFile()
$this->file->readFile($reflection->getFileName(), -41, -45),
'Test it with nonsense from to stuff.'
);

$this->assertEquals(
'',
$this->file->readFile('NoFile', 42, 48),
'Test it with a none existing file.'
);

$this->assertEquals(
'',
$this->file->readFile($reflection->getFileName(), 85, 999),
'Read more than the file has to offer. We expect the last line which is empty.'
);
}

/**
Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/Service/Reflection/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Brainworxx\Krexx\Tests\Fixtures\ComplexMethodFixture;
use Brainworxx\Krexx\Tests\Fixtures\InheritDocFixture;
use Brainworxx\Krexx\Tests\Fixtures\InterfaceFixture;
use Brainworxx\Krexx\Tests\Fixtures\MagicMethods;
use Brainworxx\Krexx\Tests\Fixtures\PublicFixture;
use Brainworxx\Krexx\Tests\Fixtures\SimpleFixture;
use Brainworxx\Krexx\Tests\Helpers\AbstractHelper;
Expand Down Expand Up @@ -142,6 +143,46 @@ public function testRetrieveValue()
}
}

/**
* We deliberately throw an error when retrieving the value.
*/
public function testRetrieveValueWithErrors()
{
// Doing it with an undeclared property.
$reflectionMock = $this->createMock(UndeclaredProperty::class);
$reflectionMock->expects($this->exactly(2))
->method('getName')
->willReturn('5');
$reflectionMock->expects($this->once())
->method('isStatic')
->willThrowException(new \Exception());
$reflection = new ReflectionClass(new \stdClass());

$this->assertNull(
$reflection->retrieveValue($reflectionMock),
'There never was a value in the first place.'
);

// Again, but with a hidden property.
$reflectionMock = $this->createMock(HiddenProperty::class);
$reflectionMock->expects($this->exactly(2))
->method('getName')
->willReturn('Tobi');
$reflectionMock->expects($this->once())
->method('isStatic')
->willThrowException(new \Exception());
$fixture = $this->createMock(MagicMethods::class);
$fixture->expects($this->once())
->method('__get')
->willThrowException(new \Exception());

$reflection = new ReflectionClass($fixture);
$this->assertNull(
$reflection->retrieveValue($reflectionMock),
'There never was a value in the first place.'
);
}

/**
* Test the retrieval of the actually implemented interfaces of this class.
*/
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/View/Output/ChunksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,19 @@ public function testDestruct()
$chunks->__destruct();
}

/**
* Test the cleanup without any directory to clean up.
*/
public function testDestructWithoutChunkDir()
{
$chunks = new Chunks(Krexx::$pool);
$globMock = $this->getFunctionMock('\\Brainworxx\\Krexx\\View\\Output\\', 'glob');
$globMock->expects($this->never());
$this->setValueByReflection('chunkDir', '', $chunks);

$chunks->__destruct();
}

/**
* Test the encoding detection.
*/
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/View/Skins/AbstractRenderHans.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ abstract class AbstractRenderHans extends AbstractHelper
public const GET_DATA = 'getData';
public const GET_TYPE = 'getType';
public const RENDER_ME = 'renderMe';
public const GET_RETURN_TYPE = 'getReturnType';

/**
* @var \PHPUnit\Framework\MockObject\MockObject
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/View/Skins/AbstractRenderSmokyGrey.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ abstract class AbstractRenderSmokyGrey extends AbstractHelper
public const GET_TYPE = 'getType';
public const RENDER_ME = 'renderMe';
public const GET_CONNECTOR_LANGUAGE = 'getConnectorLanguage';
public const GET_RETURN_TYPE = 'getReturnType';

/**
* @var \PHPUnit\Framework\MockObject\MockObject
Expand Down
Loading

0 comments on commit 7268c75

Please sign in to comment.