Browse Source

add-endpoints

pull/4/head
fedy95 5 years ago
parent
commit
5f97bc3902
  1. 4
      composer.json
  2. 425
      composer.lock
  3. 2
      config/bundles.php
  4. 1
      config/packages/json_rpc.yaml
  5. 3
      config/packages/test/validator.yaml
  6. 3
      config/packages/validator.yaml
  7. 5
      config/routes.yaml
  8. 15
      config/services.yaml
  9. 27
      src/Collector/MappingCollector.php
  10. 43
      src/Method/Weather/GetByDateMethod.php
  11. 40
      src/Method/Weather/GetHistoryMethod.php
  12. 39
      src/Repository/HistoryRepository.php
  13. 14
      src/Repository/HistoryRepositoryInterface.php
  14. 28
      symfony.lock
  15. 1
      tests/acceptance.suite.yml
  16. 36
      tests/api/Controller/PageControllerCest.php
  17. 94
      tests/api/Method/Weather/GetByDateMethodCest.php
  18. 74
      tests/api/Method/Weather/GetHistoryMethodCest.php

4
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": "*",

425
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": [

2
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],
];

1
config/packages/json_rpc.yaml

@ -0,0 +1 @@
json_rpc_http_server: ~

3
config/packages/test/validator.yaml

@ -0,0 +1,3 @@
framework:
validation:
not_compromised_password: false

3
config/packages/validator.yaml

@ -0,0 +1,3 @@
framework:
validation:
email_validation_mode: html5

5
config/routes.yaml

@ -1,3 +1,2 @@
#index:
# path: /
# controller: App\Controller\DefaultController::index
json-rpc-endpoint:
resource: '@JsonRpcHttpServerBundle/Resources/config/routing/endpoint.xml'

15
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']

27
src/Collector/MappingCollector.php

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace App\Collector;
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodAwareInterface;
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
class MappingCollector implements JsonRpcMethodAwareInterface
{
/** @var JsonRpcMethodInterface[] */
private array $mappingList = [];
public function addJsonRpcMethod(string $methodName, JsonRpcMethodInterface $method): void
{
$this->mappingList[$methodName] = $method;
}
/**
* @return JsonRpcMethodInterface[]
*/
public function getMappingList(): array
{
return $this->mappingList;
}
}

43
src/Method/Weather/GetByDateMethod.php

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace App\Method\Weather;
use App\Repository\HistoryRepositoryInterface;
use DateTime as DateTimeBase;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Component\Validator\Constraints\Required;
use Yoanm\JsonRpcParamsSymfonyValidator\Domain\MethodWithValidatedParamsInterface;
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
class GetByDateMethod implements JsonRpcMethodInterface, MethodWithValidatedParamsInterface
{
private HistoryRepositoryInterface $historyRepository;
public function __construct(HistoryRepositoryInterface $historyRepository)
{
$this->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']),
]),
]]);
}
}

40
src/Method/Weather/GetHistoryMethod.php

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Method\Weather;
use App\Repository\HistoryRepositoryInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Positive;
use Symfony\Component\Validator\Constraints\Required;
use Yoanm\JsonRpcParamsSymfonyValidator\Domain\MethodWithValidatedParamsInterface;
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
class GetHistoryMethod implements JsonRpcMethodInterface, MethodWithValidatedParamsInterface
{
private HistoryRepositoryInterface $historyRepository;
public function __construct(HistoryRepositoryInterface $historyRepository)
{
$this->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(),
]),
]]);
}
}

39
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;
}
}

14
src/Repository/HistoryRepositoryInterface.php

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace App\Repository;
use DateTimeInterface;
interface HistoryRepositoryInterface
{
public function findTempByDateAt(DateTimeInterface $date_at): ?float;
public function findWithLimit(int $limit): ?array;
}

28
symfony.lock

@ -495,6 +495,22 @@
"symfony/string": {
"version": "v5.2.2"
},
"symfony/translation-contracts": {
"version": "v2.3.0"
},
"symfony/validator": {
"version": "4.3",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "4.3",
"ref": "d902da3e4952f18d3bf05aab29512eb61cabd869"
},
"files": [
"config/packages/test/validator.yaml",
"config/packages/validator.yaml"
]
},
"symfony/var-dumper": {
"version": "v5.2.2"
},
@ -509,5 +525,17 @@
},
"webmozart/assert": {
"version": "1.9.1"
},
"yoanm/jsonrpc-params-symfony-validator-sdk": {
"version": "v2.0.2"
},
"yoanm/jsonrpc-server-sdk": {
"version": "v3.1.2"
},
"yoanm/symfony-jsonrpc-http-server": {
"version": "v3.0.4"
},
"yoanm/symfony-jsonrpc-params-validator": {
"version": "v2.0.2"
}
}

1
tests/acceptance.suite.yml

@ -9,5 +9,6 @@ modules:
url: http://nginx/
proxy: ''
curl:
CURLOPT_RETURNTRANSFER: true
CURLOPT_TIMEOUT: 10

36
tests/api/Controller/PageControllerCest.php

@ -1,36 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Tests\api\Controller;
use App\Tests\ApiTester;
use Codeception\Example;
use Iterator;
class PageControllerCest
{
private ApiTester $tester;
public function _before(ApiTester $tester): void
{
$this->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' => '/',
];
}
}

94
tests/api/Method/Weather/GetByDateMethodCest.php

@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace App\Tests\api\Method\Weather;
use App\Tests\ApiTester;
use Codeception\Example;
use DateTime;
class GetByDateMethodCest
{
private ApiTester $tester;
public function _before(ApiTester $tester): void
{
$this->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',
],
],
];
}
}

74
tests/api/Method/Weather/GetHistoryMethodCest.php

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace App\Tests\api\Method\Weather;
use App\Tests\ApiTester;
use Codeception\Example;
class GetHistoryMethodCest
{
private ApiTester $tester;
public function _before(ApiTester $tester): void
{
$this->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',
],
],
];
}
}