diff --git a/composer.json b/composer.json index e27293f..b05af63 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,9 @@ "symfony/flex": "^1.3.1", "symfony/framework-bundle": "5.2.*", "symfony/proxy-manager-bridge": "5.2.*", - "symfony/yaml": "5.2.*" + "symfony/yaml": "5.2.*", + "yoanm/symfony-jsonrpc-http-server": "^3.0", + "yoanm/symfony-jsonrpc-params-validator": "^2.0" }, "replace": { "symfony/polyfill-ctype": "*", diff --git a/composer.lock b/composer.lock index c65bb28..5e27974 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0f6245b0b31a15c0d64f49ff50212b9d", + "content-hash": "4bf919f113973c82b676001df4ed7651", "packages": [ { "name": "composer/package-versions-deprecated", @@ -4197,6 +4197,195 @@ ], "time": "2021-01-25T15:14:59+00:00" }, + { + "name": "symfony/translation-contracts", + "version": "v2.3.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/translation-contracts.git", + "reference": "e2eaa60b558f26a4b0354e1bbb25636efaaad105" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/translation-contracts/zipball/e2eaa60b558f26a4b0354e1bbb25636efaaad105", + "reference": "e2eaa60b558f26a4b0354e1bbb25636efaaad105", + "shasum": "" + }, + "require": { + "php": ">=7.2.5" + }, + "suggest": { + "symfony/translation-implementation": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.3-dev" + }, + "thanks": { + "name": "symfony/contracts", + "url": "https://github.com/symfony/contracts" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Contracts\\Translation\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Generic abstractions related to translation", + "homepage": "https://symfony.com", + "keywords": [ + "abstractions", + "contracts", + "decoupling", + "interfaces", + "interoperability", + "standards" + ], + "support": { + "source": "https://github.com/symfony/translation-contracts/tree/v2.3.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2020-09-28T13:05:58+00:00" + }, + { + "name": "symfony/validator", + "version": "v5.2.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/validator.git", + "reference": "c2c234d80dad3925247b0a3fbbcecfe676e2b551" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/validator/zipball/c2c234d80dad3925247b0a3fbbcecfe676e2b551", + "reference": "c2c234d80dad3925247b0a3fbbcecfe676e2b551", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/deprecation-contracts": "^2.1", + "symfony/polyfill-ctype": "~1.8", + "symfony/polyfill-mbstring": "~1.0", + "symfony/polyfill-php73": "~1.0", + "symfony/polyfill-php80": "^1.15", + "symfony/translation-contracts": "^1.1|^2" + }, + "conflict": { + "doctrine/lexer": "<1.0.2", + "phpunit/phpunit": "<5.4.3", + "symfony/dependency-injection": "<4.4", + "symfony/expression-language": "<5.1", + "symfony/http-kernel": "<4.4", + "symfony/intl": "<4.4", + "symfony/translation": "<4.4", + "symfony/yaml": "<4.4" + }, + "require-dev": { + "doctrine/annotations": "^1.10.4", + "doctrine/cache": "~1.0", + "egulias/email-validator": "^2.1.10", + "symfony/cache": "^4.4|^5.0", + "symfony/config": "^4.4|^5.0", + "symfony/console": "^4.4|^5.0", + "symfony/dependency-injection": "^4.4|^5.0", + "symfony/expression-language": "^5.1", + "symfony/finder": "^4.4|^5.0", + "symfony/http-client": "^4.4|^5.0", + "symfony/http-foundation": "^4.4|^5.0", + "symfony/http-kernel": "^4.4|^5.0", + "symfony/intl": "^4.4|^5.0", + "symfony/mime": "^4.4|^5.0", + "symfony/property-access": "^4.4|^5.0", + "symfony/property-info": "^4.4|^5.0", + "symfony/translation": "^4.4|^5.0", + "symfony/yaml": "^4.4|^5.0" + }, + "suggest": { + "doctrine/annotations": "For using the annotation mapping. You will also need doctrine/cache.", + "doctrine/cache": "For using the default cached annotation reader.", + "egulias/email-validator": "Strict (RFC compliant) email validation", + "psr/cache-implementation": "For using the mapping cache.", + "symfony/config": "", + "symfony/expression-language": "For using the Expression validator and the ExpressionLanguageSyntax constraints", + "symfony/http-foundation": "", + "symfony/intl": "", + "symfony/property-access": "For accessing properties within comparison constraints", + "symfony/property-info": "To automatically add NotNull and Type constraints", + "symfony/translation": "For translating validation errors.", + "symfony/yaml": "" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Validator\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides tools to validate values", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/validator/tree/v5.2.2" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2021-01-27T12:56:27+00:00" + }, { "name": "symfony/var-dumper", "version": "v5.2.2", @@ -4432,6 +4621,240 @@ } ], "time": "2021-01-27T10:01:46+00:00" + }, + { + "name": "yoanm/jsonrpc-params-symfony-validator-sdk", + "version": "v2.0.2", + "source": { + "type": "git", + "url": "https://github.com/yoanm/php-jsonrpc-params-symfony-validator-sdk.git", + "reference": "f417a0c62195bfceee6556c3d88fe7a27bf045bb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/yoanm/php-jsonrpc-params-symfony-validator-sdk/zipball/f417a0c62195bfceee6556c3d88fe7a27bf045bb", + "reference": "f417a0c62195bfceee6556c3d88fe7a27bf045bb", + "shasum": "" + }, + "require": { + "php": ">=7.2", + "symfony/validator": "^4.0 || ^5.0", + "yoanm/jsonrpc-server-sdk": "^3.0" + }, + "require-dev": { + "behat/behat": "~3.0", + "phpunit/phpunit": "^7.0 || ^8.0", + "squizlabs/php_codesniffer": "3.*", + "yoanm/php-unit-extended": "^1.0" + }, + "suggest": { + "yoanm/jsonrpc-params-symfony-constraint-doc-sdk": "To generate payload documentation from validation constraints", + "yoanm/symfony-jsonrpc-http-server": "Symfony Bundle to convert an HTTP json-rpc request into HTTP json-rpc response", + "yoanm/symfony-jsonrpc-params-validator": "Symfony bundle for easy JSON-RPC params validation" + }, + "type": "library", + "autoload": { + "psr-4": { + "Yoanm\\JsonRpcParamsSymfonyValidator\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Yoanm", + "email": "yoanm@users.noreply.github.com", + "role": "Developer" + } + ], + "description": "Simple JSON-RPC params validator that use Symfony validator component", + "support": { + "issues": "https://github.com/yoanm/php-jsonrpc-params-symfony-validator-sdk/issues", + "source": "https://github.com/yoanm/php-jsonrpc-params-symfony-validator-sdk/tree/v2.0.2" + }, + "time": "2020-07-09T04:42:09+00:00" + }, + { + "name": "yoanm/jsonrpc-server-sdk", + "version": "v3.1.2", + "source": { + "type": "git", + "url": "https://github.com/yoanm/php-jsonrpc-server-sdk.git", + "reference": "dcd886d0ae9246129ec8fbf5e082eff1fc3c49ea" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/yoanm/php-jsonrpc-server-sdk/zipball/dcd886d0ae9246129ec8fbf5e082eff1fc3c49ea", + "reference": "dcd886d0ae9246129ec8fbf5e082eff1fc3c49ea", + "shasum": "" + }, + "require": { + "ext-json": "*", + "php": ">=7.1" + }, + "require-dev": { + "behat/behat": "~3.0", + "phpunit/phpunit": "^7.0 || ^8.0", + "squizlabs/php_codesniffer": "3.*", + "yoanm/jsonrpc-server-doc-sdk": "^0.2", + "yoanm/php-unit-extended": "^1.0" + }, + "suggest": { + "yoanm/jsonrpc-params-symfony-validator-sdk": "Compatible params validator", + "yoanm/jsonrpc-server-doc-sdk": "Create documentation for your server", + "yoanm/jsonrpc-server-sdk-psr11-resolver": "Compatible PSR11 method resolver", + "yoanm/symfony-jsonrpc-http-server": "JSON-RPC HTTP Symfony server Bundle" + }, + "type": "library", + "autoload": { + "psr-4": { + "Yoanm\\JsonRpcServer\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Yoanm", + "email": "yoanm@users.noreply.github.com", + "role": "Developer" + } + ], + "description": "Server SDK to convert a json-rpc request string into json-rpc response string", + "support": { + "issues": "https://github.com/yoanm/php-jsonrpc-server-sdk/issues", + "source": "https://github.com/yoanm/php-jsonrpc-server-sdk/tree/v3.1.2" + }, + "time": "2020-07-09T04:39:07+00:00" + }, + { + "name": "yoanm/symfony-jsonrpc-http-server", + "version": "v3.0.4", + "source": { + "type": "git", + "url": "https://github.com/yoanm/symfony-jsonrpc-http-server.git", + "reference": "72c500822452d570423a986f5ae4107faf3c7ec7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/yoanm/symfony-jsonrpc-http-server/zipball/72c500822452d570423a986f5ae4107faf3c7ec7", + "reference": "72c500822452d570423a986f5ae4107faf3c7ec7", + "shasum": "" + }, + "require": { + "php": ">=7.2", + "psr/container": "^1.0", + "symfony/config": "^4.0 || ^5.0", + "symfony/dependency-injection": "^4.0 || ^5.0", + "symfony/event-dispatcher": "^4.0 || ^5.0", + "symfony/event-dispatcher-contracts": "^1.0 || ^2.0", + "symfony/http-foundation": "^4.0 || ^5.0", + "symfony/http-kernel": "^4.0 || ^5.0", + "yoanm/jsonrpc-server-sdk": "^3.0" + }, + "require-dev": { + "behat/behat": "~3.0", + "matthiasnoback/symfony-config-test": "^3.0 || ^4.0", + "matthiasnoback/symfony-dependency-injection-test": "^3.0 || ^4.0", + "phpunit/phpunit": "^7.0 || ^8.0", + "squizlabs/php_codesniffer": "3.*", + "symfony/framework-bundle": "^4.0 || ^5.0", + "symfony/routing": "^4.0 || ^5.0", + "yoanm/php-unit-extended": "~1.0" + }, + "suggest": { + "yoanm/symfony-jsonrpc-http-server-doc": "JSON-RPC documentation Bundle", + "yoanm/symfony-jsonrpc-params-validator": "Symfony bundle for easy JSON-RPC params validation" + }, + "type": "library", + "autoload": { + "psr-4": { + "Yoanm\\SymfonyJsonRpcHttpServer\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Yoanm", + "email": "yoanm@users.noreply.github.com", + "role": "Developer" + } + ], + "description": "Symfony Bundle to convert an HTTP json-rpc request into HTTP json-rpc response", + "support": { + "issues": "https://github.com/yoanm/symfony-jsonrpc-http-server/issues", + "source": "https://github.com/yoanm/symfony-jsonrpc-http-server/tree/v3.0.4" + }, + "time": "2020-12-08T06:44:07+00:00" + }, + { + "name": "yoanm/symfony-jsonrpc-params-validator", + "version": "v2.0.2", + "source": { + "type": "git", + "url": "https://github.com/yoanm/symfony-jsonrpc-params-validator.git", + "reference": "4ed9d1facaeb19e651a2a0528e3977c5585b55f8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/yoanm/symfony-jsonrpc-params-validator/zipball/4ed9d1facaeb19e651a2a0528e3977c5585b55f8", + "reference": "4ed9d1facaeb19e651a2a0528e3977c5585b55f8", + "shasum": "" + }, + "require": { + "php": ">=7.2", + "symfony/config": "^4.0 || ^5.0", + "symfony/dependency-injection": "^4.0 || ^5.0", + "symfony/http-kernel": "^4.0 || ^5.0", + "yoanm/jsonrpc-params-symfony-validator-sdk": "^2.0", + "yoanm/jsonrpc-server-sdk": "^3.0", + "yoanm/symfony-jsonrpc-http-server": "^3.0" + }, + "require-dev": { + "behat/behat": "~3.0", + "ext-json": "*", + "matthiasnoback/symfony-config-test": "^3.0 || ^4.0", + "matthiasnoback/symfony-dependency-injection-test": "^3.0 || ^4.0", + "phpunit/phpunit": "^7.0 || ^8.0", + "squizlabs/php_codesniffer": "3.*", + "symfony/framework-bundle": "^4.0 || ^5.0", + "symfony/routing": "^4.0 || ^5.0", + "yoanm/php-unit-extended": "~1.0" + }, + "suggest": { + "yoanm/symfony-jsonrpc-http-server": "Symfony Bundle to convert an HTTP json-rpc request into HTTP json-rpc response", + "yoanm/symfony-jsonrpc-params-sf-constraints-doc": "To generate payload documentation from validation constraints" + }, + "type": "library", + "autoload": { + "psr-4": { + "Yoanm\\SymfonyJsonRpcParamsValidator\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Yoanm", + "email": "yoanm@users.noreply.github.com", + "role": "Developer" + } + ], + "description": "Symfony bundle for easy JSON-RPC params validation", + "support": { + "issues": "https://github.com/yoanm/symfony-jsonrpc-params-validator/issues", + "source": "https://github.com/yoanm/symfony-jsonrpc-params-validator/tree/v2.0.2" + }, + "time": "2020-07-09T11:44:27+00:00" } ], "packages-dev": [ diff --git a/config/bundles.php b/config/bundles.php index 7d78d9e..40b48f7 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -6,4 +6,6 @@ return [ Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true], Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true], Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true], + Yoanm\SymfonyJsonRpcHttpServer\JsonRpcHttpServerBundle::class => ['all' => true], + Yoanm\SymfonyJsonRpcParamsValidator\JsonRpcParamsValidatorBundle::class => ['all' => true], ]; diff --git a/config/packages/json_rpc.yaml b/config/packages/json_rpc.yaml new file mode 100644 index 0000000..8541b4c --- /dev/null +++ b/config/packages/json_rpc.yaml @@ -0,0 +1 @@ +json_rpc_http_server: ~ diff --git a/config/packages/test/validator.yaml b/config/packages/test/validator.yaml new file mode 100644 index 0000000..1e5ab78 --- /dev/null +++ b/config/packages/test/validator.yaml @@ -0,0 +1,3 @@ +framework: + validation: + not_compromised_password: false diff --git a/config/packages/validator.yaml b/config/packages/validator.yaml new file mode 100644 index 0000000..a695e1a --- /dev/null +++ b/config/packages/validator.yaml @@ -0,0 +1,3 @@ +framework: + validation: + email_validation_mode: html5 diff --git a/config/routes.yaml b/config/routes.yaml index c3283aa..43e950d 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -1,3 +1,2 @@ -#index: -# path: / -# controller: App\Controller\DefaultController::index +json-rpc-endpoint: + resource: '@JsonRpcHttpServerBundle/Resources/config/routing/endpoint.xml' \ No newline at end of file diff --git a/config/services.yaml b/config/services.yaml index 2755ee4..a3df8fc 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -16,3 +16,18 @@ services: App\Controller\: resource: '../src/Controller/' tags: ['controller.service_arguments'] + + jsonrpc.weather.getByDate: + class: App\Method\Weather\GetByDateMethod + tags: + - { name: 'json_rpc_http_server.jsonrpc_method', method: 'weather.getByDate' } + + jsonrpc.weather.getHistory: + class: App\Method\Weather\GetHistoryMethod + tags: + - { name: 'json_rpc_http_server.jsonrpc_method', method: 'weather.getHistory' } + + mapping_aware_service: + public: true + class: App\Collector\MappingCollector + tags: ['json_rpc_http_server.method_aware'] diff --git a/src/Collector/MappingCollector.php b/src/Collector/MappingCollector.php new file mode 100644 index 0000000..2c15fc4 --- /dev/null +++ b/src/Collector/MappingCollector.php @@ -0,0 +1,27 @@ +mappingList[$methodName] = $method; + } + + /** + * @return JsonRpcMethodInterface[] + */ + public function getMappingList(): array + { + return $this->mappingList; + } +} diff --git a/src/Method/Weather/GetByDateMethod.php b/src/Method/Weather/GetByDateMethod.php new file mode 100644 index 0000000..6070d19 --- /dev/null +++ b/src/Method/Weather/GetByDateMethod.php @@ -0,0 +1,43 @@ +historyRepository = $historyRepository; + } + + /** + * {@inheritdoc} + */ + public function apply(array $paramList = null) + { + $date = DateTimeBase::createFromFormat('Y-m-d', $paramList['date']); + + return $this->historyRepository->findTempByDateAt($date); + } + + public function getParamsConstraint(): Constraint + { + return new Collection(['fields' => [ + 'date' => new Required([ + new DateTime(['format' => 'Y-m-d']), + ]), + ]]); + } +} diff --git a/src/Method/Weather/GetHistoryMethod.php b/src/Method/Weather/GetHistoryMethod.php new file mode 100644 index 0000000..908362f --- /dev/null +++ b/src/Method/Weather/GetHistoryMethod.php @@ -0,0 +1,40 @@ +historyRepository = $historyRepository; + } + + /** + * {@inheritdoc} + */ + public function apply(array $paramList = null) + { + return $this->historyRepository->findWithLimit($paramList['lastDays']); + } + + public function getParamsConstraint(): Constraint + { + return new Collection(['fields' => [ + 'lastDays' => new Required([ + new Positive(), + ]), + ]]); + } +} diff --git a/src/Repository/HistoryRepository.php b/src/Repository/HistoryRepository.php index 02d50d8..c1ae53d 100644 --- a/src/Repository/HistoryRepository.php +++ b/src/Repository/HistoryRepository.php @@ -5,19 +5,44 @@ declare(strict_types=1); namespace App\Repository; use App\Entity\History; +use DateTimeInterface; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; -/** - * @method History|null find($id, $lockMode = null, $lockVersion = null) - * @method History|null findOneBy(array $criteria, array $orderBy = null) - * @method History[] findAll() - * @method History[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) - */ -class HistoryRepository extends ServiceEntityRepository +class HistoryRepository extends ServiceEntityRepository implements HistoryRepositoryInterface { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, History::class); } + + public function findTempByDateAt(DateTimeInterface $date_at): ?float + { + $history = $this->findOneBy(['date_at' => $date_at]); + + if (null !== $history) { + return $history->getTemp(); + } + + return null; + } + + public function findWithLimit(int $limit): ?array + { + $histories = $this->findBy([], ['date_at' => 'DESC'], $limit); + + if (null !== $histories) { + $result = []; + foreach ($histories as $history) { + $result[] = [ + 'date_at' => $history->getDateAt()->format('Y-m-d'), + 'temp' => $history->getTemp(), + ]; + } + + return $result; + } + + return null; + } } diff --git a/src/Repository/HistoryRepositoryInterface.php b/src/Repository/HistoryRepositoryInterface.php new file mode 100644 index 0000000..da85ab6 --- /dev/null +++ b/src/Repository/HistoryRepositoryInterface.php @@ -0,0 +1,14 @@ +tester = $tester; - } - - /** - * @dataProvider dataTestBaseAvailability - */ - public function testBaseAvailability(Example $example): void - { - $this->tester->sendGet($example['url']); - $this->tester->seeResponseCodeIs(400); - $this->tester->seeResponseIsJson(); - } - - protected static function dataTestBaseAvailability(): Iterator - { - yield [ - 'url' => '/', - ]; - } -} diff --git a/tests/api/Method/Weather/GetByDateMethodCest.php b/tests/api/Method/Weather/GetByDateMethodCest.php new file mode 100644 index 0000000..9f8442c --- /dev/null +++ b/tests/api/Method/Weather/GetByDateMethodCest.php @@ -0,0 +1,94 @@ +tester = $tester; + } + + /** + * @dataProvider dataApply + */ + public function testApply(Example $example): void + { + $this->tester->sendPost('/json-rpc', $example['body']); + $this->tester->seeResponseIsJson(); + $this->tester->seeResponseContainsJson($example['seeResponseContainsJson']); + $this->tester->seeResponseMatchesJsonType($example['seeResponseMatchesJsonType']); + } + + protected function dataApply(): array + { + return [ + [ + 'body' => sprintf( + '[{"jsonrpc": "2.0", "method": "weather.getByDate", "params": {"date": "%s"}, "id": 1}]', + (new DateTime())->format('Y-m-d') + ), + 'seeResponseContainsJson' => [ + 'jsonrpc' => '2.0', + 'id' => 1, + ], + 'seeResponseMatchesJsonType' => [ + 'jsonrpc' => 'string', + 'id' => 'integer', + 'result' => 'float', + ], + ], + [ + 'body' => sprintf( + '[{"jsonrpc": "2.0", "method": "weather.getByDate", "params": {"date": "%s"}, "id": 1}]', + '9999-01-01' + ), + 'seeResponseContainsJson' => [ + 'jsonrpc' => '2.0', + 'id' => 1, + 'result' => null, + ], + 'seeResponseMatchesJsonType' => [ + 'jsonrpc' => 'string', + 'id' => 'integer', + 'result' => 'null', + ], + ], + [ + 'body' => sprintf( + '[{"jsonrpc": "2.0", "method": "weather.getByDate", "params": {"date": "%s"}, "id": 1}]', + '01-01-9999' + ), + 'seeResponseContainsJson' => [ + 'jsonrpc' => '2.0', + 'id' => 1, + ], + 'seeResponseMatchesJsonType' => [ + 'jsonrpc' => 'string', + 'id' => 'integer', + 'error' => 'array', + ], + ], + [ + 'body' => '[{"jsonrpc": "2.0", "method": "weather.getByDate", "params": [], "id": 1}]', + 'seeResponseContainsJson' => [ + 'jsonrpc' => '2.0', + 'id' => 1, + ], + 'seeResponseMatchesJsonType' => [ + 'jsonrpc' => 'string', + 'id' => 'integer', + 'error' => 'array', + ], + ], + ]; + } +} diff --git a/tests/api/Method/Weather/GetHistoryMethodCest.php b/tests/api/Method/Weather/GetHistoryMethodCest.php new file mode 100644 index 0000000..196b54b --- /dev/null +++ b/tests/api/Method/Weather/GetHistoryMethodCest.php @@ -0,0 +1,74 @@ +tester = $tester; + } + + /** + * @dataProvider dataApply + */ + public function testApply(Example $example): void + { + $this->tester->sendPost('/json-rpc', $example['body']); + $this->tester->seeResponseIsJson(); + $this->tester->seeResponseContainsJson($example['seeResponseContainsJson']); + $this->tester->seeResponseMatchesJsonType($example['seeResponseMatchesJsonType']); + } + + protected function dataApply(): array + { + return [ + [ + 'body' => sprintf( + '[{"jsonrpc": "2.0", "method": "weather.getHistory", "params": {"lastDays": %d}, "id": 1}]', + random_int(1, 1000) + ), + 'seeResponseContainsJson' => ['jsonrpc' => '2.0', 'id' => 1], + 'seeResponseMatchesJsonType' => [ + 'jsonrpc' => 'string', + 'id' => 'integer', + 'result' => 'array', + ], + ], + [ + 'body' => sprintf( + '[{"jsonrpc": "2.0", "method": "weather.getHistory", "params": {"lastDays": %d}, "id": 1}]', + random_int(-1000, 0) + ), + 'seeResponseContainsJson' => [ + 'jsonrpc' => '2.0', + 'id' => 1, + ], + 'seeResponseMatchesJsonType' => [ + 'jsonrpc' => 'string', + 'id' => 'integer', + 'error' => 'array', + ], + ], + [ + 'body' => '[{"jsonrpc": "2.0", "method": "weather.getHistory", "params": [], "id": 1}]', + 'seeResponseContainsJson' => [ + 'jsonrpc' => '2.0', + 'id' => 1, + ], + 'seeResponseMatchesJsonType' => [ + 'jsonrpc' => 'string', + 'id' => 'integer', + 'error' => 'array', + ], + ], + ]; + } +}