Skip to content

Commit

Permalink
Fix XRay HttpSegment wrong paramters (#7)
Browse files Browse the repository at this point in the history
* Fix XRay HttpSegment wrong paramters

* test: Xray
  • Loading branch information
evan361425 authored Jul 9, 2021
1 parent 96d5634 commit 5e5279a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 23 deletions.
22 changes: 0 additions & 22 deletions src/Collectors/SegmentCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,6 @@ public function addCustomSegment(Segment $segment, string $name): Segment
return $segment;
}

/**
* Add HTTP segment
*
* If name is not set, it will use url
*
* $config default values:
* [
* "method": "GET",
* "name": null,
* ]
*
* @param string $name
* @param array|null $config
* @return Segment
*/
public function addHttpSegment(string $url, ?array $config = []): Segment
{
$name = $config['name'] ?? $url;
Expand Down Expand Up @@ -152,13 +137,6 @@ public function endSegment(string $name): void
}
}

/**
* End HTTP segment by segment name
*
* @param string $name
* @param integer|null $responseCode = 200
* @return void
*/
public function endHttpSegment(string $name, ?int $responseCode = 200): void
{
if ($this->hasAddedSegment($name)) {
Expand Down
24 changes: 23 additions & 1 deletion src/Xray.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,24 @@ public function addCustomSegment(Segment $segment, string $name): Segment
return $this->collector->addCustomSegment($segment, $name);
}

/**
* Add HTTP segment
*
* If name is not set, it will use url
*
* $config default values:
* [
* "method": "GET",
* "name": null,
* ]
*
* @param string $name
* @param array|null $config
* @return Segment
*/
public function addHttpSegment(string $url, ?array $config = []): Segment
{
return $this->collector->addHttpSegment($name, $url, $method);
return $this->collector->addHttpSegment($url, $config);
}

public function getSegment(string $name): ?Segment
Expand All @@ -59,6 +74,13 @@ public function endSegment(string $name): void
$this->collector->endSegment($name);
}

/**
* End HTTP segment by segment name
*
* @param string $name
* @param integer|null $responseCode = 200
* @return void
*/
public function endHttpSegment(string $name, ?int $responseCode = 200): void
{
$this->collector->endHttpSegment($name, $responseCode);
Expand Down
45 changes: 45 additions & 0 deletions tests/XRayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Napp\Xray\Tests;

use Napp\Xray\Collectors\SegmentCollector;
use Napp\Xray\Segments\TimeSegment;
use Napp\Xray\Xray;
use PHPUnit\Framework\TestCase;

class XrayTest extends TestCase
{
public function test_setting_end_time()
{
$collector = $this->createMock(SegmentCollector::class);
$segment = new TimeSegment();

$xray = new Xray($collector);

$collector->expects($this->once())->method('tracer');
$collector->expects($this->once())->method('current');
$collector->expects($this->once())->method('isTracerEnabled');
$collector->expects($this->once())->method('addSegment')->with('name', null, null);
$collector->expects($this->once())->method('addCustomSegment')->with($segment, 'name');
$collector->expects($this->once())->method('addHttpSegment')->with('url', []);
$collector->expects($this->once())->method('getSegment')->with('name');
$collector->expects($this->once())->method('endSegment')->with('name');
$collector->expects($this->once())->method('endHttpSegment')->with('name');
$collector->expects($this->once())->method('hasAddedSegment')->with('name');
$collector->expects($this->once())->method('endCurrentSegment');

$xray->tracer();
$xray->current();
$xray->isEnabled();
$xray->addSegment('name');
$xray->addCustomSegment($segment, 'name');
$xray->addHttpSegment('url');
$xray->getSegment('name');
$xray->endSegment('name');
$xray->endHttpSegment('name');
$xray->hasAddedSegment('name');
$xray->endCurrentSegment();
}
}

0 comments on commit 5e5279a

Please sign in to comment.