Skip to content

Commit

Permalink
Remove deprecations (#74)
Browse files Browse the repository at this point in the history
* Remove deprecations due to private service fetching in tests

* Remove Twig deprecations

* Ignore var folder if created in root (due to SF4)

* Fix Configuration root node deprecation

* Remove deprecations from commands

* Always check deprecations in CI (partial revert of 9d34e94)

* Fix commands service definition; add tests
  • Loading branch information
Jean85 authored and ilario-pierbattista committed May 27, 2019
1 parent 9d34e94 commit 5c91c81
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/composer.lock
/tests/Functional/TestApp/cache
/tests/Functional/TestApp/logs
/var/
/vendor/
/bin/
/.php_cs.cache
Expand Down
9 changes: 2 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ env:
- PHP_IMG="base-7.0-mongoext-1.3.0" MONGO_IMG="mongo:3.2" SYMFONY="3.4.*"
- PHP_IMG="base-7.0-mongoext-1.3.0" MONGO_IMG="mongo:3.4" TEST_COVERAGE=1
- PHP_IMG="base-7.1-mongoext-1.3.0" MONGO_IMG="mongo:3.2" SYMFONY="4.*"
- PHP_IMG="base-7.1-mongoext-1.3.0" MONGO_IMG="mongo:3.2" SYMFONY="4.*" DEPRECATIONS=1
- PHP_IMG="base-7.1-mongoext-1.3.0" MONGO_IMG="mongo:3.4" SYMFONY="dev-master"
- PHP_IMG="base-7.2-mongoext-1.3.0" MONGO_IMG="mongo:3.2"
- PHP_IMG="base-7.2-mongoext-1.3.0" MONGO_IMG="mongo:3.4"
Expand Down Expand Up @@ -51,8 +50,6 @@ matrix:
fast_finish: true
allow_failures:
- env: PHP_IMG="base-7.1-mongoext-1.3.0" MONGO_IMG="mongo:3.4" SYMFONY="dev-master"
- env: PHP_IMG="base-7.1-mongoext-1.3.0" MONGO_IMG="mongo:3.2" SYMFONY="4.*" DEPRECATIONS=1


install:
- rm docker-compose.yml
Expand All @@ -71,11 +68,9 @@ script:
- docker-compose run --no-deps --rm php-cli composer validate
- |
if [[ "$TEST_COVERAGE" = "1" ]]; then
docker-compose run --rm php-cli bash -c "sleep 3; SYMFONY_DEPRECATIONS_HELPER=\"disabled\" bin/phpunit -c phpunit.xml.dist --coverage-clover=./build/coverage/coverage.clover"
elif [[ "$DEPRECATIONS" = "1" ]]; then
docker-compose run --rm php-cli bash -c "sleep 3; bin/phpunit -c phpunit.xml.dist"
docker-compose run --rm php-cli bash -c "sleep 3; bin/phpunit -c phpunit.xml.dist --coverage-clover=./build/coverage/coverage.clover"
else
docker-compose run --rm php-cli bash -c "sleep 3; SYMFONY_DEPRECATIONS_HELPER=\"disabled\" bin/phpunit -c phpunit.xml.dist"
docker-compose run --rm php-cli bash -c "sleep 3; bin/phpunit -c phpunit.xml.dist"
fi
after_success:
Expand Down
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,4 @@ stop: docker-compose.yml
docker-compose stop

test: docker-compose.yml phpunit.xml.dist
docker-compose run --rm php-cli bash -c "SYMFONY_DEPRECATIONS_HELPER=\"disabled\" bin/phpunit -c phpunit.xml.dist"

test-with-deprecations: docker-compose.yml phpunit.xml.dist
docker-compose run --rm php-cli bash -c "bin/phpunit -c phpunit.xml.dist"
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"symfony/web-profiler-bundle": "^2.8 || ^3.0 || ^4.0",
"symfony/console": "^2.8 || ^3.0 || ^4.0",
"phpunit/phpunit": "^6.0",
"symfony/phpunit-bridge": "*"
"symfony/phpunit-bridge": "^4.2"
},
"minimum-stability": "stable",
"suggest": {
Expand Down
28 changes: 24 additions & 4 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,42 @@
namespace Facile\MongoDbBundle\Command;

use MongoDB\Database as Connection;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Class AbstractCommand.
*/
abstract class AbstractCommand extends ContainerAwareCommand
abstract class AbstractCommand extends Command
{
/** @var SymfonyStyle */
protected $io;

/** @var Connection */
protected $connection;

/** @var ContainerInterface */
private $container;

/**
* AbstractCommand constructor.
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container, string $name = null)
{
parent::__construct($name);
$this->container = $container;
}

protected function getContainer(): ContainerInterface
{
return $this->container;
}

/**
* {@inheritdoc}
*/
Expand All @@ -43,10 +63,10 @@ protected function initialize(InputInterface $input, OutputInterface $output)
$connectionName .= '.' . $input->getOption('connection');
}

if (! $this->getContainer()->has($connectionName)) {
if (! $this->container->has($connectionName)) {
throw new \LogicException(sprintf('No connection named \'%s\' found', $input->getOption('connection')));
}

$this->connection = $this->getContainer()->get($connectionName);
$this->connection = $this->container->get($connectionName);
}
}
15 changes: 9 additions & 6 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Facile\MongoDbBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand All @@ -20,12 +21,14 @@ final class Configuration implements ConfigurationInterface
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootBuilder = $treeBuilder->root('mongo_db_bundle')->children();

