Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c506d2

Browse files
author
Lucas Michot
authored
chore(tests): better use of assertions (GoogleCloudPlatform#85)
1 parent 796d86c commit 3c506d2

10 files changed

+130
-130
lines changed

tests/CloudEventFunctionsWrapperTest.php

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,25 @@ public function testInvalidCloudEventRequestBody()
4141
$request = new ServerRequest('POST', '/', $headers, 'notjson');
4242
$cloudEventFunctionWrapper = new CloudEventFunctionWrapper([$this, 'invokeThis']);
4343
$response = $cloudEventFunctionWrapper->execute($request);
44-
$this->assertEquals(400, $response->getStatusCode());
45-
$this->assertEquals(
44+
$this->assertSame(400, $response->getStatusCode());
45+
$this->assertSame(
4646
'Could not parse CloudEvent: Syntax error',
4747
(string) $response->getBody()
4848
);
49-
$this->assertEquals('crash', $response->getHeaderLine('X-Google-Status'));
49+
$this->assertSame('crash', $response->getHeaderLine('X-Google-Status'));
5050
}
5151

5252
public function testInvalidLegacyEventRequestBody()
5353
{
5454
$request = new ServerRequest('POST', '/', [], 'notjson');
5555
$cloudEventFunctionWrapper = new CloudEventFunctionWrapper([$this, 'invokeThis']);
5656
$response = $cloudEventFunctionWrapper->execute($request);
57-
$this->assertEquals(400, $response->getStatusCode());
58-
$this->assertEquals(
57+
$this->assertSame(400, $response->getStatusCode());
58+
$this->assertSame(
5959
'Could not parse CloudEvent: Syntax error',
6060
(string) $response->getBody()
6161
);
62-
$this->assertEquals('crash', $response->getHeaderLine('X-Google-Status'));
62+
$this->assertSame('crash', $response->getHeaderLine('X-Google-Status'));
6363
}
6464

6565
public function testNonJsonIsValidInBinaryCloudEventRequestBody()
@@ -74,7 +74,7 @@ public function testNonJsonIsValidInBinaryCloudEventRequestBody()
7474
[$this, 'invokeThisPartial']
7575
);
7676
$response = $cloudEventFunctionWrapper->execute($request);
77-
$this->assertEquals(200, $response->getStatusCode());
77+
$this->assertSame(200, $response->getStatusCode());
7878
}
7979

8080
public function testInvalidJsonBinaryCloudEventRequestBody()
@@ -90,12 +90,12 @@ public function testInvalidJsonBinaryCloudEventRequestBody()
9090
[$this, 'invokeThisPartial']
9191
);
9292
$response = $cloudEventFunctionWrapper->execute($request);
93-
$this->assertEquals(400, $response->getStatusCode());
94-
$this->assertEquals(
93+
$this->assertSame(400, $response->getStatusCode());
94+
$this->assertSame(
9595
'Could not parse CloudEvent: Syntax error',
9696
(string) $response->getBody()
9797
);
98-
$this->assertEquals('crash', $response->getHeaderLine('X-Google-Status'));
98+
$this->assertSame('crash', $response->getHeaderLine('X-Google-Status'));
9999
}
100100

101101
public function testValidJsonBinaryCloudEventRequestBody()
@@ -111,7 +111,7 @@ public function testValidJsonBinaryCloudEventRequestBody()
111111
[$this, 'invokeThisPartial']
112112
);
113113
$response = $cloudEventFunctionWrapper->execute($request);
114-
$this->assertEquals(200, $response->getStatusCode());
114+
$this->assertSame(200, $response->getStatusCode());
115115
}
116116

