Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve flaky tests (WIP) #150

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"require-dev": {
"phpunit/phpunit": "^9.6|^10.1",
"squizlabs/php_codesniffer": "^3.6",
"vimeo/psalm": "^5.11"
"vimeo/psalm": "^5.11",
"ext-zlib": "*"
},
"provide": {
"psr/log-implementation": "3.0.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/Gelf/Test/Encoder/CompressedJsonEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testEncode()
$this->message
->expects($this->once())
->method('toArray')
->will($this->returnValue($testData));
->willReturn($testData);

$bytes = $this->encoder->encode($this->message);

Expand Down
4 changes: 2 additions & 2 deletions tests/Gelf/Test/Encoder/JsonEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testEncode(): void
$this->message
->expects($this->once())
->method('toArray')
->will($this->returnValue($testData));
->willReturn($testData);

$json = $this->encoder->encode($this->message);

Expand All @@ -53,7 +53,7 @@ public function testUnicodeEncode(): void
$this->message
->expects($this->once())
->method('toArray')
->will($this->returnValue($testData));
->willReturn($testData);

$json = $this->encoder->encode($this->message);

Expand Down
4 changes: 2 additions & 2 deletions tests/Gelf/Test/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ function (MessageInterface $message) {

private function validatePublish(Closure $validator): void
{
$this->publisher->expects($this->once())->method('publish')->will(
$this->returnCallback($validator)
$this->publisher->expects($this->once())->method('publish')->willReturnCallback(
$validator
);
}

Expand Down
12 changes: 6 additions & 6 deletions tests/Gelf/Test/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public function testLevel(): void

public function testLevelInvalidString(): void
{
self::expectException(RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->message->setLevel("invalid");
}

public function testLevelInvalidInteger()
{
self::expectException(RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->message->setLevel(8);
}

Expand All @@ -85,13 +85,13 @@ public function testLogLevelToPsr(): void

public function testLogLevelToPsrInvalidString(): void
{
self::expectException(RuntimeException::class);
$this->expectException(RuntimeException::class);
Message::logLevelToPsr("invalid");
}

public function testLogLevelToPsrInvalidInt(): void
{
self::expectException(RuntimeException::class);
$this->expectException(RuntimeException::class);
Message::logLevelToPsr(-1);
}

Expand Down Expand Up @@ -134,13 +134,13 @@ public function testAdditionals(): void

public function testSetAdditionalEmptyKey(): void
{
self::expectException(RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->message->setAdditional("", "test");
}

public function testGetAdditionalInvalidKey(): void
{
self::expectException(RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->message->getAdditional("invalid");
}

Expand Down
16 changes: 7 additions & 9 deletions tests/Gelf/Test/MessageValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,19 @@ private function getMessage(
): MessageInterface {
$msg = $this->createMock(MessageInterface::class);
$msg->expects($this->any())->method('getHost')
->will($this->returnValue($host));
->willReturn($host);
$msg->expects($this->any())->method('getVersion')
->will($this->returnValue($version));
->willReturn($version);
$msg->expects($this->any())->method('getShortMessage')
->will($this->returnValue($shortMessage));
->willReturn($shortMessage);

$msg->expects($this->any())->method('getAllAdditionals')
->will($this->returnValue($additionals));
->willReturn($additionals);

$msg->expects($this->any())->method('hasAdditional')
->will($this->returnCallback(
function ($key) use ($additionals) {
return isset($additionals[$key]);
}
));
->willReturnCallback(function ($key) use ($additionals) {
return isset($additionals[$key]);
});

return $msg;
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Gelf/Test/PublisherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,24 @@ public function testPublish(): void

$this->messageValidator->expects($this->once())
->method('validate')
->will($this->returnValue(true));
->willReturn(true);

$this->publisher->publish($this->message);
}

public function testPublishErrorOnInvalid(): void
{
self::expectException(RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->messageValidator->expects($this->once())
->method('validate')
->will($this->returnValue(false));
->willReturn(false);

$this->publisher->publish($this->message);
}

public function testMissingTransport(): void
{
self::expectException(RuntimeException::class);
$this->expectException(RuntimeException::class);
$publisher = new Publisher(null, $this->messageValidator);
self::assertCount(0, $publisher->getTransports());

Expand All @@ -88,7 +88,7 @@ public function testMultipleTransports(): void

$this->messageValidator->expects($this->once())
->method('validate')
->will($this->returnValue(true));
->willReturn(true);

$pub->publish($this->message);
}
Expand Down
34 changes: 17 additions & 17 deletions tests/Gelf/Test/Transport/HttpTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public function setUp(): void

// create an encoder always return $testMessage
$this->encoder = $this->createMock(EncoderInterface::class);
$this->encoder->expects($this->any())->method('encode')->will(
$this->returnValue($this->testMessage)
$this->encoder->expects($this->any())->method('encode')->willReturn(
$this->testMessage
);

$this->transport = $this->getTransport();
Expand Down Expand Up @@ -83,13 +83,13 @@ public function testConstructor(): void

public function testFromUrlConstructorInvalidUri(): void
{
self::expectException(InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
HttpTransport::fromUrl('-://:-');
}

public function testFromUrlConstructorInvalidScheme(): void
{
self::expectException(InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
HttpTransport::fromUrl('ftp://foobar');
}

Expand Down Expand Up @@ -152,7 +152,7 @@ public function testSslOptionsAreUsed(): void
$sslOptions = $this->createMock(SslOptions::class);
$sslOptions->expects($this->exactly(2))
->method('toStreamContext')
->will($this->returnValue(array('ssl' => null)));
->willReturn(array('ssl' => null));

$transport = new HttpTransport("localhost", 12345, "/gelf", $sslOptions);

Expand All @@ -174,8 +174,8 @@ public function testSetEncoder(): void

public function testEmptyResponseException(): void
{
self::expectException(RuntimeException::class);
self::expectExceptionMessage(
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(
"Graylog-Server didn't answer properly, expected 'HTTP/1.x 202 Accepted', response is ''"
);

Expand All @@ -201,7 +201,7 @@ public function testSendUncompressed(): void
$this->socketClient
->expects($this->once())
->method("read")
->will($this->returnValue("HTTP/1.1 202 Accepted\r\n\r\n"));
->willReturn("HTTP/1.1 202 Accepted\r\n\r\n");

$this->transport->send($this->message);
}
Expand All @@ -212,17 +212,17 @@ public function testAuthentication(): void

$this->socketClient->expects($this->once())
->method("write")
->will($this->returnCallback(function ($data) {
->willReturnCallback(function ($data) {
self::assertStringContainsString("Authorization: Basic " . base64_encode("test:test"), $data);
return 1;
}));
});



$this->socketClient
->expects($this->once())
->method("read")
->will($this->returnValue("HTTP/1.1 202 Accepted\r\n\r\n"));
->willReturn("HTTP/1.1 202 Accepted\r\n\r\n");

$this->transport->send($this->message);
}
Expand Down Expand Up @@ -265,14 +265,14 @@ public function testSendCompressed(): void
$this->socketClient
->expects($this->once())
->method("read")
->will($this->returnValue("HTTP/1.1 202 Accepted\r\n\r\n"));
->willReturn("HTTP/1.1 202 Accepted\r\n\r\n");

$compressedEncoder = $this->createMock(CompressedJsonEncoder::class);
$compressedEncoder
->expects($this->any())
->method('encode')
->will(
$this->returnValue($this->testMessage)
->willReturn(
$this->testMessage
);
$this->transport->setMessageEncoder($compressedEncoder);

Expand All @@ -284,7 +284,7 @@ public function testCloseSocketOnHttpOneZero(): void
$this->socketClient
->expects($this->once())
->method("read")
->will($this->returnValue("HTTP/1.0 202 Accepted\r\n\r\n"));
->willReturn("HTTP/1.0 202 Accepted\r\n\r\n");

$this->socketClient
->expects($this->once())
Expand All @@ -298,7 +298,7 @@ public function testCloseSocketOnConnectionClose(): void
$this->socketClient
->expects($this->once())
->method("read")
->will($this->returnValue("HTTP/1.1 202 Accepted\r\nConnection: Close\r\n\r\n"));
->willReturn("HTTP/1.1 202 Accepted\r\nConnection: Close\r\n\r\n");

$this->socketClient
->expects($this->once())
Expand All @@ -312,7 +312,7 @@ public function testConnectTimeout(): void
$this->socketClient
->expects($this->once())
->method('getConnectTimeout')
->will($this->returnValue(123));
->willReturn(123);

self::assertEquals(123, $this->transport->getConnectTimeout());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testSendSuccess(): void
$this->transport->expects($this->once())
->method('send')
->with($this->message)
->will($this->returnValue(42));
->willReturn(42);

$bytes = $this->wrapper->send($this->message);

Expand All @@ -57,8 +57,8 @@ public function testSendSuccessAfterRetry(): void

public function testSendFailTwiceWithoutResponse(): void
{
self::expectException(RuntimeException::class);
self::expectExceptionMessage("response is ''");
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage("response is ''");

$expectedException1 = new RuntimeException(self::FAILURE_MESSAGE);
$expectedException2 = new RuntimeException(self::FAILURE_MESSAGE);
Expand Down
4 changes: 2 additions & 2 deletions tests/Gelf/Test/Transport/SslOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function testToStreamContextWithHostname(): void
self::assertArrayNotHasKey('CN_match', $context['ssl']);
self::assertArrayHasKey($sniPeerNameKey, $context['ssl']);

self::assertEquals(true, $context['ssl']['SNI_enabled']);
self::assertTrue($context['ssl']['SNI_enabled']);
self::assertEquals($host, $context['ssl'][$sniPeerNameKey]);


Expand All @@ -109,7 +109,7 @@ public function testToStreamContextWithHostname(): void
self::assertArrayHasKey($peerNameKey, $context['ssl']);
self::assertArrayHasKey($sniPeerNameKey, $context['ssl']);

self::assertEquals(true, $context['ssl']['SNI_enabled']);
self::assertTrue($context['ssl']['SNI_enabled']);
self::assertEquals($host, $context['ssl'][$peerNameKey]);
self::assertEquals($host, $context['ssl'][$sniPeerNameKey]);
}
Expand Down
Loading