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 document storage #163

Merged
merged 1 commit into from
Feb 25, 2024
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
13 changes: 12 additions & 1 deletion .github/workflows/code_checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,27 @@ jobs:
matrix:
php: ['8.2']
stability: [ prefer-stable ]
mongodb-version: ['7.0']
symfony-version: ['7.0.*']
include:
- php: '8.1'
mongodb-version: '7.0'
symfony-version: 5.4.*
stability: prefer-lowest
- php: '8.1'
mongodb-version: '7.0'
symfony-version: 5.4.*
stability: prefer-stable
- php: '8.1'
symfony-version: 6.0.*
mongodb-version: '7.0'
symfony-version: 6.4.*
stability: prefer-stable
- php: '8.2'
mongodb-version: '7.0'
symfony-version: 7.0.*
stability: prefer-stable
- php: '8.3'
mongodb-version: '7.0'
symfony-version: 7.0.*
stability: prefer-stable

Expand All @@ -46,6 +52,11 @@ jobs:
extensions: pcov, curl
coverage: pcov

- name: Start MongoDB
uses: supercharge/[email protected]
with:
mongodb-version: ${{ matrix.mongodb-version }}

- name: Install dependencies
env:
SYMFONY_REQUIRE: ${{ matrix.symfony-version }}
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,8 @@ $ratioList = $pairHistoryManager->getRatioHistory('USD', $startDate, $endDate);
RatioStorage
------------

Two storages for storing ratios are available : CSV File, or Doctrine
Three storages for storing ratios are available : CSV File (csv), Doctrine ORM (doctrine), or MongoDB (document)

By default, TbbcMoneyBundle is configured with CSV File.

If you want to switch to a Doctrine storage, edit your **config.yml**
Expand Down Expand Up @@ -573,18 +574,18 @@ You can :
* subclass the MoneyFormatter and rewrite the getDefaultNumberFormatter method to set a application wide
NumberFormatter

Using the TbbcMoneyBundle without Doctrine
------------------------------------------
Using the TbbcMoneyBundle without Doctrine ORM or MongoDB
---------------------------------------------------------

You have to disable the pair history service in order to use the TbbcMoneyBundle without Doctrine.
You have to disable the pair history service in order to use the TbbcMoneyBundle without Doctrine ORM or MongoDB.

```yaml
tbbc_money:
enable_pair_history: true
```

Note : you can imagine to code your own PairHistoryManager for MongoDB or Propel, it is very easy to do. Don't
hesitate to submit a PR with your code and your tests.
Note : you can imagine to code your own PairHistoryManager for Propel, it is very easy to do. Don't hesitate to
submit a PR with your code and your tests.

Optimizations
-------------
Expand Down
10 changes: 8 additions & 2 deletions Tests/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tbbc\MoneyBundle\Tests;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;
Expand All @@ -15,22 +16,27 @@ class AppKernel extends Kernel
{
use MicroKernelTrait;

public function __construct()
public function __construct(string $environment, bool $debug, protected array $configs = [])
{
parent::__construct('test', false);
parent::__construct($environment, $debug);
}

public function registerBundles(): iterable
{
return [
yield new FrameworkBundle(),
yield new DoctrineBundle(),
yield new DoctrineMongoDBBundle(),
yield new TbbcMoneyBundle(),
];
}

public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(__DIR__.'/config/config.yaml');

foreach ($this->configs as $config) {
$loader->load($config);
}
}
}
6 changes: 4 additions & 2 deletions Tests/DatabaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

