Browse Source

add-doctrine-support

pull/3/head
fedy95 5 years ago
parent
commit
43f40c031c
  1. 2
      Makefile
  2. 9
      README.md
  3. 16
      composer.json
  4. 2359
      composer.lock
  5. 4
      config/bundles.php
  6. 14
      config/packages/doctrine.yaml
  7. 3
      config/packages/doctrine_migrations.yaml
  8. 20
      config/packages/prod/doctrine.yaml
  9. 4
      devops/docker/.env
  10. 29
      migrations/Version20210129121632.php
  11. 35
      src/DataFixtures/HistoryFixtures.php
  12. 59
      src/Entity/History.php
  13. 21
      src/Repository/HistoryRepository.php
  14. 109
      symfony.lock
  15. 135
      tests/unit/Entity/HistoryTest.php

2
Makefile

@ -27,5 +27,7 @@ enter-php:
$(DOCKER_COMPOSE) exec php bash
prepare-request:
$(DOCKER_COMPOSE) exec -T php bash -c "composer run prepare-request"
data-recreate:
$(DOCKER_COMPOSE) exec -T php bash -c "composer run data-recreate"
.DEFAULT_GOAL := init

9
README.md

@ -1,10 +1,6 @@
# symfony-5-cli-template
# weather_history
Template repo "pure-skeleton" with symfony 5.2. Includes:
- local devops via docker-compose and make
- codeception support with acceptance/api/unit-testing
- php/yaml linters
- php/composer code style fixers
Source with a daily forecasts by the last 6 months
### Requirements
@ -15,6 +11,7 @@ Template repo "pure-skeleton" with symfony 5.2. Includes:
### Install
```shell script
make init
make data-recreate
```
### Development

16
composer.json

@ -6,10 +6,15 @@
"ext-apcu": "5.1.*",
"ext-ctype": "7.4.*",
"ext-iconv": "7.4.*",
"composer/package-versions-deprecated": "1.11.99.1",
"doctrine/doctrine-bundle": "^2.2",
"doctrine/doctrine-migrations-bundle": "^3.0",
"doctrine/orm": "^2.8",
"symfony/console": "5.2.*",
"symfony/dotenv": "5.2.*",
"symfony/flex": "^1.3.1",
"symfony/framework-bundle": "5.2.*",
"symfony/proxy-manager-bridge": "5.2.*",
"symfony/yaml": "5.2.*"
},
"replace": {
@ -22,10 +27,13 @@
"codeception/module-asserts": "^1.3",
"codeception/module-phpbrowser": "^1.0",
"codeception/module-rest": "^1.2",
"doctrine/doctrine-fixtures-bundle": "^3.4",
"ergebnis/composer-normalize": "^2.13",
"friendsofphp/php-cs-fixer": "^2.18",
"fzaninotto/faker": "^1.9",
"php-parallel-lint/php-parallel-lint": "^1.2",
"roave/security-advisories": "dev-master"
"roave/security-advisories": "dev-master",
"symfony/maker-bundle": "^1.28"
},
"config": {
"apcu-autoloader": true,
@ -73,6 +81,12 @@
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"codecept": "codecept -c tests/codeception.yml",
"data-recreate": [
"bin/console doctrine:database:drop --if-exists --force",
"bin/console doctrine:database:create --if-not-exists",
"bin/console doctrine:migrations:migrate -n --allow-no-migration -q",
"bin/console doctrine:fixtures:load --append"
],
"json-cs-fix": "composer normalize",
"lint:php": [
"parallel-lint --no-progress --no-colors --blame ./bin/console",

2359
composer.lock
File diff suppressed because it is too large
View File

4
config/bundles.php

@ -2,4 +2,8 @@
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
];

14
config/packages/doctrine.yaml

@ -0,0 +1,14 @@
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App

3
config/packages/doctrine_migrations.yaml

@ -0,0 +1,3 @@
doctrine_migrations:
migrations_paths:
'DoctrineMigrations': '%kernel.project_dir%/migrations'

20
config/packages/prod/doctrine.yaml

@ -0,0 +1,20 @@
doctrine:
orm:
auto_generate_proxy_classes: false
metadata_cache_driver:
type: pool
pool: doctrine.system_cache_pool
query_cache_driver:
type: pool
pool: doctrine.system_cache_pool
result_cache_driver:
type: pool
pool: doctrine.result_cache_pool
framework:
cache:
pools:
doctrine.result_cache_pool:
adapter: cache.app
doctrine.system_cache_pool:
adapter: cache.system

4
devops/docker/.env

@ -1,5 +1,5 @@
COMPOSE_PROJECT_NAME=template
PROJECT_ID=001
COMPOSE_PROJECT_NAME=weather_history
PROJECT_ID=002
COMPOSE_DOCKER_CLI_BUILD=1
DOCKER_BUILDKIT=1

29
migrations/Version20210129121632.php

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20210129121632 extends AbstractMigration
{
public function up(Schema $schema) : void
{
$this->addSql('
CREATE TABLE history (
id INT AUTO_INCREMENT NOT NULL,
temp FLOAT NOT NULL,
date_at DATE NOT NULL, PRIMARY KEY(id)
)
DEFAULT CHARACTER SET utf8mb4
COLLATE `utf8mb4_unicode_ci`
ENGINE = InnoDB');
}
public function down(Schema $schema) : void
{
$this->addSql('DROP TABLE history');
}
}

35
src/DataFixtures/HistoryFixtures.php

@ -0,0 +1,35 @@
<?php
namespace App\DataFixtures;
use App\Entity\History;
use DateTime;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Faker\Factory;
class HistoryFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
$now = new DateTime();
$past = (new DateTime())->sub(new \DateInterval('P6M'));
$interval = $now->diff($past);
for ($i = (int) $interval->format('%a'); $i >= 0; --$i) {
$dayOffset = sprintf('P%dD', $i);
$history = new History();
$history
->setTemp(
(Factory::create('en_EN'))
->randomFloat(1, -50, 50)
)
->setDateAt((new DateTime())
->sub(new \DateInterval($dayOffset))
);
$manager->persist($history);
$manager->flush();
}
}
}

