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

Add Steam API client mock #296

Merged
merged 1 commit into from
Oct 13, 2023
Merged
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
12 changes: 12 additions & 0 deletions config/packages/http_client.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
framework:
http_client:
scoped_clients:
client.steam_api:
scope: 'https://(api|store).steampowered.com'

when@test:
services:
client.steam_api:
class: Symfony\Component\HttpClient\MockHttpClient
arguments:
$responseFactory: '@App\Service\SteamApiClient\Test\SteamApiClientMockResponseFactory'
4 changes: 4 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ services:
arguments:
$storagePath: '%kernel.project_dir%/var/cache_http/%kernel.environment%'

App\Service\SteamApiClient\SteamApiClient:
arguments:
$httpClient: '@client.steam_api'

App\Service\Version\VersionProvider:
arguments:
$projectDir: '%kernel.project_dir%'
Expand Down
4 changes: 2 additions & 2 deletions src/Form/Dlc/DataTransformer/DlcFormDtoDataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use App\Form\Dlc\Dto\DlcFormDto;
use App\Form\FormDtoInterface;
use App\Form\RegisteredDataTransformerInterface;
use App\Service\Steam\Helper\SteamHelper;
use App\Service\Steam\SteamApiClientInterface;
use App\Service\SteamApiClient\Helper\SteamHelper;
use App\Service\SteamApiClient\SteamApiClientInterface;
use Ramsey\Uuid\Uuid;

class DlcFormDtoDataTransformer implements RegisteredDataTransformerInterface
Expand Down
4 changes: 2 additions & 2 deletions src/Form/Mod/DataTransformer/ModFormDtoDataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use App\Form\FormDtoInterface;
use App\Form\Mod\Dto\ModFormDto;
use App\Form\RegisteredDataTransformerInterface;
use App\Service\Steam\Helper\SteamHelper;
use App\Service\Steam\SteamApiClientInterface;
use App\Service\SteamApiClient\Helper\SteamHelper;
use App\Service\SteamApiClient\SteamApiClientInterface;
use Ramsey\Uuid\Uuid;

class ModFormDtoDataTransformer implements RegisteredDataTransformerInterface
Expand Down
9 changes: 0 additions & 9 deletions src/Service/Steam/Exception/AppNotFoundException.php

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Service\Steam\Dto;
namespace App\Service\SteamApiClient\Dto;

class AppInfoDto
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Service\Steam\Dto;
namespace App\Service\SteamApiClient\Dto;

