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

Fail loud should apply consistently #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ cache:
- $HOME/.composer/cache/files

php:
- 7.0
- 7.1
- 7.2
- 7.3
- nightly

before_install:
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
}
},
"require": {
"php": ">=5.5.0",
"php": ">=7.1.0",
"nacmartin/phpexecjs": "^2.0",
"psr/cache": "^1.0.0",
"psr/log": "^1.1.0",
"twig/twig": "^1.20|^2.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^2.5",
"escapestudios/symfony2-coding-standard": "^2.9",
"phpunit/phpunit": "^7.5.0",
"wimg/php-compatibility": "^7.0"
},
"scripts": {
Expand Down
45 changes: 33 additions & 12 deletions src/Limenius/ReactRenderer/Renderer/ExternalServerReactRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Limenius\ReactRenderer\Context\ContextProviderInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

/**
* Class ExternalServerReactRenderer
Expand Down Expand Up @@ -55,23 +56,43 @@ public function setServerSocketPath($serverSocketPath)
*/
public function render($componentName, $propsString, $uuid, $registeredStores = array(), $trace)
{
if (strpos($this->serverSocketPath, '://') === false) {
$this->serverSocketPath = 'unix://'.$this->serverSocketPath;
}
try {
if (\strpos($this->serverSocketPath, '://') === false) {
$this->serverSocketPath = 'unix://'.$this->serverSocketPath;
}

if (!$sock = stream_socket_client($this->serverSocketPath, $errno, $errstr)) {
throw new \RuntimeException($errstr);
}
stream_socket_sendto($sock, $this->wrap($componentName, $propsString, $uuid, $registeredStores, $trace)."\0");
if (!$sock = \stream_socket_client($this->serverSocketPath, $errno, $errstr)) {
throw new \RuntimeException($errstr);
}
\stream_socket_sendto(
$sock,
$this->wrap($componentName, $propsString, $uuid, $registeredStores, $trace)."\0"
);

$contents = '';

$contents = '';
while (!\feof($sock)) {
$contents .= \fread($sock, 8192);
}
\fclose($sock);

$result = \json_decode($contents, true);
} catch (\Throwable $t) {
if ($this->failLoud) {
throw $t;
}

if ($this->logger) {
$this->logger->log(LogLevel::ERROR, $t->getMessage(), ['exception' => $t]);
}

while (!feof($sock)) {
$contents .= fread($sock, 8192);
return [
'evaluated' => '',
'consoleReplay' => '',
'hasErrors' => true,
];
}
fclose($sock);

$result = json_decode($contents, true);
if ($result['hasErrors']) {
$this->logErrors($result['consoleReplayScript']);
if ($this->failLoud) {
Expand Down
42 changes: 33 additions & 9 deletions src/Limenius/ReactRenderer/Renderer/PhpExecJsReactRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Limenius\ReactRenderer\Renderer;

use Nacmartin\PhpExecJs\PhpExecJs;
use Psr\Log\LoggerInterface;
use Limenius\ReactRenderer\Context\ContextProviderInterface;
use Nacmartin\PhpExecJs\PhpExecJs;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

/**
* Class PhpExecJsReactRenderer
Expand Down Expand Up @@ -93,15 +94,38 @@ public function setServerBundlePath($serverBundlePath)
*/
public function render($componentName, $propsString, $uuid, $registeredStores = array(), $trace)
{
$this->ensurePhpExecJsIsBuilt();
if ($this->needToSetContext) {
if ($this->phpExecJs->supportsCache()) {
$this->phpExecJs->setCache($this->cache);
try {
$this->ensurePhpExecJsIsBuilt();
if ($this->needToSetContext) {
if ($this->phpExecJs->supportsCache()) {
$this->phpExecJs->setCache($this->cache);
}
$this->phpExecJs->createContext(
$this->consolePolyfill()."\n".$this->timerPolyfills($trace)."\n".$this->loadServerBundle(),
$this->cacheKey
);
$this->needToSetContext = false;
}
$result = \json_decode(
$this->phpExecJs->evalJs($this->wrap($componentName, $propsString, $uuid, $registeredStores, $trace)),
true
);
} catch (\Throwable $t) {
if ($this->failLoud) {
throw $t;
}

if ($this->logger) {
$this->logger->log(LogLevel::ERROR, $t->getMessage(), ['exception' => $t]);
}
$this->phpExecJs->createContext($this->consolePolyfill()."\n".$this->timerPolyfills($trace)."\n".$this->loadServerBundle(), $this->cacheKey);
$this->needToSetContext = false;

return [
'evaluated' => '',
'consoleReplay' => '',
'hasErrors' => true,
];
}
$result = json_decode($this->phpExecJs->evalJs($this->wrap($componentName, $propsString, $uuid, $registeredStores, $trace)), true);

if ($result['hasErrors']) {
$this->logErrors($result['consoleReplayScript']);
if ($this->failLoud) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Limenius\ReactRenderer\Tests\Renderer;

use Limenius\ReactRenderer\Renderer\PhpExecJsReactRenderer;
use Limenius\ReactRenderer\Context\ContextProviderInterface;
use Psr\Log\LoggerInterface;
use Limenius\ReactRenderer\Exception\EvalJsException;
use Limenius\ReactRenderer\Renderer\PhpExecJsReactRenderer;
use Nacmartin\PhpExecJs\PhpExecJs;
use PHPUnit\Framework\Exception;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

/**
* Class PhpExecJsReactRendererTest
Expand All @@ -31,7 +34,7 @@ class PhpExecJsReactRendererTest extends TestCase
/**
* {@inheritdoc}
*/
public function setUp()
public function setUp(): void
{
$this->logger = $this->getMockBuilder(LoggerInterface::class)
->getMock();
Expand All @@ -46,11 +49,9 @@ public function setUp()
$this->renderer->setPhpExecJs($this->phpExecJs);
}

/**
* @expectedException \RuntimeException
*/
public function testServerBundleNotFound()
{
$this->expectException(\RuntimeException::class);
$this->renderer = new PhpExecJsReactRenderer(__DIR__.'/Fixtures/i-dont-exist.js', $this->logger, $this->contextProvider);
$this->renderer->render('MyApp', 'props', 1, null, false);
}
Expand Down Expand Up @@ -108,9 +109,6 @@ public function testReactOnRails()
$this->renderer->render('MyApp', '{msg:"It Works!"}', 1, null, true));
}

/**
* @expectedException \Limenius\ReactRenderer\Exception\EvalJsException
*/
public function testFailLoud()
{
$phpExecJs = $this->getMockBuilder(PhpExecJs::class)
Expand All @@ -119,6 +117,54 @@ public function testFailLoud()
->willReturn('{ "html" : "go for it", "hasErrors" : true, "consoleReplayScript": " - my replay"}');
$this->renderer = new PhpExecJsReactRenderer(__DIR__.'/Fixtures/server-bundle.js', true, $this->contextProvider, $this->logger);
$this->renderer->setPhpExecJs($phpExecJs);
$this->expectException(EvalJsException::class);
$this->renderer->render('MyApp', 'props', 1, null, true);
}

/**
* @testdox failLoud true bubbles thrown exceptions
*/
public function testFailLoudBubblesThrownException()
{
$err = new Exception('test exception');
$this->phpExecJs->method('createContext')->willThrowException($err);
$this->renderer = new PhpExecJsReactRenderer(__DIR__.'/Fixtures/server-bundle.js', true, $this->contextProvider, $this->logger);
$this->renderer->setPhpExecJs($this->phpExecJs);

$this->expectExceptionObject($err);
$this->renderer->render('MyApp', 'props', 1, null, true);
}

/**
* @testdox failLoud false returns empty error result on exception
*/
public function testFailQuietReturnsEmptyErrorResultOnException()
{
$this->phpExecJs->method('createContext')->willThrowException(new \Exception('test exception'));

$this->assertEquals(
[
'evaluated' => '',
'consoleReplay' => '',
'hasErrors' => true,
],
$this->renderer->render('MyApp', 'props', 1, null, true)
);
}

/**
* @testdox failLoud false logs thrown exceptions
*/
public function testFailQuietLogsThrownExceptions()
{
$err = new Exception('test exception');
$this->phpExecJs->method('createContext')->willThrowException($err);

$this->logger
->expects($this->exactly(1))
->method('log')
->with(LogLevel::ERROR, 'test exception', ['exception' => $err]);

$this->renderer->render('MyApp', 'props', 1, null, true);
}
}