Skip to content

[bundle][profiler] Fix array to string conversion notice. #212

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

Merged
merged 2 commits into from
Oct 3, 2017
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
13 changes: 4 additions & 9 deletions pkg/enqueue-bundle/Profiler/MessageQueueCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Enqueue\Client\MessagePriority;
use Enqueue\Client\ProducerInterface;
use Enqueue\Client\TraceableProducer;
use Enqueue\Util\JSON;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
Expand Down Expand Up @@ -65,19 +66,13 @@ public function prettyPrintPriority($priority)
}

/**
* @param string $message
* @param mixed $body
*
* @return string
*/
public function prettyPrintMessage($message)
public function ensureString($body)
{
if (is_scalar($message)) {
return htmlspecialchars($message);
}

return htmlspecialchars(
json_encode($message, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
);
return is_string($body) ? $body : JSON::encode($body);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion pkg/enqueue-bundle/Resources/views/Profiler/panel.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
<td>{{ sentMessage.topic }}</td>
<td style="width: 70%">
<span class="metadata">
<span>{{ sentMessage.body[0:40] }}... </span><a class="btn btn-link text-small sf-toggle" data-toggle-selector="#message-body-{{ loop.index }}" data-toggle-alt-content="Hide trace">Show trace</a>
<span>
{{ collector.ensureString(sentMessage.body)[0:40] }}{% if collector.ensureString(sentMessage.body)[0:40] != collector.ensureString(sentMessage.body) %}...{% endif %}
</span>
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#message-body-{{ loop.index }}" data-toggle-alt-content="Hide trace">Show trace</a>

<div id="message-body-{{ loop.index }}" class="context sf-toggle-content sf-toggle-hidden">
{{ profiler_dump(sentMessage.body) }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,19 @@ public function testShouldPrettyPrintUnknownPriority()
$this->assertEquals('unknownPriority', $collector->prettyPrintPriority('unknownPriority'));
}

public function testShouldPrettyPrintScalarMessage()
public function testShouldEnsureStringKeepStringSame()
{
$collector = new MessageQueueCollector($this->createProducerMock());

$this->assertEquals('foo', $collector->prettyPrintMessage('foo'));
$this->assertEquals('&lt;p&gt;', $collector->prettyPrintMessage('<p>'));
$this->assertEquals('foo', $collector->ensureString('foo'));
$this->assertEquals('bar baz', $collector->ensureString('bar baz'));
}

public function testShouldPrettyPrintArrayMessage()
public function testShouldEnsureStringEncodeArrayToJson()
{
$collector = new MessageQueueCollector($this->createProducerMock());

$expected = "[\n &quot;foo&quot;,\n &quot;bar&quot;\n]";

$this->assertEquals($expected, $collector->prettyPrintMessage(['foo', 'bar']));
$this->assertEquals('["foo","bar"]', $collector->ensureString(['foo', 'bar']));
}

/**
Expand Down