117117
public function testNoFunctionParameters()
@@ -232,14 +232,14 @@ public function invokeThis(CloudEvent $cloudevent)
232232
{
233233
$this->assertFalse(self::$functionCalled);
234234
self::$functionCalled = true;
235-
$this->assertEquals('1413058901901494', $cloudevent->getId());
236-
$this->assertEquals('//pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC', $cloudevent->getSource());
237-
$this->assertEquals('1.0', $cloudevent->getSpecVersion());
238-
$this->assertEquals('com.google.cloud.pubsub.topic.publish', $cloudevent->getType());
239-
$this->assertEquals('application/json', $cloudevent->getDataContentType());
240-
$this->assertEquals('type.googleapis.com/google.logging.v2.LogEntry', $cloudevent->getDataSchema());
241-
$this->assertEquals('My Subject', $cloudevent->getSubject());
242-
$this->assertEquals('2020-12-08T20:03:19.162Z', $cloudevent->getTime());
235+
$this->assertSame('1413058901901494', $cloudevent->getId());
236+
$this->assertSame('//pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC', $cloudevent->getSource());
237+
$this->assertSame('1.0', $cloudevent->getSpecVersion());
238+
$this->assertSame('com.google.cloud.pubsub.topic.publish', $cloudevent->getType());
239+
$this->assertSame('application/json', $cloudevent->getDataContentType());
240+
$this->assertSame('type.googleapis.com/google.logging.v2.LogEntry', $cloudevent->getDataSchema());
241+
$this->assertSame('My Subject', $cloudevent->getSubject());
242+
$this->assertSame('2020-12-08T20:03:19.162Z', $cloudevent->getTime());
243243
}
244244

245245
public function testWithNotFullButValidCloudEvent()
@@ -262,10 +262,10 @@ public function invokeThisPartial(CloudEvent $cloudevent)
262262
{
263263
$this->assertFalse(self::$functionCalled);
264264
self::$functionCalled = true;
265-
$this->assertEquals('fooBar', $cloudevent->getId());
266-
$this->assertEquals('my-source', $cloudevent->getSource());
267-
$this->assertEquals('1.0', $cloudevent->getSpecVersion());
268-
$this->assertEquals('my.type', $cloudevent->getType());
265+
$this->assertSame('fooBar', $cloudevent->getId());
266+
$this->assertSame('my-source', $cloudevent->getSource());
267+
$this->assertSame('1.0', $cloudevent->getSpecVersion());
268+
$this->assertSame('my.type', $cloudevent->getType());
269269
}
270270

271271
public function testFromLegacyEventWithContextProperty()
@@ -293,20 +293,20 @@ public function invokeThisLegacy(CloudEvent $cloudevent)
293293
{
294294
$this->assertFalse(self::$functionCalled);
295295
self::$functionCalled = true;
296-
$this->assertEquals('1413058901901494', $cloudevent->getId());
297-
$this->assertEquals(
296+
$this->assertSame('1413058901901494', $cloudevent->getId());
297+
$this->assertSame(
298298
'//pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC',
299299
$cloudevent->getSource()
300300
);
301-
$this->assertEquals('1.0', $cloudevent->getSpecVersion());
302-
$this->assertEquals(
301+
$this->assertSame('1.0', $cloudevent->getSpecVersion());
302+
$this->assertSame(
303303
'google.cloud.pubsub.topic.v1.messagePublished',
304304
$cloudevent->getType()
305305
);
306-
$this->assertEquals('application/json', $cloudevent->getDataContentType());
307-
$this->assertEquals(null, $cloudevent->getDataSchema());
308-
$this->assertEquals(null, $cloudevent->getSubject());
309-
$this->assertEquals('2020-12-08T20:03:19.162Z', $cloudevent->getTime());
306+
$this->assertSame('application/json', $cloudevent->getDataContentType());
307+
$this->assertNull($cloudevent->getDataSchema());
308+
$this->assertNull($cloudevent->getSubject());
309+
$this->assertSame('2020-12-08T20:03:19.162Z', $cloudevent->getTime());
310310
}
311311

312312
public function testFromStructuredEventRequest()

tests/CloudEventTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ public function testJsonSerialize()
6666
}
6767
}';
6868

69-
$this->assertEquals(json_encode($event, JSON_PRETTY_PRINT), $want);
69+
$this->assertSame(json_encode($event, JSON_PRETTY_PRINT), $want);
7070
}
7171
}