class WorkshopItemInfoDto
{
Expand Down
13 changes: 13 additions & 0 deletions src/Service/SteamApiClient/Exception/AppNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Service\SteamApiClient\Exception;

class AppNotFoundException extends ClientException
{
public static function createForAppId(int $appId): self
{
return new self(sprintf('No apps found by app id "%s"!', $appId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Service\Steam\Exception;
namespace App\Service\SteamApiClient\Exception;

class ClientException extends \Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Service\SteamApiClient\Exception;

class WorkshopItemNotFoundException extends ClientException
{
public static function createForItemId(int $itemId): self
{
return new self(sprintf('No items found by item id "%s"!', $itemId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Service\Steam\Helper\Exception;
namespace App\Service\SteamApiClient\Helper\Exception;

class InvalidAppUrlFormatException extends \Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Service\Steam\Helper\Exception;
namespace App\Service\SteamApiClient\Helper\Exception;

class InvalidWorkshopItemUrlFormatException extends \Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace App\Service\Steam\Helper;
namespace App\Service\SteamApiClient\Helper;

use App\Service\Steam\Helper\Exception\InvalidAppUrlFormatException;
use App\Service\Steam\Helper\Exception\InvalidWorkshopItemUrlFormatException;
use App\Service\SteamApiClient\Helper\Exception\InvalidAppUrlFormatException;
use App\Service\SteamApiClient\Helper\Exception\InvalidWorkshopItemUrlFormatException;

class SteamHelper
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace App\Service\Steam;
namespace App\Service\SteamApiClient;

use App\Service\Steam\Dto\AppInfoDto;
use App\Service\Steam\Dto\WorkshopItemInfoDto;
use App\Service\Steam\Exception\AppNotFoundException;
use App\Service\Steam\Exception\WorkshopItemNotFoundException;
use App\Service\SteamApiClient\Dto\AppInfoDto;
use App\Service\SteamApiClient\Dto\WorkshopItemInfoDto;
use App\Service\SteamApiClient\Exception\AppNotFoundException;
use App\Service\SteamApiClient\Exception\WorkshopItemNotFoundException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\HttpClient\HttpClientInterface;

Expand All @@ -32,12 +32,13 @@ public function getWorkshopItemInfo(int $itemId): WorkshopItemInfoDto

$responseArray = $response->toArray();
$responseKey = $responseArray['response'];
$itemsCount = $responseKey['resultcount'];
if (1 !== $itemsCount) {
throw new WorkshopItemNotFoundException(sprintf('No items found by item id "%s"!', $itemId));
$publishedFileDetails = $responseKey['publishedfiledetails'][0] ?? null;

$timeCreated = $publishedFileDetails['time_created'] ?? null;
if (!$timeCreated) {
throw WorkshopItemNotFoundException::createForItemId($itemId);
}

$publishedFileDetails = $responseKey['publishedfiledetails'][0] ?? null;
$name = $publishedFileDetails['title'] ?? null;
$gameId = $publishedFileDetails['creator_app_id'] ?? null;

Expand All @@ -57,7 +58,7 @@ public function getAppInfo(int $appId): AppInfoDto
$responseKey = $responseArray[$appId];
$success = $responseKey['success'];
if (!$success) {
throw new AppNotFoundException(sprintf('No apps found by app id "%s"!', $appId));
throw AppNotFoundException::createForAppId($appId);
}

$data = $responseKey['data'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace App\Service\Steam;
namespace App\Service\SteamApiClient;

use App\Service\Steam\Dto\AppInfoDto;
use App\Service\Steam\Dto\WorkshopItemInfoDto;
use App\Service\SteamApiClient\Dto\AppInfoDto;
use App\Service\SteamApiClient\Dto\WorkshopItemInfoDto;

interface SteamApiClientInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace App\Service\SteamApiClient\Test;

use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Contracts\HttpClient\ResponseInterface;

class SteamApiClientMockResponseFactory
{
public function __invoke(string $method, string $url, array $options = []): ResponseInterface
{
return match (true) {
'POST' === $method && 'https://api.steampowered.com/ISteamRemoteStorage/GetPublishedFileDetails/v1/' === $url => $this->getGetPublishedFileDetailsResponse($method, $url, $options),
'GET' === $method && str_starts_with($url, 'https://store.steampowered.com/api/appdetails') => $this->getStoreResponse($method, $url, $options),
default => throw new \InvalidArgumentException('Unsupported request provided')
};
}

private function getGetPublishedFileDetailsResponse(string $method, string $url, array $options = []): MockResponse
{
$params = $this->parseFormDataParameters($options);
$publishedFileIds = $params['publishedfileids[0]'];
$path = sprintf('%s/data/GetPublishedFileDetails/%s.json', __DIR__, $publishedFileIds);
$json = @file_get_contents($path);
if (!$json) {
return $this->createPublishedFileDetailsNotFoundResponse($publishedFileIds);
}

return $this->createJsonResponse($json);
}

private function createPublishedFileDetailsNotFoundResponse(string $publishedFileIds): MockResponse
{
return $this->createJsonResponse([
'response' => [
'result' => 1,
'resultcount' => 1,
'publishedfiledetails' => [
[
'publishedfileid' => $publishedFileIds,
'result' => 1,
],
],
],
]);
}

private function getStoreResponse(string $method, string $url, array $options = []): MockResponse
{
$appId = $options['query']['appids'];
$path = sprintf('%s/data/appdetails/%s.json', __DIR__, $appId);
$json = @file_get_contents($path);
if (!$json) {
return $this->createAppDetailsNotFoundResponse($appId);
}

return $this->createJsonResponse($json);
}

private function createAppDetailsNotFoundResponse(int $appId): MockResponse
{
return $this->createJsonResponse([
$appId => [
'success' => false,
],
]);
}

private function parseFormDataParameters(array $options): array
{
$params = [];
$formDataParams = explode('&', urldecode($options['body']));
foreach ($formDataParams as $formDataParam) {
[$paramName, $paramValue] = explode('=', $formDataParam);
$params[$paramName] = $paramValue;
}

return $params;
}

private function createJsonResponse(array|string $json): MockResponse
{
if (\is_array($json)) {
$json = json_encode($json, JSON_THROW_ON_ERROR);
}

return new MockResponse($json, [
'response_headers' => [
'content-type' => 'application/json',
],
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"response": {
"result": 1,
"resultcount": 1,
"publishedfiledetails": [
{
"publishedfileid": "1934142795",
"result": 1,
"creator": "76561199002669156",
"creator_app_id": 107410,
"consumer_app_id": 107410,
"filename": "",
"file_size": 30551990,
"file_url": "",
"hcontent_file": "5032060125057666325",
"preview_url": "https://steamuserimages-a.akamaihd.net/ugc/770609890915323775/A25459A5CA3CCDA214F4B5E1D8537D125E4739AD/",
"hcontent_preview": "770609890915323775",
"title": "ArmaForces - Mods",
"description": "[b]ArmaForces Mods[/b] is a collaborative effort by the members of [url=https://armaforces.com]ArmaForces[/url], polish Arma 3 community.\r\n\r\nMain purpose of this addon is to bend and adjust Arma 3 game features to the Group gameplay needs and style.\r\n\r\n\r\nhttps://github.com/ArmaForces/Mods",
"time_created": 1576074419,
"time_updated": 1696695976,
"visibility": 0,
"banned": 0,
"ban_reason": "",
"subscriptions": 360,
"favorited": 8,
"lifetime_subscriptions": 728,
"lifetime_favorited": 9,
"views": 894,
"tags": [
{
"tag": "Mod"
},
{
"tag": "Mechanics"
},
{
"tag": "Editor Extension"
}
]
}
]
}
}
433 changes: 433 additions & 0 deletions src/Service/SteamApiClient/Test/data/appdetails/1227700.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Twig/SteamExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Twig;

use App\Service\Steam\Helper\SteamHelper;
use App\Service\SteamApiClient\Helper\SteamHelper;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

Expand Down
8 changes: 4 additions & 4 deletions src/Validator/Dlc/SteamStoreArma3DlcUrlValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace App\Validator\Dlc;

use App\Form\Dlc\Dto\DlcFormDto;
use App\Service\Steam\Exception\AppNotFoundException;
use App\Service\Steam\Helper\Exception\InvalidAppUrlFormatException;
use App\Service\Steam\Helper\SteamHelper;
use App\Service\Steam\SteamApiClientInterface;
use App\Service\SteamApiClient\Exception\AppNotFoundException;
use App\Service\SteamApiClient\Helper\Exception\InvalidAppUrlFormatException;
use App\Service\SteamApiClient\Helper\SteamHelper;
use App\Service\SteamApiClient\SteamApiClientInterface;
use App\Validator\AbstractValidator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Constraint;
Expand Down
4 changes: 2 additions & 2 deletions src/Validator/Dlc/UniqueSteamStoreDlcValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use App\Entity\Dlc\Dlc;
use App\Form\Dlc\Dto\DlcFormDto;
use App\Service\Steam\Helper\Exception\InvalidAppUrlFormatException;
use App\Service\Steam\Helper\SteamHelper;
use App\Service\SteamApiClient\Helper\Exception\InvalidAppUrlFormatException;
use App\Service\SteamApiClient\Helper\SteamHelper;
use App\Validator\AbstractValidator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
Expand Down
Loading
Loading