Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from chesslablab/issue/5-install-and-configure-…
Browse files Browse the repository at this point in the history
…Doctrine

Issue/5 install and configure doctrine
  • Loading branch information
programarivm authored Jan 11, 2022
2 parents 1581dce + a4e020c commit d286ea5
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 28 deletions.
22 changes: 7 additions & 15 deletions .env
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
# * .env contains default values for the environment variables needed by the app
# * .env.local uncommitted file with local overrides
# * .env.$APP_ENV committed environment-specific defaults
# * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
#
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
#
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=cf517d2e216f2c631fbd7be616bd7fce
###< symfony/framework-bundle ###

DB_DRIVER=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=chess
DB_USERNAME=root
DB_PASSWORD=
11 changes: 11 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=cf517d2e216f2c631fbd7be616bd7fce
###< symfony/framework-bundle ###

DB_DRIVER=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=chess
DB_USERNAME=root
DB_PASSWORD=
6 changes: 6 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# define your env variables for the test env here
KERNEL_CLASS='ChessApi\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@
/composer.lock
/symfony.lock
###< symfony/framework-bundle ###

###> phpunit/phpunit ###
/phpunit.xml
.phpunit.result.cache
###< phpunit/phpunit ###

.env
.env.test
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## Chess API

A chess REST API.

### Setup

Clone the `chesslablab/chess-api` repo into your projects folder as it is described in the following example:

$ git clone [email protected]:chesslablab/chess-api.git

Then `cd` the `chess-api` directory and install the Composer dependencies:

$ composer install

Create an `.env` file:

$ cp .env.example .env

If necessary, update the environment variables in your `.env` file.
2 changes: 1 addition & 1 deletion bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php

use App\Kernel;
use ChessApi\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;

if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
Expand Down
14 changes: 9 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
"ext-ctype": "*",
"ext-iconv": "*",
"chesslablab/php-chess": "^1.1",
"doctrine/annotations": "^1.13",
"composer/package-versions-deprecated": "1.11.99.4",
"friendsofsymfony/rest-bundle": "^3.2",
"symfony/console": "6.0.*",
"symfony/dotenv": "6.0.*",
"symfony/flex": "^2",
"symfony/framework-bundle": "6.0.*",
"symfony/proxy-manager-bridge": "6.0.*",
"symfony/runtime": "6.0.*",
"symfony/yaml": "6.0.*"
"symfony/yaml": "6.0.*",
"vlucas/phpdotenv": "^5.4"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"symfony/maker-bundle": "^1.36"
},
"config": {
Expand All @@ -34,12 +37,12 @@
},
"autoload": {
"psr-4": {
"App\\": "src/"
"ChessApi\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
"ChessApi\\Tests\\": "tests/"
}
},
"replace": {
Expand Down Expand Up @@ -68,7 +71,8 @@
"extra": {
"symfony": {
"allow-contrib": false,
"require": "6.0.*"
"require": "6.0.*",
"docker": false
}
}
}
4 changes: 2 additions & 2 deletions config/preload.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

if (file_exists(dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php')) {
require dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php';
if (file_exists(dirname(__DIR__).'/var/cache/prod/ChessApi_KernelProdContainer.preload.php')) {
require dirname(__DIR__).'/var/cache/prod/ChessApi_KernelProdContainer.preload.php';
}
2 changes: 1 addition & 1 deletion config/routes.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
api_play:
path: /api/play
controller: App\Controller\PlayController::index
controller: ChessApi\Controller\PlayController::index
methods: POST
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
ChessApi\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
Expand Down
38 changes: 38 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
convertDeprecationsToExceptions="false"
>
<php>
<ini name="display_errors" value="1" />
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
</php>

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>

<!-- Run `composer require symfony/panther` before enabling this extension -->
<!--
<extensions>
<extension class="Symfony\Component\Panther\ServerExtension" />
</extensions>
-->
</phpunit>
2 changes: 1 addition & 1 deletion public/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use App\Kernel;
use ChessApi\Kernel;

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/PlayController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Controller;
namespace ChessApi\Controller;

use Chess\Player;
use Chess\FEN\BoardToString;
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App;
namespace ChessApi;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
Expand Down
101 changes: 101 additions & 0 deletions src/Pdo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace ChessApi;

/**
* Pdo class.
*/
class Pdo
{
/**
* Pdo instance.
*
* @var \ChessApi\Pdo
*/
private static $instance;

/**
* DSN.
*
* @var string
*/
private $dsn;

/**
* PDO handler.
*
* @var \PDO
*/
private $pdo;

/**
* Returns the current instance.
*
* @param array $conf
* @return \ChessData\Pdo
*/
public static function getInstance(array $conf)
{
if (null === static::$instance) {
static::$instance = new static($conf);
}

return static::$instance;
}

/**
* Constructor.
*
* @param array $conf
*/
protected function __construct(array $conf)
{
$this->dsn = $conf['driver'] . ':host=' . $conf['host'] . ';dbname=' . $conf['database'];

$this->pdo = new \PDO(
$this->dsn,
$conf['username'],
$conf['password']
);

$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}

/**
* Prevents from cloning.
*/
private function __clone()
{
}

/**
* Prevents from unserializing.
*/
public function __wakeup()
{
}

/**
* Queries the database.
*
* @param string
* @param array
* @return bool
*/
public function query($sql, $values = [])
{
$stmt = $this->pdo->prepare($sql);

foreach ($values as $value) {
$stmt->bindValue(
$value['param'],
$value['value'],
$value['type'] ?? null
);
}

$stmt->execute();

return $stmt;
}
}
11 changes: 11 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Symfony\Component\Dotenv\Dotenv;

require dirname(__DIR__).'/vendor/autoload.php';

if (file_exists(dirname(__DIR__).'/config/bootstrap.php')) {
require dirname(__DIR__).'/config/bootstrap.php';
} elseif (method_exists(Dotenv::class, 'bootEnv')) {
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
}
36 changes: 36 additions & 0 deletions tests/integration/DatabaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace ChessApi\Tests\Integration;

use ChessApi\Pdo;
use PHPUnit\Framework\TestCase;

class DatabaseTest extends TestCase
{
public static $pdo;

public static function setUpBeforeClass(): void
{
$conf = [
'driver' => $_ENV['DB_DRIVER'],
'host' => $_ENV['DB_HOST'],
'database' => $_ENV['DB_DATABASE'],
'username' => $_ENV['DB_USERNAME'],
'password' => $_ENV['DB_PASSWORD'],
];

self::$pdo = Pdo::getInstance($conf);
}

/**
* @test
*/
public function count_all()
{
$sql = 'SELECT * FROM games';
$result = self::$pdo->query($sql)->fetchAll();
$expected = 402976;

$this->assertEquals($expected, count($result));
}
}

0 comments on commit d286ea5

Please sign in to comment.