trait DatabaseTrait
{
private static array $kernelOptions = [];

public function setupDatabase(): void
{
self::dropDatabase();
Expand All @@ -24,7 +26,7 @@ public function dropDatabase(): void

private static function createDatabase(): void
{
$kernel = static::createKernel();
$kernel = static::createKernel(self::$kernelOptions);
$kernel->boot();
$application = new Application($kernel);
$application->setAutoExit(false);
Expand All @@ -44,7 +46,7 @@ private static function createDatabase(): void

private static function doDropDatabase(): void
{
$kernel = static::createKernel();
$kernel = static::createKernel(self::$kernelOptions);
$kernel->boot();
$application = new Application($kernel);
$application->setAutoExit(false);
Expand Down
48 changes: 48 additions & 0 deletions Tests/Document/DocumentRatioHistoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Tbbc\MoneyBundle\Tests\Document;

use DateTime;
use PHPUnit\Framework\TestCase;
use Tbbc\MoneyBundle\Document\DocumentRatioHistory;

class DocumentRatioHistoryTest extends TestCase
{
public function testClassExists(): void
{
$this->assertTrue(class_exists(\Tbbc\MoneyBundle\Document\DocumentRatioHistory::class));
}

public function testProperties(): void
{
$ratioHistory = new DocumentRatioHistory();

self::assertNull($ratioHistory->getId());
$ratioHistory->setId('deadbeefdeadbeefdeadbeefdeadbeef');
self::assertSame('deadbeefdeadbeefdeadbeefdeadbeef', $ratioHistory->getId());

$ratioHistory->setCurrencyCode('USD');
self::assertSame('USD', $ratioHistory->getCurrencyCode());

$ratioHistory->setRatio(1.6);
self::assertSame(1.6, $ratioHistory->getRatio());

$ratioHistory->setReferenceCurrencyCode('code');
self::assertSame('code', $ratioHistory->getReferenceCurrencyCode());

$ratioHistory->setSavedAt(new DateTime('2012-01-01'));
self::assertSame('2012-01-01', $ratioHistory->getSavedAt()->format('Y-m-d'));

self::assertTrue(method_exists($ratioHistory, 'getId'));
self::assertTrue(method_exists($ratioHistory, 'getCurrencyCode'));
self::assertTrue(method_exists($ratioHistory, 'setCurrencyCode'));
self::assertTrue(method_exists($ratioHistory, 'getRatio'));
self::assertTrue(method_exists($ratioHistory, 'setRatio'));
self::assertTrue(method_exists($ratioHistory, 'setReferenceCurrencyCode'));
self::assertTrue(method_exists($ratioHistory, 'getReferenceCurrencyCode'));
self::assertTrue(method_exists($ratioHistory, 'setSavedAt'));
self::assertTrue(method_exists($ratioHistory, 'getSavedAt'));
}
}
42 changes: 42 additions & 0 deletions Tests/Document/DocumentStorageRatioTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Tbbc\MoneyBundle\Tests\Document;

use PHPUnit\Framework\TestCase;
use Tbbc\MoneyBundle\Document\DocumentStorageRatio;

class DocumentStorageRatioTest extends TestCase
{
public function testClassExists(): void
{
$this->assertTrue(class_exists(\Tbbc\MoneyBundle\Document\DocumentStorageRatio::class));
}

public function testConstructor(): void
{
$dollar = new DocumentStorageRatio('USD', 1.6);
self::assertSame('USD', $dollar->getCurrencyCode());
self::assertSame(1.6, $dollar->getRatio());
}

public function testProperties(): void
{
$currency = new DocumentStorageRatio();

self::assertNull($currency->getId());

$currency->setCurrencyCode('USD');
self::assertSame('USD', $currency->getCurrencyCode());

$currency->setRatio(1.6);
self::assertSame(1.6, $currency->getRatio());

self::assertTrue(method_exists($currency, 'getId'));
self::assertTrue(method_exists($currency, 'getCurrencyCode'));
self::assertTrue(method_exists($currency, 'setCurrencyCode'));
self::assertTrue(method_exists($currency, 'getRatio'));
self::assertTrue(method_exists($currency, 'setRatio'));
}
}
54 changes: 54 additions & 0 deletions Tests/DocumentDatabaseTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Tbbc\MoneyBundle\Tests;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

trait DocumentDatabaseTrait
{
private static array $kernelOptions = [];

public function setupDatabase(): void
{
self::dropDatabase();
self::createDatabase();
}

public function dropDatabase(): void
{
self::doDropDatabase();
}

private static function createDatabase(): void
{
$kernel = static::createKernel(self::$kernelOptions);
$kernel->boot();
$application = new Application($kernel);
$application->setAutoExit(false);

$code = $application->run(new ArrayInput([
'command' => 'doctrine:mongodb:schema:create',
'--quiet' => true,
]), new NullOutput());
self::assertSame(Command::SUCCESS, $code);
}

private static function doDropDatabase(): void
{
$kernel = static::createKernel(self::$kernelOptions);
$kernel->boot();
$application = new Application($kernel);
$application->setAutoExit(false);

$code = $application->run(new ArrayInput([
'command' => 'doctrine:mongodb:schema:drop',
'--quiet' => true,
]), new NullOutput());
self::assertSame(Command::SUCCESS, $code);
}
}
20 changes: 19 additions & 1 deletion Tests/Pair/Storage/DoctrineStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Persistence\ObjectManager;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpKernel\KernelInterface;
use Tbbc\MoneyBundle\Entity\DoctrineStorageRatio;
use Tbbc\MoneyBundle\Pair\Storage\DoctrineStorage;
use Tbbc\MoneyBundle\Tests\DatabaseTrait;
Expand All @@ -20,7 +21,13 @@ class DoctrineStorageTest extends KernelTestCase
public function setUp(): void
{
parent::setUp();
self::bootKernel();
self::$kernelOptions = [
'environment' => 'testDoctrine',
'configs' => [
__DIR__.'/../../config/doctrine.yaml'
],
];
self::bootKernel(self::$kernelOptions);
$this->entityManager = self::getContainer()->get('doctrine')->getManager();
$this->doctrineStorage = new DoctrineStorage($this->entityManager, 'USD');
self::createDatabase();
Expand All @@ -32,6 +39,17 @@ protected function tearDown(): void
$this->dropDatabase();
}

protected static function createKernel(array $options = []): KernelInterface
{
static::$class ??= static::getKernelClass();

$env = $options['environment'] ?? $_ENV['APP_ENV'] ?? $_SERVER['APP_ENV'] ?? 'test';
$debug = $options['debug'] ?? $_ENV['APP_DEBUG'] ?? $_SERVER['APP_DEBUG'] ?? true;
$configs = $options['configs'] ?? [];

return new static::$class($env, $debug, $configs);
}

public function testLoadDefaultCurrency(): void
{
$ratioList = $this->doctrineStorage->loadRatioList();
Expand Down
Loading
Loading