1717
1818namespace Google \Cloud \Logging \Tests \Unit ;
1919
20+ use Google \Api \MonitoredResource ;
2021use Google \Cloud \Core \Batch \OpisClosureSerializerV4 ;
2122use Google \Cloud \Core \Report \EmptyMetadataProvider ;
2223use Google \Cloud \Logging \Logger ;
2324use Google \Cloud \Logging \PsrLogger ;
2425use Google \Cloud \Logging \Connection \Gapic ;
26+ use Google \Cloud \Logging \V2 \Client \LoggingServiceV2Client ;
27+ use Google \Cloud \Logging \V2 \LogEntry ;
28+ use Google \Cloud \Logging \V2 \WriteLogEntriesRequest ;
29+ use Google \Cloud \Logging \V2 \WriteLogEntriesResponse ;
30+ use Google \Protobuf \Internal \GPBType ;
31+ use Google \Protobuf \Internal \MapField ;
32+ use Google \Protobuf \Struct ;
33+ use Google \Protobuf \Timestamp ;
34+ use Google \Protobuf \Value ;
2535use PHPUnit \Framework \TestCase ;
2636use Psr \Log \InvalidArgumentException ;
2737use Prophecy \PhpUnit \ProphecyTrait ;
@@ -49,7 +59,7 @@ public function setUp(): void
4959
5060 public function getPsrLogger ($ connection , ?array $ resource = null , ?array $ labels = null , $ messageKey = 'message ' )
5161 {
52- $ logger = new Logger ($ connection-> reveal () , $ this ->logName , $ this ->projectId , $ resource , $ labels );
62+ $ logger = new Logger ($ connection , $ this ->logName , $ this ->projectId , $ resource , $ labels );
5363 return new PsrLogger ($ logger , $ messageKey , ['metadataProvider ' => new EmptyMetadataProvider ()]);
5464 }
5565
@@ -71,7 +81,7 @@ public function testWritesEntryWithDefinedLevels($level)
7181 ])
7282 ->willReturn ([])
7383 ->shouldBeCalledTimes (1 );
74- $ psrLogger = $ this ->getPsrLogger ($ this ->connection );
84+ $ psrLogger = $ this ->getPsrLogger ($ this ->connection -> reveal () );
7585
7686 $ this ->assertNull (
7787 $ psrLogger ->$ level ($ this ->textPayload , [
@@ -80,6 +90,40 @@ public function testWritesEntryWithDefinedLevels($level)
8090 );
8191 }
8292
93+ /**
94+ * @dataProvider levelProvider
95+ */
96+ public function testWritesEntryRequestWithDefinedLevels ($ level )
97+ {
98+ $ map = new MapField (GPBType::STRING , GPBType::MESSAGE , Value::class);
99+ $ map ['message ' ] = new Value (['string_value ' => $ this ->textPayload ]);
100+ $ entry = new LogEntry ([
101+ 'severity ' => array_flip (Logger::getLogLevelMap ())[$ level ],
102+ 'log_name ' => $ this ->formattedName ,
103+ 'resource ' => new MonitoredResource ($ this ->resource ),
104+ 'timestamp ' => new Timestamp (['seconds ' => 100 ]),
105+ 'json_payload ' => new Struct (['fields ' => $ map ])
106+ ]);
107+ $ request = new WriteLogEntriesRequest (['entries ' => [$ entry ]]);
108+
109+ $ loggingClient = $ this ->prophesize (LoggingServiceV2Client::class);
110+ $ loggingClient ->writeLogEntries ($ request , [])
111+ ->shouldBeCalledOnce ()
112+ ->willReturn (new WriteLogEntriesResponse ());
113+
114+ $ connection = new Gapic ([
115+ 'loggingGapicClient ' => $ loggingClient ->reveal ()
116+ ]);
117+
118+ $ psrLogger = $ this ->getPsrLogger ($ connection );
119+
120+ $ this ->assertNull (
121+ $ psrLogger ->$ level ($ this ->textPayload , [
122+ 'stackdriverOptions ' => ['timestamp ' => date ('Y-m-d H:i:s ' , 100 )]
123+ ])
124+ );
125+ }
126+
83127 public function levelProvider ()
84128 {
85129 return [
@@ -99,7 +143,7 @@ public function testWritesEntry()
99143 $ this ->connection ->writeLogEntries ([
100144 'entries ' => [
101145 [
102- 'severity ' => $ this ->severity ,
146+ 'severity ' => array_flip (Logger:: getLogLevelMap ())[ $ this ->severity ] ,
103147 'jsonPayload ' => ['message ' => $ this ->textPayload ],
104148 'logName ' => $ this ->formattedName ,
105149 'resource ' => $ this ->resource ,
@@ -109,7 +153,7 @@ public function testWritesEntry()
109153 ])
110154 ->willReturn ([])
111155 ->shouldBeCalledTimes (1 );
112- $ psrLogger = $ this ->getPsrLogger ($ this ->connection );
156+ $ psrLogger = $ this ->getPsrLogger ($ this ->connection -> reveal () );
113157
114158 $ this ->assertNull (
115159 $ psrLogger ->log ($ this ->severity , $ this ->textPayload , [
@@ -125,7 +169,7 @@ public function testPsrLoggerUsesDefaults()
125169 $ this ->connection ->writeLogEntries ([
126170 'entries ' => [
127171 [
128- 'severity ' => $ this ->severity ,
172+ 'severity ' => array_flip (Logger:: getLogLevelMap ())[ $ this ->severity ] ,
129173 'jsonPayload ' => ['message ' => $ this ->textPayload ],
130174 'logName ' => $ this ->formattedName ,
131175 'resource ' => $ resource ,
@@ -136,7 +180,7 @@ public function testPsrLoggerUsesDefaults()
136180 ])
137181 ->willReturn ([])
138182 ->shouldBeCalledTimes (1 );
139- $ psrLogger = $ this ->getPsrLogger ($ this ->connection , $ resource , $ labels );
183+ $ psrLogger = $ this ->getPsrLogger ($ this ->connection -> reveal () , $ resource , $ labels );
140184
141185 $ this ->assertNull (
142186 $ psrLogger ->log ($ this ->severity , $ this ->textPayload , [
@@ -153,7 +197,7 @@ public function testOverridePsrLoggerDefaults()
153197 $ this ->connection ->writeLogEntries ([
154198 'entries ' => [
155199 [
156- 'severity ' => $ this ->severity ,
200+ 'severity ' => array_flip (Logger:: getLogLevelMap ())[ $ this ->severity ] ,
157201 'jsonPayload ' => ['message ' => $ this ->textPayload ],
158202 'logName ' => $ this ->formattedName ,
159203 'resource ' => $ newResource ,
@@ -164,7 +208,7 @@ public function testOverridePsrLoggerDefaults()
164208 ])
165209 ->willReturn ([])
166210 ->shouldBeCalledTimes (1 );
167- $ psrLogger = $ this ->getPsrLogger ($ this ->connection , null , $ defaultLabels );
211+ $ psrLogger = $ this ->getPsrLogger ($ this ->connection -> reveal () , null , $ defaultLabels );
168212
169213 $ this ->assertNull (
170214 $ psrLogger ->log ($ this ->severity , $ this ->textPayload , [
@@ -181,7 +225,7 @@ public function testLogThrowsExceptionWithInvalidLevel()
181225 {
182226 $ this ->expectException (InvalidArgumentException::class);
183227
184- $ psrLogger = $ this ->getPsrLogger ($ this ->connection );
228+ $ psrLogger = $ this ->getPsrLogger ($ this ->connection -> reveal () );
185229 $ psrLogger ->log ('INVALID-LEVEL ' , $ this ->textPayload );
186230 }
187231
@@ -205,7 +249,7 @@ public function testUsesCustomMessageKey()
205249 $ this ->connection ->writeLogEntries ([
206250 'entries ' => [
207251 [
208- 'severity ' => $ this ->severity ,
252+ 'severity ' => array_flip (Logger:: getLogLevelMap ())[ $ this ->severity ] ,
209253 'jsonPayload ' => [$ customKey => $ this ->textPayload ],
210254 'logName ' => $ this ->formattedName ,
211255 'resource ' => $ this ->resource ,
@@ -215,7 +259,7 @@ public function testUsesCustomMessageKey()
215259 ])
216260 ->willReturn ([])
217261 ->shouldBeCalledTimes (1 );
218- $ psrLogger = $ this ->getPsrLogger ($ this ->connection , null , null , $ customKey );
262+ $ psrLogger = $ this ->getPsrLogger ($ this ->connection -> reveal () , null , null , $ customKey );
219263 $ psrLogger ->log ($ this ->severity , $ this ->textPayload , [
220264 'stackdriverOptions ' => ['timestamp ' => null ]
221265 ]);
@@ -278,7 +322,7 @@ private function expectLogWithExceptionInContext($throwable)
278322 $ this ->connection ->writeLogEntries ([
279323 'entries ' => [
280324 [
281- 'severity ' => $ this ->severity ,
325+ 'severity ' => array_flip (Logger:: getLogLevelMap ())[ $ this ->severity ] ,
282326 'jsonPayload ' => [
283327 'message ' => $ this ->textPayload ,
284328 'exception ' => (string ) $ throwable
@@ -291,7 +335,7 @@ private function expectLogWithExceptionInContext($throwable)
291335 ])
292336 ->willReturn ([])
293337 ->shouldBeCalledTimes (1 );
294- $ psrLogger = $ this ->getPsrLogger ($ this ->connection );
338+ $ psrLogger = $ this ->getPsrLogger ($ this ->connection -> reveal () );
295339 $ psrLogger ->log ($ this ->severity , $ this ->textPayload , [
296340 'exception ' => $ throwable ,
297341 'stackdriverOptions ' => ['timestamp ' => null ]
0 commit comments