Skip to content

Commit

Permalink
Merge pull request #70 from jakzal/proxies-target-dir
Browse files Browse the repository at this point in the history
Proxies target dir
  • Loading branch information
jakzal committed Mar 16, 2016
2 parents faf0a08 + 2e36c1a commit b94ddc4
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 19 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ matrix:
- php: 5.5
- php: 5.6
- php: 7.0
allow_failures:
- php: 7.0
fast_finish: true

install:
- sudo apt-get install nginx
- sudo cp ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf.default ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf
- if [[ -f ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.d/www.conf.default ]]; then sudo cp ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.d/www.conf.default ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.d/www.conf; fi
- if [[ -f ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf.default ]]; then sudo cp ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf.default ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf; fi
- echo "cgi.fix_pathinfo = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- ~/.phpenv/versions/$(phpenv version-name)/sbin/php-fpm
- sudo cp -f .travis_nginx.conf /etc/nginx/sites-enabled/default.conf
- sudo sed -e "s?%APP_DIR%?$(pwd)/features/application?g" --in-place /etc/nginx/sites-enabled/default.conf
- sudo service nginx restart

before_script:
- phpenv config-rm xdebug.ini
- composer self-update && composer --dev install

script:
Expand Down
5 changes: 4 additions & 1 deletion behat.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
default:
calls:
error_reporting: -1
suites:
default:
filters:
tags: "~@wip"
tags: "~@wip&&~@fixtures"
contexts:
- PhpServerContext
- BehatRunnerContext
- InjectingPageObjectsContext

ci:
suites:
Expand Down
1 change: 1 addition & 0 deletions features/application/index.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?php error_reporting(-1); ?>
<!DOCTYPE html>
<html>
<head>
Expand Down
23 changes: 22 additions & 1 deletion features/bootstrap/BehatRunnerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -105,13 +106,33 @@ public function itShouldPass()
{
try {
expect($this->process->getExitCode())->toBe(0);
expect($this->process->getErrorOutput())->notToMatch('/PHP Warning: /');
expect($this->process->getErrorOutput())->notToMatch('/PHP Notice: /');
} catch (\Exception $e) {
echo $this->getOutput();

throw $e;
}
}

public function givenBehatProject($path)
{
$this->getFilesystem()->mirror($path, $this->workingDir);
}

/**
* @param string $path
*
* @return Finder
*/
public function listWorkingDir($path)
{
return Finder::create()
->files()
->name('*.php')
->in($this->workingDir.$path);
}

/**
* @Then /^it should fail$/
*/
Expand Down Expand Up @@ -176,4 +197,4 @@ private function findPhpBinary()

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

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Context\Context;

