Skip to content

Commit

Permalink
test: Make tests more robust
Browse files Browse the repository at this point in the history
Replace assertEquals by assertSame.
  • Loading branch information
mbaumgartl committed Mar 21, 2024
1 parent c505f16 commit 83a1e07
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion tests/AuthorizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testAuthorizationProviderInteraction(): void
]);

self::assertTrue($request->hasHeader('Authorization'), 'Authorization header not available');
self::assertEquals(['am9obmRvZTpzZWNyZXQ='], $request->getHeader('Authorization'));
self::assertSame('am9obmRvZTpzZWNyZXQ=', $request->getHeaderLine('Authorization'));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public function setUp(): void
public function testApiHost(): void
{
$request = (new ApiRequestFactory($this->uri))->create(['resource' => 'user']);
self::assertEquals(['api.example.com'], $request->getHeader('Host'));
self::assertSame('api.example.com', $request->getHeaderLine('Host'));
}

public function testResourceInUrl(): void
{
$request = (new ApiRequestFactory($this->uri))->create(['resource' => 'user']);
$uri = $request->getUri();

self::assertEquals('/user/', $uri->getPath());
self::assertSame('/user/', $uri->getPath());
self::assertStringNotContainsString('resource=', $uri->getQuery());
}

Expand Down
4 changes: 2 additions & 2 deletions tests/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testDefaultHttpRequestTimeout(): void
->getLastOptions();

self::assertArrayHasKey('timeout', $options);
self::assertEquals(30, $options['timeout'], 'Default request timeout not set');
self::assertSame(30, $options['timeout'], 'Default request timeout not set');
}

public function testHttpRequestTimeoutOption(): void
Expand All @@ -39,6 +39,6 @@ public function testHttpRequestTimeoutOption(): void
$client->execute(['resource' => 'user', 'id' => 1337]);

$options = $mockHandler->getLastOptions();
self::assertEquals(5, $options['timeout'], 'Request timeout not set correctly');
self::assertSame(5, $options['timeout'], 'Request timeout not set correctly');
}
}
10 changes: 5 additions & 5 deletions tests/ParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function setUp(): void
public function testRequestParameter($name, $value, $encoded): void
{
$request = (new ApiRequestFactory($this->uri))->create(['resource' => 'user', $name => $value]);
self::assertEquals($name . '=' . $encoded, $request->getUri()->getQuery());
self::assertSame("{$name}={$encoded}", $request->getUri()->getQuery());
}

public static function parameters(): array
Expand Down Expand Up @@ -64,8 +64,8 @@ public function testSendRandomDataAsJson(): void
]
]);

self::assertEquals(['application/json'], $request->getHeader('Content-Type'));
self::assertEquals('{"title":"Lorem Ipsum","author":{"id":1337}}', (string) $request->getBody());
self::assertSame('application/json', $request->getHeaderLine('Content-Type'));
self::assertSame('{"title":"Lorem Ipsum","author":{"id":1337}}', (string) $request->getBody());
}

public function testFormatParameter(): void
Expand All @@ -77,7 +77,7 @@ public function testFormatParameter(): void
'format' => 'image'
]);

self::assertEquals('/user/1337.image', $request->getUri()->getPath());
self::assertSame('/user/1337.image', $request->getUri()->getPath());
self::assertStringNotContainsString('format=', $request->getUri()->getQuery());
}

Expand All @@ -102,7 +102,7 @@ public function testParameterOrder(): void
'filter' => 'address.country.iso2=AT'
]);

self::assertEquals($expectedQueryString, $request->getUri()->getQuery());
self::assertSame($expectedQueryString, $request->getUri()->getQuery());
}

public function testAuthorizeParameter(): void
Expand Down
14 changes: 7 additions & 7 deletions tests/RequestMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ public function setUp(): void
public function testHttpGetMethod(array $params, string $message): void
{
$request = (new ApiRequestFactory($this->uri))->create($params);
self::assertEquals('GET', $request->getMethod(), $message);
self::assertSame('GET', $request->getMethod(), $message);
}

