Skip to content

Commit 29ca2d6

Browse files
committed
Added doctrine-dbal connection tests
1 parent 4c8c649 commit 29ca2d6

File tree

7 files changed

+214
-7
lines changed

7 files changed

+214
-7
lines changed

behat.yml

+5
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ default:
1414
- PHPCR\Shell\Test\EmbeddedContext
1515
paths:
1616
- features/all
17+
cli:
18+
contexts:
19+
- PHPCR\Shell\Test\CliContext
20+
paths:
21+
- features/cli

features/all/phpcr_node_edit.feature

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Feature: Edit a node
77
Given that I am logged in as "testuser"
88
And the "cms.xml" fixtures are loaded
99

10-
Scenario: Make a nutral edit
10+
Scenario: Make a neutral edit
1111
Given I have an editor which produces the following:
1212
""""
1313
weight:
@@ -91,7 +91,7 @@ Feature: Edit a node
9191
And I save the session
9292
Then the command should not fail
9393
And the property "/cms/products/product1/weight" should have type "Long" and value "10"
94-
And the property "/cms/products/product1/cost" should have type "Long" and value "100"
94+
And the property "/cms/products/product1/cost" should have type "Double" and value "100"
9595
And the property "/cms/products/product1/size" should have type "String" and value "XXL"
9696
And the property "/cms/products/product1/name" should have type "String" and value "Product One"
9797
And the property "/cms/products/product1/jcr:primaryType" should have type "Name" and value "nt:unstructured"

features/cli/doctrine-dbal.feature

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Feature: Connect to a doctrine dbal repository
2+
In order to use the jackalope doctrine-dbal repository
3+
As a user
4+
I need to be able to connect to it
5+
6+
Background:
7+
Given I initialize doctrine dbal
8+
9+
Scenario: Connect to doctrine-dbal session
10+
Given I run PHPCR shell with "--transport=doctrine-dbal --db-driver=pdo_sqlite --db-path=./app.sqlite --command='ls'"
11+
Then the command should not fail
12+
13+
Scenario: Connect to doctrine-dbal session create a new profile
14+
Given I run PHPCR shell with "--transport=doctrine-dbal --db-driver=pdo_sqlite --db-path=./app.sqlite --profile=new --no-interaction --command='ls'"
15+
Then the command should not fail
16+
17+
Scenario: Connect to an existing profile
18+
Given the following profile "phpcrtest" exists:
19+
"""
20+
transport:
21+
name: doctrine-dbal
22+
db_name: phpcrtest
23+
db_path: app.sqlite
24+
db_driver: pdo_sqlite
25+
phpcr:
26+
workspace: default
27+
username: admin
28+
password: admin
29+
"""
30+
And I run PHPCR shell with "--profile=phpcrtest --no-interaction --command='ls'"
31+
Then the command should not fail
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
$dbConn = \Doctrine\DBAL\DriverManager::getConnection(array(
3+
'driver' => 'pdo_sqlite',
4+
'dbname' => 'test',
5+
'path' => __DIR__.'/app.sqlite',
6+
));
7+
8+
/*
9+
* configuration
10+
*/
11+
$workspace = 'default'; // phpcr workspace to use
12+
$user = 'admin';
13+
$pass = 'admin';
14+
15+
$factory = new \Jackalope\RepositoryFactoryDoctrineDBAL();
16+
$repository = $factory->getRepository(array('jackalope.doctrine_dbal_connection' => $dbConn));
17+
18+
$credentials = new \PHPCR\SimpleCredentials($user, $pass);
19+
20+
/* only create a session if this is not about the server control command */
21+
if (isset($argv[1])
22+
&& $argv[1] != 'jackalope:init:dbal'
23+
&& $argv[1] != 'list'
24+
&& $argv[1] != 'help'
25+
) {
26+
$session = $repository->login($credentials, $workspace);
27+
28+
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
29+
'dialog' => new \Symfony\Component\Console\Helper\DialogHelper(),
30+
'phpcr' => new \PHPCR\Util\Console\Helper\PhpcrHelper($session),
31+
'phpcr_console_dumper' => new \PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper(),
32+
));
33+
} else if (isset($argv[1]) && $argv[1] == 'jackalope:init:dbal') {
34+
// special case: the init command needs the db connection, but a session is impossible if the db is not yet initialized
35+
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
36+
'connection' => new \Jackalope\Tools\Console\Helper\DoctrineDbalHelper($dbConn)
37+
));
38+
}
39+

spec/PHPCR/Shell/Config/ProfileSpec.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace spec\PHPCR\Shell\Config;
1313

1414
use PhpSpec\ObjectBehavior;
15+
use PHPCR\Shell\Config\Config;
1516

1617
class ProfileSpec extends ObjectBehavior
1718
{
@@ -39,9 +40,7 @@ public function it_has_a_method_to_get_config()
3940
'foo' => 'bar'
4041
));
4142

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

4645
$this->get('transport', 'foo')->shouldReturn('bar');
4746
}

src/PHPCR/Shell/Subscriber/ProfileWriterSubscriber.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function handleProfileInit(ProfileInitEvent $e)
4040
$profile = $e->getProfile();
4141
$input = $e->getInput();
4242
$output = $e->getOutput();
43+
$noInteraction = $input->getOption('no-interaction');
4344
$transport = $input->getOption('transport');
4445
$profileName = $input->getOption('profile');
4546

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

5253
if (file_exists($this->profileLoader->getProfilePath($profileName))) {
53-
$res = $this->questionHelper->askConfirmation($output, sprintf('Update existing profile "%s"?', $profileName));
54+
$res = true;
55+
if (false === $noInteraction) {
56+
$res = $this->questionHelper->askConfirmation($output, sprintf('Update existing profile "%s"?', $profileName));
57+
}
5458
$overwrite = true;
5559
} else {
56-
$res = $this->questionHelper->askConfirmation($output, sprintf('Create new profile "%s"?', $profileName));
60+
$res = true;
61+
62+
if (false === $noInteraction) {
63+
$res = $this->questionHelper->askConfirmation($output, sprintf('Create new profile "%s"?', $profileName));
64+
}
5765
}
5866

5967
if ($res) {

src/PHPCR/Shell/Test/CliContext.php

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHPCR Shell package
5+
*
6+
* (c) Daniel Leech <daniel@dantleech.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace PHPCR\Shell\Test;
13+
14+
use Jackalope\RepositoryFactoryJackrabbit;
15+
use PHPCR\SimpleCredentials;
16+
use PHPCR\Util\NodeHelper;
17+
use Symfony\Component\Filesystem\Filesystem;
18+
use PHPCR\PathNotFoundException;
19+
use PHPCR\Util\PathHelper;
20+
use PHPCR\PropertyInterface;
21+
use PHPCR\PropertyType;
22+
use Behat\Behat\Context\SnippetAcceptingContext;
23+
use Behat\Behat\Context\Context;
24+
use PHPCR\NodeInterface;
25+
use Behat\Gherkin\Node\PyStringNode;
26+
use Behat\Gherkin\Node\TableNode;
27+
28+
/**
29+
* Features context.
30+
*/
31+
class CliContext implements Context, SnippetAcceptingContext
32+
{
33+
private $output = array();
34+
private $rootPath;
35+
private $lastExitCode;
36+
private $fixturesDir;
37+
38+
/**
39+
* Initializes context.
40+
* Every scenario gets it's own context object.
41+
*
42+
* @BeforeScenario
43+
*/
44+
public function beforeScenario()
45+
{
46+
$this->output = array();
47+
48+
$this->rootPath = realpath(__DIR__ . '/../../../..');
49+
$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'phpcr-shell' . DIRECTORY_SEPARATOR .
50+
md5(microtime() * rand(0, 10000));
51+
$this->fixturesDir = realpath(__DIR__.'/../../../../features/fixtures/');
52+
53+
$this->workingDir = $dir;
54+
putenv('PHPCRSH_HOME=' . $dir);
55+
56+
mkdir($this->workingDir, 0777, true);
57+
mkdir($this->workingDir . '/profiles');
58+
chdir($this->workingDir);
59+
$this->filesystem = new Filesystem();
60+
}
61+
62+
/**
63+
* Cleans test folders in the temporary directory.
64+
*
65+
* @AfterSuite
66+
*/
67+
public static function cleanTestFolders()
68+
{
69+
$fs = new Filesystem();
70+
$fs->remove(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'phpcr-shell');
71+
}
72+
73+
private function exec($command)
74+
{
75+
exec($command, $output, $return);
76+
$this->output += $output;
77+
$this->lastExitCode = $return;
78+
}
79+
80+
/**
81+
* @Given I run PHPCR shell with ":argsAndOptions"
82+
*/
83+
public function execShell($argsAndOptions)
84+
{
85+
$this->exec(sprintf('%s/bin/phpcrsh %s', $this->rootPath, $argsAndOptions));
86+
}
87+
88+
/**
89+
* @Given print output
90+
*/
91+
public function printOutput()
92+
{
93+
foreach ($this->output as $line) {
94+
echo $line . PHP_EOL;
95+
}
96+
}
97+
98+
/**
99+
* @Given the following profile ":profileName" exists:
100+
*/
101+
public function iHaveTheFollowingProfile($profileName, PyStringNode $text)
102+
{
103+
file_put_contents($this->workingDir . '/profiles/' . $profileName . '.yml', $text->getRaw());
104+
}
105+
106+
/**
107+
* @Given I initialize doctrine dbal
108+
*/
109+
public function initializeDoctrineDbal()
110+
{
111+
$this->filesystem->copy(
112+
$this->fixturesDir . '/jackalope-doctrine-dbal-cli-config.php',
113+
$this->workingDir . '/cli-config.php'
114+
);
115+
$this->exec($this->rootPath . '/vendor/jackalope/jackalope-doctrine-dbal/bin/jackalope jackalope:init:dbal --force');
116+
}
117+
118+
/**
119+
* @Then the command should not fail
120+
*/
121+
public function theCommandShouldNotFail()
122+
{
123+
\PHPUnit_Framework_Assert::assertEquals(0, $this->lastExitCode);
124+
}
125+
}

0 commit comments

Comments
 (0)