final class InjectingPageObjectsContext implements Context
{
/**
* @var BehatRunnerContext|null
*/
private $behatRunnerContext;

/**
* @BeforeScenario
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$this->behatRunnerContext = $scope->getEnvironment()->getContext('BehatRunnerContext');
}

/**
* @Given a feature with a context file that uses page objects
*/
public function aFeatureWithAContextFileThatUsesPageObjects()
{
$this->behatRunnerContext->givenBehatProject('features/fixtures/default/');
}

/**
* @Then the proxies should be generated in the :path directory
*/
public function theProxiesShouldBeGeneratedInTheDirectory($path)
{
$filesCount = $this->behatRunnerContext->listWorkingDir($path)->count();

if ($filesCount < 1) {
throw new \LogicException('Expected at least one proxy to be generated but found none.');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Page\Element;

use SensioLabs\Behat\PageObjectExtension\PageObject\Element;

class SearchResultsNavigation extends Element
{
/**
* @var string
*/
protected $selector = 'div.tabs';

/**
* @return boolean
*/
public function hasTab($name)
{
return $this->hasLink($name);
}
}
13 changes: 13 additions & 0 deletions features/fixtures/default/features/bootstrap/Page/Homepage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Page;

use SensioLabs\Behat\PageObjectExtension\PageObject\Page;

class Homepage extends Page
{
/**
* @var string
*/
protected $path = '/';
}
46 changes: 46 additions & 0 deletions features/fixtures/default/features/bootstrap/SearchContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use Behat\Behat\Context\Context;
use Page\Homepage;
use Page\Element\SearchResultsNavigation;

class SearchContext implements Context
{
/**
* @var Homepage
*/
private $homepage;

/**
* @var SearchResultsNavigation
*/
private $searchResultsNavigation;

/**
* @param Homepage $homepage
* @param SearchResultsNavigation $searchResultsNavigation
*/
public function __construct(Homepage $homepage, SearchResultsNavigation $searchResultsNavigation)
{
$this->homepage = $homepage;
$this->searchResultsNavigation = $searchResultsNavigation;
}

/**
* @Given /^I visited the homepage$/
*/
public function iVisitedTheHomepage()
{
$this->homepage->open();
}

/**
* @When /^I should not see the "(?P<tab>[^"]*)" tab$/
*/
public function iShouldSeeNotTheTab($tab)
{
if ($this->searchResultsNavigation->hasTab($tab)) {
throw new \LogicException(sprintf('%s tab is present on the page', $tab));
}
}
}
9 changes: 9 additions & 0 deletions features/fixtures/default/features/search.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@fixtures
Feature: Search
In order to find lolcats
As a Cat Lover
I want to search the internetz

Scenario: Searching for lolcats
Given I visited the homepage
Then I should not see the "Images" tab
Empty file.
21 changes: 19 additions & 2 deletions features/injecting_a_page_object_directly_into_the_context.feature
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ Feature: Injecting a page object directly into the context
namespace Page\Element;
use Behat\Mink\Exception\ElementNotFoundException;
use SensioLabs\Behat\PageObjectExtension\PageObject\Element;
use SensioLabs\Behat\PageObjectExtension\PageObject\Page;
class SearchResultsNavigation extends Element
{
Expand Down Expand Up @@ -110,3 +108,22 @@ Feature: Injecting a page object directly into the context
When I run behat
Then it should pass

Scenario: Configuring the generated proxy location
Given a behat configuration:
"""
default:
suites:
default:
contexts: [SearchContext]
extensions:
SensioLabs\Behat\PageObjectExtension:
factory:
proxies_target_dir: %paths.base%/tmp/
Behat\MinkExtension:
goutte: ~
base_url: http://localhost:8000
"""
And a feature with a context file that uses page objects
When I run behat
Then it should pass
And the proxies should be generated in the "tmp" directory
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function let(ContainerBuilder $container, ParameterBagInterface $parameterBag)
$container->addResource(Argument::any())->willReturn($container);
$container->getParameterBag()->willReturn($parameterBag);
$container->setDefinition(Argument::cetera())->willReturn(null);
$container->setParameter(Argument::cetera())->willReturn(null);
$container->addCompilerPass(Argument::cetera())->willReturn(null);
$container->setAlias(Argument::cetera())->willReturn(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,8 @@ public function load(ContainerBuilder $container, array $config)
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/config'));
$loader->load('services.xml');

if (isset($config['namespaces'])) {
$this->updateNamespaceParameters($container, $config['namespaces']);
}

if (isset($config['factory'])) {
$this->updatePageObjectFactoryDefinition($container, $config['factory']);
}

if (!interface_exists('ProxyManager\Proxy\LazyLoadingInterface')) {
$container->removeDefinition('sensio_labs.page_object_extension.context.argument_resolver.page_object');
}
$this->updateNamespaceParameters($container, isset($config['namespaces']) ? $config['namespaces'] : array());
$this->updatePageObjectFactoryDefinition($container, isset($config['factory']) ? $config['factory'] : array());
}

/**
Expand Down Expand Up @@ -99,6 +90,10 @@ private function configurePageObjectFactory(ArrayNodeDefinition $builder)
->info('parameters passed from the factory when creating a page')
->prototype('scalar')->end()
->end()
->scalarNode('proxies_target_dir')
->info('Target directory for proxies generated for the lazy factory')
->defaultValue(sys_get_temp_dir())
->end()
->end();
}

Expand Down Expand Up @@ -135,5 +130,21 @@ private function updatePageObjectFactoryDefinition(ContainerBuilder $container,
if (!empty($factory['page_parameters'])) {
$container->setParameter('sensio_labs.page_object_extension.page_factory.page_parameters', $factory['page_parameters']);
}

$this->configureFactoryProxies($container, $factory);
}

/**
* @param ContainerBuilder $container
* @param array $factory
*/
private function configureFactoryProxies(ContainerBuilder $container, array $factory)
{
$proxiesTargetDir = !empty($factory['proxies_target_dir']) ? $factory['proxies_target_dir'] : sys_get_temp_dir();
$container->setParameter('sensio_labs.page_object_extension.proxies_target_dir', $proxiesTargetDir);

if (!interface_exists('ProxyManager\Proxy\LazyLoadingInterface')) {
$container->removeDefinition('sensio_labs.page_object_extension.context.argument_resolver.page_object');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
<service id="sensio_labs.page_object_extension.page_factory.lazy" class="SensioLabs\Behat\PageObjectExtension\PageObject\Factory\LazyFactory" public="false">
<argument type="service" id="sensio_labs.page_object_extension.page_factory" />
<argument type="service">
<service class="ProxyManager\Factory\LazyLoadingValueHolderFactory" />
<service class="ProxyManager\Factory\LazyLoadingValueHolderFactory">
<argument type="service">
<service class="ProxyManager\Configuration">
<call method="setProxiesTargetDir"><argument>%sensio_labs.page_object_extension.proxies_target_dir%</argument></call>
</service>
</argument>
</service>
</argument>
</service>

Expand Down

0 comments on commit b94ddc4

Please sign in to comment.