tests/ContextTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ public function testFromArray()
3333
'eventType' => 'ghi',
3434
'resource' => ['name' => 'jkl', 'service' => 'mno'],
3535
]);
36-
$this->assertEquals('abc', $context->getEventId());
37-
$this->assertEquals('def', $context->getTimestamp());
38-
$this->assertEquals('ghi', $context->getEventType());
39-
$this->assertEquals([
36+
$this->assertSame('abc', $context->getEventId());
37+
$this->assertSame('def', $context->getTimestamp());
38+
$this->assertSame('ghi', $context->getEventType());
39+
$this->assertSame([
4040
'name' => 'jkl',
4141
'service' => 'mno'
4242
], $context->getResource());
43-
$this->assertEquals('jkl', $context->getResourceName());
44-
$this->assertEquals('mno', $context->getService());
43+
$this->assertSame('jkl', $context->getResourceName());
44+
$this->assertSame('mno', $context->getService());
4545
}
4646

4747
public function testFromEmptyArray()
4848
{
4949
$context = Context::fromArray([]);
50-
$this->assertEquals(null, $context->getEventId());
51-
$this->assertEquals(null, $context->getTimestamp());
52-
$this->assertEquals(null, $context->getEventType());
53-
$this->assertEquals(null, $context->getResource());
54-
$this->assertEquals(null, $context->getResourceName());
55-
$this->assertEquals(null, $context->getService());
50+
$this->assertNull($context->getEventId());
51+
$this->assertNull($context->getTimestamp());
52+
$this->assertNull($context->getEventType());
53+
$this->assertNull($context->getResource());
54+
$this->assertNull($context->getResourceName());
55+
$this->assertNull($context->getService());
5656
}
5757
}

tests/EmitterTest.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,67 +39,67 @@ public function testEmit()
3939
$headers = xdebug_get_headers();
4040
$output = ob_get_clean();
4141

42-
$this->assertEquals('Foo', $output);
42+
$this->assertSame('Foo', $output);
4343
$this->assertContains('Foo-Header:bar', $headers);
44-
$this->assertEquals(200, http_response_code());
44+
$this->assertSame(200, http_response_code());
4545
}
4646

4747
public function testSingleHeader()
4848
{
4949
$emitter = new TestEmitter();
5050
$emitter->emit(new Response(200, ['foo-header' => 'bar']));
5151

52-
$this->assertEquals('Foo-Header:bar', $emitter->headers[1][0]);
53-
$this->assertEquals(true, $emitter->headers[1][1]);
54-
$this->assertEquals(200, $emitter->headers[1][2]);
52+
$this->assertSame('Foo-Header:bar', $emitter->headers[1][0]);
53+
$this->assertTrue($emitter->headers[1][1]);
54+
$this->assertSame(200, $emitter->headers[1][2]);
5555
}
5656

5757
public function testRepeatHeaders()
5858
{
5959
$emitter = new TestEmitter();
6060
$emitter->emit(new Response(200, ['foo-header' => ['bar', 'baz']]));
6161

62-
$this->assertEquals('Foo-Header:bar', $emitter->headers[1][0]);
63-
$this->assertEquals(true, $emitter->headers[1][1]);
64-
$this->assertEquals(200, $emitter->headers[1][2]);
62+
$this->assertSame('Foo-Header:bar', $emitter->headers[1][0]);
63+
$this->assertTrue($emitter->headers[1][1]);
64+
$this->assertSame(200, $emitter->headers[1][2]);
6565

66-
$this->assertEquals('Foo-Header:baz', $emitter->headers[2][0]);
67-
$this->assertEquals(false, $emitter->headers[2][1]);
68-
$this->assertEquals(200, $emitter->headers[2][2]);
66+
$this->assertSame('Foo-Header:baz', $emitter->headers[2][0]);
67+
$this->assertFalse($emitter->headers[2][1]);
68+
$this->assertSame(200, $emitter->headers[2][2]);
6969
}
7070

7171
public function testCookies()
7272
{
7373
$emitter = new TestEmitter();
7474
$emitter->emit(new Response(200, ['Set-Cookie' => ['1', '2']]));
7575

76-
$this->assertEquals('Set-Cookie:1', $emitter->headers[1][0]);
77-
$this->assertEquals(false, $emitter->headers[1][1]);
78-
$this->assertEquals(200, $emitter->headers[1][2]);
76+
$this->assertSame('Set-Cookie:1', $emitter->headers[1][0]);
77+
$this->assertFalse($emitter->headers[1][1]);
78+
$this->assertSame(200, $emitter->headers[1][2]);
7979

80-
$this->assertEquals('Set-Cookie:2', $emitter->headers[2][0]);
81-
$this->assertEquals(false, $emitter->headers[2][1]);
82-
$this->assertEquals(200, $emitter->headers[2][2]);
80+
$this->assertSame('Set-Cookie:2', $emitter->headers[2][0]);
81+
$this->assertFalse($emitter->headers[2][1]);
82+
$this->assertSame(200, $emitter->headers[2][2]);
8383
}
8484

