Skip to content

Commit

Permalink
[FIX] PHP-linter: Cannot redeclare exec() in ExecMockFunctions.php
Browse files Browse the repository at this point in the history
  • Loading branch information
dkd-kaehm committed Aug 15, 2023
1 parent 5c04545 commit 3a4aae6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?php

/** Don't define strict_types on this place because of inclusion in {@link ExecRecorder} */
/**
* Don't define strict_types on this place because of inclusion in {@link ExecRecorder}
*
* Note:
* This file has extension ".php.Hack", to prevent PHP-Linter raise FATALs like:
* "PHP Fatal error: Cannot redeclare exec() in ./Tests/Unit/ExecMockFunctions.php on line 0XX"
*
* If you want to edit this file, please associate this file as php in your IDE manually.
*/

/*
* This file is part of the TYPO3 CMS project.
Expand All @@ -19,25 +27,19 @@

/**
* exec() mock to capture invocation parameters for the actual \exec() function
*
* @param string $command
* @param array|null $output
* @param int|null $result_code
*/
function exec(string $command, array &$output = null, int &$result_code = null)
function exec(string $command, array &$output = null, int &$result_code = null): string|false
{
$output = array_key_exists(ExecRecorder::$execCalled, ExecRecorder::$execOutput) ? ExecRecorder::$execOutput[ExecRecorder::$execCalled] : '';
ExecRecorder::$execCalled++;
ExecRecorder::$execCommand = $command;
return '';
}

/**
* shell_exec() mock to capture invocation parameters for the actual \shell_exec() function
*
* @param $command
* @return string|false|null
*/
function shell_exec($command)
function shell_exec(string $command): string|false|null
{
$output = array_key_exists(ExecRecorder::$execCalled, ExecRecorder::$execOutput) ? ExecRecorder::$execOutput[ExecRecorder::$execCalled] : '';
ExecRecorder::$execCalled++;
Expand Down
12 changes: 3 additions & 9 deletions Tests/Unit/ExecRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
namespace ApacheSolrForTypo3\Tika\Tests\Unit;

// load the mocked functions into the namespaces which need them during tests
// include() or require() cannot load into namespaces
// include() or require() cannot load into namespaces,
// so we use this little trick to achieve the effect
eval('namespace ApacheSolrForTypo3\Tika { ?>' . file_get_contents(__DIR__ . '/ExecMockFunctions.php') . ' }');
eval('namespace ApacheSolrForTypo3\Tika\Service\Tika { ?>' . file_get_contents(__DIR__ . '/ExecMockFunctions.php') . ' }');
eval('namespace ApacheSolrForTypo3\Tika { ?>' . file_get_contents(__DIR__ . '/ExecMockFunctions.php.Hack') . ' }');
eval('namespace ApacheSolrForTypo3\Tika\Service\Tika { ?>' . file_get_contents(__DIR__ . '/ExecMockFunctions.php.Hack') . ' }');

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----

Expand All @@ -36,22 +36,16 @@ class ExecRecorder
{
/**
* Allows to capture exec() parameters
*
* @var string
*/
public static string $execCommand = '';

/**
* Output to return to exec() calls
*
* @var array
*/
public static array $execOutput = [];

/**
* Indicator whether/how many times the exec() mock was called.
*
* @var int
*/
public static int $execCalled = 0;

Expand Down

0 comments on commit 3a4aae6

Please sign in to comment.