59
src/Entity/History.php

@ -0,0 +1,59 @@
<?php
namespace App\Entity;
use App\Repository\HistoryRepository;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=HistoryRepository::class)
*/
class History
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private ?int $id;
/**
* @ORM\Column(type="float")
*/
private ?float $temp;
/**
* @ORM\Column(type="date")
*/
private ?DateTimeInterface $date_at;
public function getId(): ?int
{
return $this->id;
}
public function getTemp(): ?float
{
return $this->temp;
}
public function setTemp(float $temp): self
{
$this->temp = $temp;
return $this;
}
public function getDateAt(): ?DateTimeInterface
{
return $this->date_at;
}
public function setDateAt(DateTimeInterface $date_at): self
{
$this->date_at = $date_at;
return $this;
}
}

21
src/Repository/HistoryRepository.php

@ -0,0 +1,21 @@
<?php
namespace App\Repository;
use App\Entity\History;
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
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, History::class);
}
}

109
symfony.lock

@ -50,6 +50,9 @@
"codeception/stub": {
"version": "3.7.0"
},
"composer/package-versions-deprecated": {
"version": "1.11.99.1"
},
"composer/semver": {
"version": "3.2.4"
},
@ -68,12 +71,85 @@
"config/routes/annotations.yaml"
]
},
"doctrine/cache": {
"version": "1.10.2"
},
"doctrine/collections": {
"version": "1.6.7"
},
"doctrine/common": {
"version": "3.1.1"
},
"doctrine/data-fixtures": {
"version": "1.5.0"
},
"doctrine/dbal": {
"version": "2.12.1"
},
"doctrine/doctrine-bundle": {
"version": "2.0",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "2.0",
"ref": "368794356c1fb634e58b38ad2addb36933f2e73e"
},
"files": [
"config/packages/doctrine.yaml",
"config/packages/prod/doctrine.yaml",
"src/Entity/.gitignore",
"src/Repository/.gitignore"
]
},
"doctrine/doctrine-fixtures-bundle": {
"version": "3.0",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "3.0",
"ref": "e5b542d4ef47d8a003c91beb35650c76907f7e53"
},
"files": [
"src/DataFixtures/AppFixtures.php"
]
},
"doctrine/doctrine-migrations-bundle": {
"version": "2.2",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "2.2",
"ref": "baaa439e3e3179e69e3da84b671f0a3e4a2f56ad"
},
"files": [
"config/packages/doctrine_migrations.yaml",
"migrations/.gitignore"
]
},
"doctrine/event-manager": {
"version": "1.1.1"
},
"doctrine/inflector": {
"version": "2.0.3"
},
"doctrine/instantiator": {
"version": "1.4.0"
},
"doctrine/lexer": {
"version": "1.2.1"
},
"doctrine/migrations": {
"version": "3.0.2"
},
"doctrine/orm": {
"version": "2.8.1"
},
"doctrine/persistence": {
"version": "2.1.0"
},
"doctrine/sql-formatter": {
"version": "1.1.1"
},
"ergebnis/composer-normalize": {
"version": "2.13.2"
},
@ -95,6 +171,12 @@
".php_cs.dist"
]
},
"friendsofphp/proxy-manager-lts": {
"version": "v1.0.3"
},
"fzaninotto/faker": {
"version": "v1.9.2"
},
"guzzlehttp/guzzle": {
"version": "7.2.0"
},
@ -107,6 +189,15 @@
"justinrainbow/json-schema": {
"version": "5.2.10"
},
"laminas/laminas-code": {
"version": "4.0.0"
},
"laminas/laminas-eventmanager": {
"version": "3.3.0"
},
"laminas/laminas-zendframework-bridge": {
"version": "1.1.1"
},
"localheinz/diff": {
"version": "1.1.1"
},
@ -277,6 +368,9 @@
"symfony/deprecation-contracts": {
"version": "v2.2.0"
},
"symfony/doctrine-bridge": {
"version": "v5.2.2"
},
"symfony/dom-crawler": {
"version": "v5.2.2"
},
@ -339,9 +433,21 @@
"symfony/http-kernel": {
"version": "v5.2.2"
},
"symfony/maker-bundle": {
"version": "1.0",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "1.0",
"ref": "fadbfe33303a76e25cb63401050439aa9b1a9c7f"
}
},
"symfony/options-resolver": {
"version": "v5.2.2"
},
"symfony/orm-pack": {
"version": "v2.1.0"
},
"symfony/polyfill-intl-grapheme": {
"version": "v1.22.0"
},
@ -363,6 +469,9 @@
"symfony/process": {
"version": "v5.2.2"
},
"symfony/proxy-manager-bridge": {
"version": "v5.2.2"
},
"symfony/routing": {
"version": "5.1",
"recipe": {

135
tests/unit/Entity/HistoryTest.php

@ -0,0 +1,135 @@
<?php
declare(strict_types=1);
namespace App\Tests\unit\Entity;
use App\Entity\History;
use App\Tests\UnitTester;
use Codeception\Test\Unit;
use DateTimeInterface;
use Faker\Factory;
use ReflectionException;
/**
* @internal
*/
class HistoryTest extends Unit
{
protected UnitTester $tester;
/**
* @dataProvider dataGetId
*
* @throws ReflectionException
* @test
*/
public function getId(?int $id): void
{
$entity = new History();
$property = $this->tester->getClassProperty($entity, 'id');
$property->setValue($entity, $id);
$result = $entity->getId();
self::assertEquals($id, $result);
}
public static function dataGetId(): array
{
$faker = Factory::create('en_EN');
return [
['id' => null],
['id' => $faker->randomNumber()],
];
}
/**
* @dataProvider dataGetTemp
*
* @throws ReflectionException
* @test
*/
public function getTemp(?float $temp): void
{
$entity = new History();
$property = $this->tester->getClassProperty($entity, 'temp');
$property->setValue($entity, $temp);
$result = $entity->getTemp();
self::assertEquals($temp, $result);
}
public static function dataGetTemp(): array
{
$faker = Factory::create('en_EN');
return [
['temp' => null],
['temp' => $faker->randomFloat()],
];
}
/**
* @throws ReflectionException
* @test
*/
public function setTemp(): void
{
$entity = new History();
$property = $this->tester->getClassProperty($entity, 'temp');
$temp = (Factory::create('en_EN'))->randomFloat();
$resultSet = $entity->setTemp($temp);
$result = $property->getValue($entity);
self::assertEquals($temp, $result);
self::assertEquals($entity, $resultSet);
}
/**
* @dataProvider dataGetDateAt
*
* @throws ReflectionException
* @test
*/
public function getDateAt(?DateTimeInterface $date_at): void
{
$entity = new History();
$property = $this->tester->getClassProperty($entity, 'date_at');
$property->setValue($entity, $date_at);
$result = $entity->getDateAt();
self::assertEquals($date_at, $result);
}
public static function dataGetDateAt(): array
{
$faker = Factory::create('en_EN');
return [
['date_at' => null],
['date_at' => $faker->dateTime()],
];
}
/**
* @throws ReflectionException
* @test
*/
public function setDateAt(): void
{
$entity = new History();
$property = $this->tester->getClassProperty($entity, 'date_at');
$date_at = (Factory::create('en_EN'))->dateTime();
$resultSet = $entity->setDateAt($date_at);
$result = $property->getValue($entity);
self::assertEquals($date_at, $result);
self::assertEquals($entity, $resultSet);
}
}