Skip to content

Load the profile after setting the config from the Input #137

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

Merged
merged 3 commits into from
Feb 4, 2015
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ php:
- 5.4

before_script:
- composer self-update
- composer require "symfony/symfony" "2.6.*" --no-update
- composer install
- bash tests/bin/travis_jackrabbit.sh
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dev-master
### Bug fixes

- [embedded] No exit code returned
- [profile] Profile configuration overwritten by session parameters

beta1
-----
Expand Down
5 changes: 5 additions & 0 deletions behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ default:
- PHPCR\Shell\Test\EmbeddedContext
paths:
- features/all
cli:
contexts:
- PHPCR\Shell\Test\CliContext
paths:
- features/cli
4 changes: 2 additions & 2 deletions features/all/phpcr_node_edit.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature: Edit a node
Given that I am logged in as "testuser"
And the "cms.xml" fixtures are loaded

Scenario: Make a nutral edit
Scenario: Make a neutral edit
Given I have an editor which produces the following:
""""
weight:
Expand Down Expand Up @@ -91,7 +91,7 @@ Feature: Edit a node
And I save the session
Then the command should not fail
And the property "/cms/products/product1/weight" should have type "Long" and value "10"
And the property "/cms/products/product1/cost" should have type "Long" and value "100"
And the property "/cms/products/product1/cost" should have type "Double" and value "100"
And the property "/cms/products/product1/size" should have type "String" and value "XXL"
And the property "/cms/products/product1/name" should have type "String" and value "Product One"
And the property "/cms/products/product1/jcr:primaryType" should have type "Name" and value "nt:unstructured"
Expand Down
31 changes: 31 additions & 0 deletions features/cli/doctrine-dbal.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Feature: Connect to a doctrine dbal repository
In order to use the jackalope doctrine-dbal repository
As a user
I need to be able to connect to it

Background:
Given I initialize doctrine dbal

Scenario: Connect to doctrine-dbal session
Given I run PHPCR shell with "--transport=doctrine-dbal --db-driver=pdo_sqlite --db-path=./app.sqlite --command='ls'"
Then the command should not fail

Scenario: Connect to doctrine-dbal session create a new profile
Given I run PHPCR shell with "--transport=doctrine-dbal --db-driver=pdo_sqlite --db-path=./app.sqlite --profile=new --no-interaction --command='ls'"
Then the command should not fail

Scenario: Connect to an existing profile
Given the following profile "phpcrtest" exists:
"""
transport:
name: doctrine-dbal
db_name: phpcrtest
db_path: app.sqlite
db_driver: pdo_sqlite
phpcr:
workspace: default
username: admin
password: admin
"""
And I run PHPCR shell with "--profile=phpcrtest --no-interaction --command='ls'"
Then the command should not fail
39 changes: 39 additions & 0 deletions features/fixtures/jackalope-doctrine-dbal-cli-config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
$dbConn = \Doctrine\DBAL\DriverManager::getConnection(array(
'driver' => 'pdo_sqlite',
'dbname' => 'test',
'path' => __DIR__.'/app.sqlite',
));

/*
* configuration
*/
$workspace = 'default'; // phpcr workspace to use
$user = 'admin';
$pass = 'admin';

$factory = new \Jackalope\RepositoryFactoryDoctrineDBAL();
$repository = $factory->getRepository(array('jackalope.doctrine_dbal_connection' => $dbConn));

$credentials = new \PHPCR\SimpleCredentials($user, $pass);

/* only create a session if this is not about the server control command */
if (isset($argv[1])
&& $argv[1] != 'jackalope:init:dbal'
&& $argv[1] != 'list'
&& $argv[1] != 'help'
) {
$session = $repository->login($credentials, $workspace);

$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'dialog' => new \Symfony\Component\Console\Helper\DialogHelper(),
'phpcr' => new \PHPCR\Util\Console\Helper\PhpcrHelper($session),
'phpcr_console_dumper' => new \PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper(),
));
} else if (isset($argv[1]) && $argv[1] == 'jackalope:init:dbal') {
// special case: the init command needs the db connection, but a session is impossible if the db is not yet initialized
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'connection' => new \Jackalope\Tools\Console\Helper\DoctrineDbalHelper($dbConn)
));
}

5 changes: 2 additions & 3 deletions spec/PHPCR/Shell/Config/ProfileSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace spec\PHPCR\Shell\Config;

use PhpSpec\ObjectBehavior;
use PHPCR\Shell\Config\Config;

class ProfileSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -39,9 +40,7 @@ public function it_has_a_method_to_get_config()
'foo' => 'bar'
));

$this->get('transport')->shouldReturn(array(
'foo' => 'bar'
));
$this->get('transport')->shouldHaveType('PHPCR\Shell\Config\Config');

$this->get('transport', 'foo')->shouldReturn('bar');
}
Expand Down
2 changes: 1 addition & 1 deletion src/PHPCR/Shell/Config/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function get($domain, $key = null)
$this->validateDomain($domain);

if (null === $key) {
return $this->profile[$domain];
return new Config($this->profile[$domain]);
}

if (!isset($this->profile[$domain][$key])) {
Expand Down
10 changes: 5 additions & 5 deletions src/PHPCR/Shell/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public function registerPhpcr()
public function registerEvent()
{
if ($this->mode === PhpcrShell::MODE_STANDALONE) {
$this->register(
'event.subscriber.profile_from_session_input',
'PHPCR\Shell\Subscriber\ProfileFromSessionInputSubscriber'
)->addTag('event.subscriber');

$this->register(
'event.subscriber.profile_loader',
'PHPCR\Shell\Subscriber\ProfileLoaderSubscriber'
Expand All @@ -112,11 +117,6 @@ public function registerEvent()
->addArgument(new Reference('helper.question'))
->addTag('event.subscriber');

$this->register(
'event.subscriber.profile_from_session_input',
'PHPCR\Shell\Subscriber\ProfileFromSessionInputSubscriber'
)->addTag('event.subscriber');

$this->register(
'event.subscriber.profile_writer',
'PHPCR\Shell\Subscriber\ProfileWriterSubscriber'
Expand Down
12 changes: 10 additions & 2 deletions src/PHPCR/Shell/Subscriber/ProfileWriterSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function handleProfileInit(ProfileInitEvent $e)
$profile = $e->getProfile();
$input = $e->getInput();
$output = $e->getOutput();
$noInteraction = $input->getOption('no-interaction');
$transport = $input->getOption('transport');
$profileName = $input->getOption('profile');

Expand All @@ -50,10 +51,17 @@ public function handleProfileInit(ProfileInitEvent $e)
$overwrite = false;

if (file_exists($this->profileLoader->getProfilePath($profileName))) {
$res = $this->questionHelper->askConfirmation($output, sprintf('Update existing profile "%s"?', $profileName));
$res = true;
if (false === $noInteraction) {
$res = $this->questionHelper->askConfirmation($output, sprintf('Update existing profile "%s"?', $profileName));
}
$overwrite = true;
} else {
$res = $this->questionHelper->askConfirmation($output, sprintf('Create new profile "%s"?', $profileName));
$res = true;

if (false === $noInteraction) {
$res = $this->questionHelper->askConfirmation($output, sprintf('Create new profile "%s"?', $profileName));
}
}

if ($res) {
Expand Down
125 changes: 125 additions & 0 deletions src/PHPCR/Shell/Test/CliContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

/*
* This file is part of the PHPCR Shell package
*
* (c) Daniel Leech <daniel@dantleech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PHPCR\Shell\Test;

use Jackalope\RepositoryFactoryJackrabbit;
use PHPCR\SimpleCredentials;
use PHPCR\Util\NodeHelper;
use Symfony\Component\Filesystem\Filesystem;
use PHPCR\PathNotFoundException;
use PHPCR\Util\PathHelper;
use PHPCR\PropertyInterface;
use PHPCR\PropertyType;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Context\Context;
use PHPCR\NodeInterface;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

/**
* Features context.
*/
class CliContext implements Context, SnippetAcceptingContext
{
private $output = array();
private $rootPath;
private $lastExitCode;
private $fixturesDir;

/**
* Initializes context.
* Every scenario gets it's own context object.
*
* @BeforeScenario
*/
public function beforeScenario()
{
$this->output = array();

$this->rootPath = realpath(__DIR__ . '/../../../..');
$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'phpcr-shell' . DIRECTORY_SEPARATOR .
md5(microtime() * rand(0, 10000));
$this->fixturesDir = realpath(__DIR__.'/../../../../features/fixtures/');

$this->workingDir = $dir;
putenv('PHPCRSH_HOME=' . $dir);

mkdir($this->workingDir, 0777, true);
mkdir($this->workingDir . '/profiles');
chdir($this->workingDir);
$this->filesystem = new Filesystem();
}

/**
* Cleans test folders in the temporary directory.
*
* @AfterSuite
*/
public static function cleanTestFolders()
{
$fs = new Filesystem();
$fs->remove(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'phpcr-shell');
}

private function exec($command)
{
exec($command, $output, $return);
$this->output += $output;
$this->lastExitCode = $return;
}

/**
* @Given I run PHPCR shell with ":argsAndOptions"
*/
public function execShell($argsAndOptions)
{
$this->exec(sprintf('%s/bin/phpcrsh %s', $this->rootPath, $argsAndOptions));
}

/**
* @Given print output
*/
public function printOutput()
{
foreach ($this->output as $line) {
echo $line . PHP_EOL;
}
}

/**
* @Given the following profile ":profileName" exists:
*/
public function iHaveTheFollowingProfile($profileName, PyStringNode $text)
{
file_put_contents($this->workingDir . '/profiles/' . $profileName . '.yml', $text->getRaw());
}

/**
* @Given I initialize doctrine dbal
*/
public function initializeDoctrineDbal()
{
$this->filesystem->copy(
$this->fixturesDir . '/jackalope-doctrine-dbal-cli-config.php',
$this->workingDir . '/cli-config.php'
);
$this->exec($this->rootPath . '/vendor/jackalope/jackalope-doctrine-dbal/bin/jackalope jackalope:init:dbal --force');
}

/**
* @Then the command should not fail
*/
public function theCommandShouldNotFail()
{
\PHPUnit_Framework_Assert::assertEquals(0, $this->lastExitCode);
}
}
3 changes: 2 additions & 1 deletion src/PHPCR/Shell/Transport/Transport/DoctrineDbal.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\DBAL\DriverManager;
use Jackalope\RepositoryFactoryDoctrineDBAL;
use PHPCR\Shell\Transport\TransportInterface;
use PHPCR\Shell\Config\Config;

class DoctrineDbal implements TransportInterface
{
Expand All @@ -22,7 +23,7 @@ public function getName()
return 'doctrine-dbal';
}

public function getRepository(array $config)
public function getRepository(Config $config)
{
$connection = DriverManager::getConnection($ops = array(
'user' => $config['db_username'],
Expand Down
3 changes: 2 additions & 1 deletion src/PHPCR/Shell/Transport/Transport/JackalopeFs.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPCR\Shell\Transport\TransportInterface;
use Jackalope\RepositoryFactoryFilesystem;
use PHPCR\Shell\Config\Config;

class JackalopeFs implements TransportInterface
{
Expand All @@ -21,7 +22,7 @@ public function getName()
return 'jackalope-fs';
}

public function getRepository(array $config)
public function getRepository(Config $config)
{
$params = array(
'path' => $config['repo_path'],
Expand Down
3 changes: 2 additions & 1 deletion src/PHPCR/Shell/Transport/Transport/Jackrabbit.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Jackalope\RepositoryFactoryJackrabbit;
use PHPCR\Shell\Transport\TransportInterface;
use PHPCR\Shell\Config\Config;

class Jackrabbit implements TransportInterface
{
Expand All @@ -21,7 +22,7 @@ public function getName()
return 'jackrabbit';
}

public function getRepository(array $config)
public function getRepository(Config $config)
{
$params = array(
'jackalope.jackrabbit_uri' => $config['repo_url'],
Expand Down
4 changes: 3 additions & 1 deletion src/PHPCR/Shell/Transport/TransportInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace PHPCR\Shell\Transport;

use PHPCR\Shell\Config\Config;

/**
* All phpcr-shell transports must implement this interface
*
Expand All @@ -20,5 +22,5 @@ interface TransportInterface
{
public function getName();

public function getRepository(array $config);
public function getRepository(Config $config);
}