8585
public function testStatusLine()
8686
{
8787
$emitter = new TestEmitter();
8888
$emitter->emit(new Response(200));
8989

90-
$this->assertEquals('HTTP/1.1 200 OK', $emitter->headers[0][0]);
91-
$this->assertEquals(true, $emitter->headers[0][1]);
92-
$this->assertEquals(200, $emitter->headers[0][2]);
90+
$this->assertSame('HTTP/1.1 200 OK', $emitter->headers[0][0]);
91+
$this->assertTrue($emitter->headers[0][1]);
92+
$this->assertSame(200, $emitter->headers[0][2]);
9393
}
9494

9595
public function testStatusLineEmptyReasonPhrase()
9696
{
9797
$emitter = new TestEmitter();
9898
$emitter->emit(new Response(419));
9999

100-
$this->assertEquals('HTTP/1.1 419', $emitter->headers[0][0]);
101-
$this->assertEquals(true, $emitter->headers[0][1]);
102-
$this->assertEquals(419, $emitter->headers[0][2]);
100+
$this->assertSame('HTTP/1.1 419', $emitter->headers[0][0]);
101+
$this->assertTrue($emitter->headers[0][1]);
102+
$this->assertSame(419, $emitter->headers[0][2]);
103103
}
104104
}
105105

tests/HttpFunctionWrapperTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,20 @@ public function testHttpHttpFunctionWrapper()
103103
$httpFunctionWrapper = new HttpFunctionWrapper([$this, 'invokeThis']);
104104
$request = new ServerRequest('GET', '/');
105105
$response = $httpFunctionWrapper->execute($request);
106-
$this->assertEquals((string) $response->getBody(), 'Invoked!');
106+
$this->assertSame('Invoked!', (string) $response->getBody());
107107
}
108108

109109
public function testHttpErrorPaths()
110110
{
111111
$httpFunctionWrapper = new HttpFunctionWrapper([$this, 'invokeThis']);
112112
$request = new ServerRequest('GET', '/robots.txt');
113113
$response = $httpFunctionWrapper->execute($request);
114-
$this->assertEquals($response->getStatusCode(), 404);
115-
$this->assertEquals('', (string) $response->getBody());
114+
$this->assertSame(404, $response->getStatusCode());
115+
$this->assertEmpty((string) $response->getBody());
116116
$request = new ServerRequest('GET', '/favicon.ico');
117117
$response = $httpFunctionWrapper->execute($request);
118-
$this->assertEquals($response->getStatusCode(), 404);
119-
$this->assertEquals('', (string) $response->getBody());
118+
$this->assertSame(404, $response->getStatusCode());
119+
$this->assertEmpty((string) $response->getBody());
120120
}
121121

122122
public function invokeThis(ServerRequestInterface $request)

tests/InvokerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testHttpInvoker()
4141
{
4242
$invoker = new Invoker([$this, 'invokeThis'], 'http');
4343
$response = $invoker->handle();
44-
$this->assertEquals((string) $response->getBody(), 'Invoked!');
44+
$this->assertSame('Invoked!', (string) $response->getBody());
4545
}
4646

4747
/**
@@ -65,12 +65,12 @@ public function testErrorHandling($signatureType, $errorStatus, $request = null)
6565
$response = $invoker->handle($request);
6666

6767
// Verify the error message response
68-
$this->assertEquals('', (string) $response->getBody());
69-
$this->assertEquals(500, $response->getStatusCode());
68+
$this->assertEmpty((string) $response->getBody());
69+
$this->assertSame(500, $response->getStatusCode());
7070
$this->assertTrue(
7171
$response->hasHeader(FunctionWrapper::FUNCTION_STATUS_HEADER)
7272
);
73-
$this->assertEquals(
73+
$this->assertSame(
7474
$errorStatus,
7575
$response->getHeaderLine(FunctionWrapper::FUNCTION_STATUS_HEADER)
7676
);

0 commit comments

Comments
 (0)