From 5e5279aa1659502b84bde11fde6334c383f6175e Mon Sep 17 00:00:00 2001 From: Shueh Chou Lu Date: Fri, 9 Jul 2021 10:54:27 +0800 Subject: [PATCH] Fix XRay HttpSegment wrong paramters (#7) * Fix XRay HttpSegment wrong paramters * test: Xray --- src/Collectors/SegmentCollector.php | 22 -------------- src/Xray.php | 24 ++++++++++++++- tests/XRayTest.php | 45 +++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 23 deletions(-) create mode 100644 tests/XRayTest.php diff --git a/src/Collectors/SegmentCollector.php b/src/Collectors/SegmentCollector.php index cf50e32..608a54f 100644 --- a/src/Collectors/SegmentCollector.php +++ b/src/Collectors/SegmentCollector.php @@ -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; @@ -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)) { diff --git a/src/Xray.php b/src/Xray.php index 60dfd42..4c48070 100644 --- a/src/Xray.php +++ b/src/Xray.php @@ -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 @@ -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); diff --git a/tests/XRayTest.php b/tests/XRayTest.php new file mode 100644 index 0000000..523173e --- /dev/null +++ b/tests/XRayTest.php @@ -0,0 +1,45 @@ +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(); + } +}