$this->addDataCollection($rootBuilder);
$this->addClients($rootBuilder);
$this->addConnections($rootBuilder);
$treeBuilder = new TreeBuilder('mongo_db_bundle');
$rootBuilder = \method_exists(TreeBuilder::class, 'getRootNode')
? $treeBuilder->getRootNode()
: $treeBuilder->root('mongo_db_bundle');

$this->addDataCollection($rootBuilder->children());
$this->addClients($rootBuilder->children());
$this->addConnections($rootBuilder->children());

return $treeBuilder;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
<!-- commands -->

<service id="facile_mongo_db.command.drop_database" class="Facile\MongoDbBundle\Command\DropDatabaseCommand">
<argument type="service" id="service_container" />
<tag name="console.command"/>
</service>

<service id="facile_mongo_db.command.drop_collection"
class="Facile\MongoDbBundle\Command\DropCollectionCommand">
<service id="facile_mongo_db.command.drop_collection" class="Facile\MongoDbBundle\Command\DropCollectionCommand">
<argument type="service" id="service_container" />
<tag name="console.command"/>
</service>

<service id="facile_mongo_db.command.load_fixtures" class="Facile\MongoDbBundle\Command\LoadFixturesCommand">
<argument type="service" id="service_container" />
<tag name="console.command"/>
</service>

</services>
</container>
</container>
20 changes: 15 additions & 5 deletions src/Twig/FacileMongoDbBundleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
namespace Facile\MongoDbBundle\Twig;

use Facile\MongoDbBundle\Services\Explain\ExplainQueryService;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class FacileMongoDbBundleExtension extends \Twig_Extension
if (! class_exists('\Twig\Extension\AbstractExtension')) {
class_alias(\Twig_Extension::class, '\Twig\Extension\AbstractExtension');
}

if (! class_exists('\Twig\TwigFunction')) {
class_alias(\Twig_Function::class, '\Twig\TwigFunction');
}

class FacileMongoDbBundleExtension extends AbstractExtension
{
private $methodDataTranslationMap = [
'aggregate' => 'Pipeline',
Expand All @@ -20,9 +30,9 @@ class FacileMongoDbBundleExtension extends \Twig_Extension
public function getFunctions()
{
return [
new \Twig_Simplefunction('filterLabelTranslate', array($this, 'queryFilterTranslate')),
new \Twig_Simplefunction('dataLabelTranslate', array($this, 'queryDataTranslate')),
new \Twig_Simplefunction('isQueryExplainable', array($this, 'isQueryExplainable')),
new TwigFunction('filterLabelTranslate', array($this, 'queryFilterTranslate')),
new TwigFunction('dataLabelTranslate', array($this, 'queryDataTranslate')),
new TwigFunction('isQueryExplainable', array($this, 'isQueryExplainable')),
];
}

Expand Down Expand Up @@ -62,4 +72,4 @@ public function getName()
{
return 'facile_mongo_db_extesion';
}
}
}
21 changes: 13 additions & 8 deletions tests/Functional/Command/AbstractCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,26 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;

