Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Resolve issue with tests using table() #31447

Merged
merged 4 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/Illuminate/Foundation/Testing/MockStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Illuminate\Foundation\Testing;

use ErrorException;
use Symfony\Component\Console\Output\OutputInterface;

class MockStream
{
/**
* @var \Symfony\Component\Console\Output\OutputInterface
*/
protected static $output;

/**
* Register a new stream wrapper using the protocol mock://.
*
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
public static function register(OutputInterface $output)
{
self::unregister();

self::$output = $output;

stream_wrapper_register('mock', self::class);
}

/**
* Attempt to restore the stream wrapper to the previous state, if it existed.
*
* @return void
*/
public static function restore()
{
self::unregister();

try {
stream_wrapper_restore('mock');
} catch (ErrorException $e) {
//
}
}

/**
* Unregister the mock:// stream wrapper.
*
* @return void
*/
protected static function unregister()
{
if (in_array('mock', stream_get_wrappers())) {
stream_wrapper_unregister('mock');
}
}

public function stream_open($path, $mode, $options, &$opened_path)
{
return true;
}

public function stream_write($data)
{
self::$output->doWrite($data, true);

return strlen($data);
}
}
18 changes: 18 additions & 0 deletions src/Illuminate/Foundation/Testing/PendingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use Mockery;
use Mockery\Exception\NoMatchingExpectationException;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\Output;

class PendingCommand
{
Expand Down Expand Up @@ -193,6 +196,19 @@ private function createABufferedOutputMock()
->shouldAllowMockingProtectedMethods()
->shouldIgnoreMissing();

MockStream::register($mock);
$stream = fopen('mock://stream', 'r+');
$consoleOutputSections = [];

$mock->shouldReceive('section')
->andReturn(new ConsoleSectionOutput(
$stream,
$consoleOutputSections,
Output::VERBOSITY_NORMAL,
false,
new OutputFormatter)
);

foreach ($this->test->expectedOutput as $i => $output) {
$mock->shouldReceive('doWrite')
->once()
Expand All @@ -218,5 +234,7 @@ public function __destruct()
}

$this->run();

MockStream::restore();
}
}
44 changes: 44 additions & 0 deletions tests/Foundation/Testing/MockStreamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Illuminate\Tests\Foundation\Testing;

use ErrorException;
use Illuminate\Foundation\Testing\MockStream;
use Mockery;
use Orchestra\Testbench\TestCase;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\StreamOutput;

class MockStreamTest extends TestCase
{
public function testMockStreamWritesToOutputInterface()
{
$mock = Mockery::mock(BufferedOutput::class.'[doWrite]')
->shouldAllowMockingProtectedMethods();

$mock->shouldReceive('doWrite')
->with('Taylor', true)
->once();

MockStream::register($mock);
$stream = fopen('mock://stream', 'r+');
$output = new StreamOutput($stream);
$output->write('Taylor');
MockStream::restore();
}

public function testMockStreamIsUnregisteredOnRestore()
{
MockStream::register(new BufferedOutput);
MockStream::restore();

$failed = false;
try {
fopen('mock://test', 'r+');
} catch (ErrorException $e) {
$failed = true;
}

$this->assertTrue($failed);
}
}