public function testHttpPostMethodForNonRetrieveActions(): void
{
$request = (new ApiRequestFactory($this->uri))
->create(['resource' => 'user', 'id' => 1337, 'action' => 'foobar']);

self::assertEquals('POST', $request->getMethod());
self::assertEquals('application/x-www-form-urlencoded', $request->getHeaderLine('Content-Type'));
self::assertSame('POST', $request->getMethod());
self::assertSame('application/x-www-form-urlencoded', $request->getHeaderLine('Content-Type'));
}

public function testHttpMethodParameterOverwrite(): void
{
$request = (new ApiRequestFactory($this->uri))
->create(['resource' => 'user', 'id' => 1337, 'httpMethod' => 'POST']);

self::assertEquals('POST', $request->getMethod());
self::assertSame('POST', $request->getMethod());
}

public function testHttpPostMethodForLongQueryStrings(): void
Expand All @@ -58,7 +58,7 @@ public function testHttpPostMethodForLongQueryStrings(): void
'page' => 10
]);

self::assertEquals('POST', $request->getMethod());
self::assertSame('POST', $request->getMethod());
self::assertStringContainsString(
'select=',
(string) $request->getBody(),
Expand All @@ -75,8 +75,8 @@ public function testHttpPostMethodForJsonData(): void
'data' => ['title' => 'Lorem Ipsum']
]);

self::assertEquals('POST', $request->getMethod());
self::assertEquals('action=create', $request->getUri()->getQuery());
self::assertSame('POST', $request->getMethod());
self::assertSame('action=create', $request->getUri()->getQuery());
}

public static function defaultHttpMethodDataProvider(): array
Expand Down
14 changes: 7 additions & 7 deletions tests/SelectSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class SelectSerializationTest extends TestCase
{
public function testSimpleArray(): void
{
self::assertEquals('id,name', stringify_select(['id', 'name']));
self::assertSame('id,name', stringify_select(['id', 'name']));
}

public function testNestedArrays(): void
{
self::assertEquals('id,name,attr1,attr2', stringify_select(['id', 'name', ['attr1', 'attr2']]));
self::assertSame('id,name,attr1,attr2', stringify_select(['id', 'name', ['attr1', 'attr2']]));
}

public function testNestedArraysWithNonNumericKeys(): void
Expand All @@ -27,12 +27,12 @@ public function testNestedArraysWithNonNumericKeys(): void
'attr'
];

self::assertEquals('id,name,subGroup[attr1,attr2],attr', stringify_select($spec));
self::assertSame('id,name,subGroup[attr1,attr2],attr', stringify_select($spec));
}

public function testNonNumericKeysWithNonArrayValues(): void
{
self::assertEquals('subGroup.attr', stringify_select(['subGroup' => 'attr']));
self::assertSame('subGroup.attr', stringify_select(['subGroup' => 'attr']));
}

public function testNestedNonNumericArrays(): void
Expand All @@ -41,7 +41,7 @@ public function testNestedNonNumericArrays(): void
'subGroup' => ['subSubGroup' => 'attr']
];

self::assertEquals('subGroup.subSubGroup.attr', stringify_select($spec));
self::assertSame('subGroup.subSubGroup.attr', stringify_select($spec));
}

public function testDeeplyNestedArrays(): void
Expand All @@ -62,12 +62,12 @@ public function testDeeplyNestedArrays(): void
];

$selectString = 'id,name,subGroupA[id,name,subSubGroupA[attr1,attr2],subSubGroupB[subSubSubGroupA[attr1,attr2],subSubSubItem,subSubSubGroupB[attr1,attr2]]]';
self::assertEquals($selectString, stringify_select($spec));
self::assertSame($selectString, stringify_select($spec));
}

public function testSingleItemSubItemGroup(): void
{
self::assertEquals('group.attr', stringify_select(['group' => ['attr']]));
self::assertSame('group.attr', stringify_select(['group' => ['attr']]));
}

public function testError(): void
Expand Down

0 comments on commit 83a1e07

Please sign in to comment.