class AbstractAppTest extends AppTestCase
class AbstractCommandTest extends AppTestCase
{
/**
* @dataProvider commandOptionsProvider
*/
public function test_AbstractCommand_execution(array $arguments)
{

$this->getApplication()->add(new FakeCommand());
$this->addCommandToApplication();

$command = $this->getApplication()->find('mongodb:fake:command');

$commandTester = new CommandTester($command);
$commandTester->execute(array_merge(['command' => $command->getName()], $arguments));

self:
self::assertContains('Executed', $commandTester->getDisplay());
}

public function test_AbstractCommand_connection_exception()
{

$this->getApplication()->add(new FakeCommand());
$this->addCommandToApplication();

$command = $this->getApplication()->find('mongodb:fake:command');

Expand All @@ -53,11 +50,19 @@ public function commandOptionsProvider()
[['--connection' => 'test_db']],
];
}

private function addCommandToApplication()
{
$container = $this->getApplication()
->getKernel()
->getContainer();

$this->getApplication()->add(new FakeCommand($container));
}
}

class FakeCommand extends AbstractCommand
{

/**
* {@inheritdoc}
*/
Expand All @@ -73,4 +78,4 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->io->writeln('Executed');
}
}
}
15 changes: 11 additions & 4 deletions tests/Functional/Command/DropCollectionCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use MongoDB\Database;
use Symfony\Component\Console\Tester\CommandTester;

class DropCollectionAppTest extends AppTestCase
class DropCollectionCommandTest extends AppTestCase
{
public function test_command()
{
Expand All @@ -20,15 +20,22 @@ public function test_command()
$conn->dropCollection('testFunctionalCollection');
$conn->createCollection('testFunctionalCollection');

$this->getApplication()->add(new DropCollectionCommand());
$this->addCommandToApplication();

$command = $this->getApplication()->find('mongo:collection:drop');

$commandTester = new CommandTester($command);
$commandTester->execute(['command' => $command->getName(), 'collection' => 'testFunctionalCollection']);

self:
self::assertContains('Collection dropped', $commandTester->getDisplay());
}

}
private function addCommandToApplication()
{
$container = $this->getApplication()
->getKernel()
->getContainer();

$this->getApplication()->add(new DropCollectionCommand($container));
}
}
16 changes: 12 additions & 4 deletions tests/Functional/Command/DropDatabaseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use MongoDB\Database;
use Symfony\Component\Console\Tester\CommandTester;

class DropDatabaseAppTest extends AppTestCase
class DropDatabaseCommandTest extends AppTestCase
{
public function test_command()
{
Expand All @@ -19,14 +19,22 @@ public function test_command()

$conn->createCollection('testFunctionalCollection');

$this->getApplication()->add(new DropDatabaseCommand());
$this->addCommandToApplication();

$command = $this->getApplication()->find('mongo:database:drop');

$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName()));

self:
self::assertContains('Database dropped', $commandTester->getDisplay());
}
}

private function addCommandToApplication()
{
$container = $this->getApplication()
->getKernel()
->getContainer();

$this->getApplication()->add(new DropDatabaseCommand($container));
}
}
15 changes: 12 additions & 3 deletions tests/Functional/Command/LoadFixturesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function test_command()
{
$this->conn->createCollection('testFixturesCollection');

$this->getApplication()->add(new LoadFixturesCommand());
$this->addCommandToApplication();
$command = $this->getApplication()->find('mongodb:fixtures:load');
$commandTester = new CommandTester($command);
$commandTester->execute(
Expand All @@ -55,7 +55,7 @@ public function test_command()

public function test_command_not_fixtures_found()
{
$this->getApplication()->add(new LoadFixturesCommand());
$this->addCommandToApplication();
$command = $this->getApplication()->find('mongodb:fixtures:load');
$commandTester = new CommandTester($command);

Expand All @@ -67,7 +67,7 @@ public function test_command_order_fixtures()
{
$this->conn->createCollection('testFixturesOrderedCollection');

$this->getApplication()->add(new LoadFixturesCommand());
$this->addCommandToApplication();
$command = $this->getApplication()->find('mongodb:fixtures:load');
$commandTester = new CommandTester($command);
$commandTester->execute(
Expand Down Expand Up @@ -105,4 +105,13 @@ public function test_command_order_fixtures()
$this->conn->dropCollection('testFixturesOrderedCollection');
}


private function addCommandToApplication()
{
$container = $this->getApplication()
->getKernel()
->getContainer();

$this->getApplication()->add(new LoadFixturesCommand($container));
}
}
Loading

0 comments on commit 5c91c81

Please sign in to comment.