Skip to content

Commit

Permalink
feat(http-calls): integration of blackfire command line generation (#48)
Browse files Browse the repository at this point in the history
* feat(http-calls): integration of blackfire command line generation
[WEBTECH-56]

* fix(phpstan): temporarily ignore some difficult issues
[WEBTECH-56]

* Update phpstan.neon

* ci: use local http-bin

* ci: fix Abstract test

* ci: fix AbstractTests

* ci: allow warning

* ci: remove useless class

* ci: add coverage config

* ci: fix phpunit to 9.*

---------

Co-authored-by: vchoquer <[email protected]>
Co-authored-by: LAHAXE Arnaud <[email protected]>
  • Loading branch information
3 people authored Jun 6, 2023
1 parent 34145b1 commit 20f5238
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 452 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ on: [ "pull_request" ]

jobs:
tests:
services:
httpbin:
image: kennethreitz/httpbin
ports:
- 8080:80
strategy:
matrix:
php-versions: [ '8.0', '8.1' ]
Expand Down Expand Up @@ -36,4 +41,4 @@ jobs:
level: 0
php_version: ${{ matrix.php-versions }}
- name: "Run phpunit"
run: "XDEBUG_MODE=coverage php ./vendor/bin/phpunit --coverage-text"
run: "HTTP_BIN_URL=http://localhost:8080 XDEBUG_MODE=coverage php ./vendor/bin/phpunit --coverage-text"
51 changes: 51 additions & 0 deletions DataCollector/ProfilerDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ class ProfilerDataCollector extends DataCollector implements EventSubscriberInte
*/
protected $stopwatch;

/**
* blackfire instance id
* @var string
*/
protected string $blackfireClientId;

/**
* Access key to blackfire instance
* @var string
*/
protected string $blackfireClientToken;

/**
* sample amount used by blackfire commande
* number of times that the curl will be executed
* @var int
*/
protected int $blackfireSamples;

/**
* @param bool $debug
*/
Expand Down Expand Up @@ -131,6 +150,7 @@ public function normalizeCalls()
'sfDebugLink' => false,
'trace' => array_slice($v['trace'], 3),
'curlCommand' => $this->buildCurlCommand($v['request']),
'blackfireCommand' => $this->buildBlackfireCommand($v['request']),
);

if (isset($v['response'])) {
Expand Down Expand Up @@ -283,6 +303,25 @@ public function buildCurlCommand(Request $request)
return str_replace("\n", " \\\n", $command);
}

/**
* @param Request $request
* @return string
*/
public function buildBlackfireCommand(Request $request)
{
$command = "blackfire \\\n";
if (!empty($this->blackfireClientId)) {
$command .= "--client-id=\"$this->blackfireClientId\" \\\n";
}
if (!empty($this->blackfireClientToken)) {
$command .= "--client-token=\"$this->blackfireClientToken\" \\\n";
}
$command .= "--samples $this->blackfireSamples \\\n";

$curl = $this->buildCurlCommand($request);
return $command . $curl;
}

public function fetchTransferInfos(array $call)
{
$call['stop'] = isset($call['stop']) ? $call['stop'] : 0;
Expand Down Expand Up @@ -641,4 +680,16 @@ public function reset()
$this->errors = [];
$this->stopwatch->reset();
}

/**
* Set blackfire properties
* @param $config array of items that can be used to set blackfire config
* @return void
*/
public function setBlackfireConfig(array $config): void
{
$this->blackfireClientId = $config["client_id"] ?? "";
$this->blackfireClientToken = $config["client_token"] ?? "";
$this->blackfireSamples = $config["samples"] ?? 10;
}
}
34 changes: 34 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace evaisse\SimpleHttpBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{

public function getConfigTreeBuilder() : TreeBuilder
{
$treeBuilder = new TreeBuilder('simple_http');
$rootNode = $treeBuilder->getRootNode();
$rootNode
->children()
->arrayNode('blackfire')
->children()
->scalarNode('client_id')
->defaultValue(null)
->end()
->scalarNode('client_token')
->defaultValue(null)
->end()
->scalarNode('samples')
->defaultValue(10)
->end()
->end()
->end()
->end();

return $treeBuilder;
}
}
8 changes: 8 additions & 0 deletions DependencyInjection/SimpleHttpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ class SimpleHttpExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

