Skip to content

Commit

Permalink
Update base command class to add support to set reader custom stream
Browse files Browse the repository at this point in the history
  • Loading branch information
nguereza-tony committed Nov 22, 2023
1 parent 838faeb commit e13785e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/Console/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@

namespace Platine\Framework\Console;

use InvalidArgumentException;
use Platine\Console\Command\Command;
use Platine\Console\Input\Reader;
use Platine\Filesystem\Filesystem;
use Platine\Framework\App\Application;
use Platine\Stdlib\Helper\Path;
Expand Down Expand Up @@ -208,4 +210,25 @@ public function createParentDirectory(string $path): void
}
}
}

/**
* Set reader stream content
* @param Reader $reader
* @param string $filename
* @param string|array<string> $data
* @return void
*/
public function setReaderContent(Reader $reader, string $filename, $data): void
{
if (is_array($data)) {
$data = implode(PHP_EOL, $data);
}
file_put_contents($filename, $data, FILE_APPEND);

$resource = fopen($filename, 'r');
if ($resource === false) {
throw new InvalidArgumentException(sprintf('Could not open filename [%s] for reading', $filename));
}
$reader->setStream($resource);
}
}
4 changes: 2 additions & 2 deletions src/Console/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ protected function replaceConstructor(string $content): string

return str_replace('%constructor%', $replaceContent, $content);
}

/**
* Replace the method body
* @param string $content
Expand Down Expand Up @@ -311,7 +311,7 @@ public function __construct(
}
EOF;
}

/**
* Return the constructor content
* @return string
Expand Down
33 changes: 33 additions & 0 deletions tests/Console/Command/MakeActionCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Platine\Test\Framework\Console\Command;

use InvalidArgumentException;
use Platine\Console\Application as ConsoleApp;
use Platine\Console\Input\Reader;
use Platine\Console\IO\Interactor;
use Platine\Filesystem\Adapter\Local\LocalAdapter;
use Platine\Filesystem\Filesystem;
Expand Down Expand Up @@ -287,4 +289,35 @@ public function testGetClassTemplate(): void

$this->assertNotEmpty($o->getClassTemplate());
}

public function testSetReaderContent(): void
{
$localAdapter = new LocalAdapter();
$filesystem = new Filesystem($localAdapter);
$app = $this->getMockInstance(Application::class, []);
$reader = new Reader();

$o = new MakeActionCommand($app, $filesystem);

$o->setReaderContent($reader, $this->vfsInputStream->url(), ['foo']);
$this->assertEquals($reader->read(), 'foo');
}

public function testSetReaderContentInvalidFilenameForRead(): void
{
global $mock_fopen_to_false;

$mock_fopen_to_false = true;

$localAdapter = new LocalAdapter();
$filesystem = new Filesystem($localAdapter);
$app = $this->getMockInstance(Application::class, []);
$reader = new Reader();

$o = new MakeActionCommand($app, $filesystem);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Could not open filename [not_found_reader_filename] for reading');
$o->setReaderContent($reader, 'not_found_reader_filename', ['foo']);
}
}
13 changes: 13 additions & 0 deletions tests/fixtures/mocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,16 @@ function fopen(string $str, string $mode)

return \fopen($str, $mode);
}

namespace Platine\Framework\Console;
$mock_fopen_to_false = false;
function fopen(string $str, string $mode)
{
global $mock_fopen_to_false;

if ($mock_fopen_to_false) {
return false;
}

return \fopen($str, $mode);
}

0 comments on commit e13785e

Please sign in to comment.