1818namespace OpenCensus \Tests \Integration \Trace ;
1919
2020use GuzzleHttp \Client ;
21+ use HttpTest \HttpTestServer ;
2122use OpenCensus \Trace \Tracer ;
2223use OpenCensus \Trace \Exporter \ExporterInterface ;
2324use OpenCensus \Trace \Integrations \Guzzle \EventSubscriber ;
25+ use Psr \Http \Message \RequestInterface ;
26+ use Psr \Http \Message \ResponseInterface ;
2427use PHPUnit \Framework \TestCase ;
2528
2629/**
2730 * @group trace
2831 */
2932class Guzzle5Test extends TestCase
3033{
31- public function testGuzzleRequest ()
34+ private $ client ;
35+
36+ public function setUp ()
3237 {
33- $ client = new Client ();
38+ parent ::setUp ();
39+ $ this ->client = new Client ();
3440 $ subscriber = new EventSubscriber ();
35- $ client ->getEmitter ()->attach ($ subscriber );
41+ $ this ->client ->getEmitter ()->attach ($ subscriber );
42+ if (extension_loaded ('opencensus ' )) {
43+ opencensus_trace_clear ();
44+ }
45+ }
46+
47+ public function testGuzzleRequest ()
48+ {
49+ $ server = HttpTestServer::create (
50+ function (RequestInterface $ request , ResponseInterface &$ response ) {
51+ /* Assert the HTTP call includes the expected values */
52+ $ this ->assertEquals ('GET ' , $ request ->getMethod ());
53+ $ response = $ response ->withStatus (200 );
54+ }
55+ );
3656
37- $ url = 'http://www.google.com/ ' ;
3857 $ exporter = $ this ->prophesize (ExporterInterface::class);
3958 $ tracer = Tracer::start ($ exporter ->reveal (), [
4059 'skipReporting ' => true
4160 ]);
42- $ response = $ client ->get ($ url );
61+
62+ $ server ->start ();
63+
64+ $ response = $ this ->client ->get ($ server ->getUrl ());
4365 $ this ->assertEquals (200 , $ response ->getStatusCode ());
4466
67+ $ server ->stop ();
68+
69+ $ tracer ->onExit ();
70+
71+ $ spans = $ tracer ->tracer ()->spans ();
72+ $ this ->assertCount (2 , $ spans );
73+
74+ $ curlSpan = $ spans [1 ];
75+ $ this ->assertEquals ('GuzzleHttp::request ' , $ curlSpan ->name ());
76+ $ this ->assertEquals ('GET ' , $ curlSpan ->attributes ()['method ' ]);
77+ $ this ->assertEquals ($ server ->getUrl (), $ curlSpan ->attributes ()['uri ' ]);
78+ }
79+
80+ public function testPersistsTraceContext ()
81+ {
82+ $ server = HttpTestServer::create (
83+ function (RequestInterface $ request , ResponseInterface &$ response ) {
84+ /* Assert the HTTP call includes the expected values */
85+ $ this ->assertEquals ('GET ' , $ request ->getMethod ());
86+ $ contextHeader = $ request ->getHeaderLine ('X-Cloud-Trace-Context ' );
87+ $ this ->assertNotEmpty ($ contextHeader );
88+ $ this ->assertStringStartsWith ('1603c1cde5c74f23bcf1682eb822fcf7 ' , $ contextHeader );
89+ $ response = $ response ->withStatus (200 );
90+ }
91+ );
92+
93+ $ traceContextHeader = '1603c1cde5c74f23bcf1682eb822fcf7/1150672535;o=1 ' ;
94+ $ exporter = $ this ->prophesize (ExporterInterface::class);
95+ $ tracer = Tracer::start ($ exporter ->reveal (), [
96+ 'skipReporting ' => true ,
97+ 'headers ' => [
98+ 'HTTP_X_CLOUD_TRACE_CONTEXT ' => $ traceContextHeader
99+ ]
100+ ]);
101+
102+ $ server ->start ();
103+
104+ $ response = $ this ->client ->get ($ server ->getUrl ());
105+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
106+
107+ $ server ->stop ();
108+
45109 $ tracer ->onExit ();
46110
47111 $ spans = $ tracer ->tracer ()->spans ();
@@ -50,6 +114,6 @@ public function testGuzzleRequest()
50114 $ curlSpan = $ spans [1 ];
51115 $ this ->assertEquals ('GuzzleHttp::request ' , $ curlSpan ->name ());
52116 $ this ->assertEquals ('GET ' , $ curlSpan ->attributes ()['method ' ]);
53- $ this ->assertEquals ($ url , $ curlSpan ->attributes ()['uri ' ]);
117+ $ this ->assertEquals ($ server -> getUrl () , $ curlSpan ->attributes ()['uri ' ]);
54118 }
55119}
0 commit comments