if (!empty($config['blackfire'])) {
$container->getDefinition('simple_http.profiler.data_collector')
->addMethodCall('setBlackfireConfig', [$config['blackfire']]);
}
}
}
47 changes: 33 additions & 14 deletions Resources/views/Collector/partials/call.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

<div id="call-{{ index }}" class="http-call http-call--{{ call.request.method | lower }} {{ call.request.schemeAndHttpHost | simple_http_md5 }}">

<h3 class="http-call__title">
{#<span style="float: right"><a href="#" class="replay" data-request="{{ call.request|json_encode }}" data-index="{{ index }}">Replay</a></span>#}
<span class="http-call__title__method">{{ call.request.method }}</span>
Expand All @@ -23,22 +23,22 @@
<a href="javascript://">Request</a>
</li>
{% if call.response and call.response.headers is defined %}
<li>
<a href="javascript://">Response {{ call.response | simple_http_format_http_code }}</a>
</li>
<li>
<a href="javascript://">Response {{ call.response | simple_http_format_http_code }}</a>
</li>
{% endif %}
{% if call.error %}
<li>
<a href="javascript://">Error</a>
</li>
<li>
<a href="javascript://">Error</a>
</li>
{% endif %}
<li>
<a href="javascript://">Cookies</a>
</li>
{% if call.auth %}
<li>
<a href="javascript://">Auth <span class="simple-http-badge">{{ call.auth.type }}</span></a>
</li>
<li>
<a href="javascript://">Auth <span class="simple-http-badge">{{ call.auth.type }}</span></a>
</li>
{% endif %}

{% if dataDecoded %}
Expand All @@ -55,6 +55,11 @@
<a href="javascript://">Curl</a>
</li>
{% endif %}
{% if call.blackfireCommand is defined %}
<li>
<a href="javascript://">Blackfire</a>
</li>
{% endif %}
</ul>

<div class="tabs-content">
Expand Down Expand Up @@ -90,9 +95,9 @@
</div>
{% endif %}
{% if dataDecoded %}
<div class="tab-content-data">
{% include '@SimpleHttp/Collector/partials/data.html.twig' with {'dataDecoded': dataDecoded} only %}
</div>
<div class="tab-content-data">
{% include '@SimpleHttp/Collector/partials/data.html.twig' with {'dataDecoded': dataDecoded} only %}
</div>
{% endif %}
<div class="tab-content--raw">
{{ dump(call) }}
Expand All @@ -109,7 +114,21 @@
</tr>
</tbody>
</table>

</div>
</div>
{% endif %}
{% if call.blackfireCommand is defined %}
<div class="tab-content--curl">
<div class="active" style="display:block;">
<table>
<tbody>
<tr>
<td>
<pre>{{ call.blackfireCommand | raw }}</pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
{% endif %}
Expand Down
16 changes: 7 additions & 9 deletions Tests/Unit/AbstractTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@

namespace evaisse\SimpleHttpBundle\Tests\Unit;



use evaisse\SimpleHttpBundle\Http\Kernel;
use evaisse\SimpleHttpBundle\Http\Request;
use evaisse\SimpleHttpBundle\Http\Statement;
use evaisse\SimpleHttpBundle\Service\Helper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\EventDispatcher\EventDispatcher;


class AbstractTests extends TestCase
{
public static $baseUrl = null;

public function __construct(?string $name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
self::$baseUrl = self::$baseUrl ?? getenv('HTTP_BIN_URL');
}

// public static $baseUrl = "http://127.0.0.1:8989";
public static $baseUrl = "http://httpbin.org";

protected function createContext()
{
Expand Down
14 changes: 0 additions & 14 deletions Tests/Unit/ReturnCodeTest.php

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"require-dev": {
"roave/security-advisories": "dev-latest",
"phpunit/phpunit": ">=9.5.11"
"phpunit/phpunit": "^9.5"
},
"suggest": {
},
Expand Down
Loading

0 comments on commit 20f5238

Please sign in to comment.