Skip to content

Commit 5aa4d6f

Browse files
author
Jefersson Nathan
committed
Add behat context initializer
1 parent 405a823 commit 5aa4d6f

File tree

8 files changed

+184
-10
lines changed

8 files changed

+184
-10
lines changed

behat.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ default:
1212
tags: "@event"
1313

1414
extensions:
15+
initializer.php: ~
1516
Behat\MinkExtension:
16-
base_url: http://conticket.local
17+
base_url: http://192.168.33.99/
1718
sessions:
1819
default:
1920
goutte: ~

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"behat/mink": "*@stable",
4848
"behat/mink-extension": "*",
4949
"behat/mink-selenium2-driver": "*",
50+
"behat/mink-goutte-driver": "*",
5051
"phpunit/phpunit": "4.7.*"
5152
},
5253
"scripts": {
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Feature;
4+
5+
use Behat\MinkExtension\Context\MinkContext;
6+
use Doctrine\ODM\MongoDB\DocumentManager;
7+
8+
/**
9+
* @author Jefersson Nathan <[email protected]>
10+
*/
11+
class AbstractContext extends MinkContext
12+
{
13+
/**
14+
* @var DocumentManager
15+
*/
16+
protected $documentManager;
17+
18+
/**
19+
* @return DocumentManager
20+
*/
21+
public function getDocumentManager()
22+
{
23+
return $this->documentManager;
24+
}
25+
26+
/**
27+
* @param DocumentManager $documentManager
28+
*/
29+
public function setDocumentManager(DocumentManager $documentManager)
30+
{
31+
$this->documentManager = $documentManager;
32+
}
33+
}
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Feature;
4+
5+
use Behat\Behat\Context\Initializer\ContextInitializer;
6+
use Behat\Behat\Context\Context;
7+
use Doctrine\MongoDB\Connection;
8+
use Doctrine\ODM\MongoDB\Configuration;
9+
use Doctrine\ODM\MongoDB\DocumentManager;
10+
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
11+
12+
/**
13+
* @author Jefersson Nathan <[email protected]>
14+
*/
15+
class CustomInitializer implements ContextInitializer
16+
{
17+
/**
18+
* @param Context $context
19+
*
20+
* @return bool
21+
*/
22+
public function supportsContext(Context $context)
23+
{
24+
return $context instanceof AbstractContext;
25+
}
26+
27+
/**
28+
* {@inheritDoc}
29+
*/
30+
public function initializeContext(Context $context)
31+
{
32+
if (! $this->supportsContext($context)) {
33+
return;
34+
}
35+
36+
AnnotationDriver::registerAnnotationClasses();
37+
38+
$config = new Configuration();
39+
$config->setProxyDir(sys_get_temp_dir());
40+
$config->setProxyNamespace('Proxies');
41+
$config->setHydratorDir(sys_get_temp_dir());
42+
$config->setHydratorNamespace('Hydrators');
43+
$config->setMetadataDriverImpl(AnnotationDriver::create(__DIR__ . '/../../src/Conticket/ApiBundle/Document/'));
44+
$config->setDefaultDB('symfony');
45+
46+
$documentManager = DocumentManager::create(
47+
new Connection('192.168.33.99'),
48+
$config
49+
);
50+
51+
/** @var AbstractContext $context */
52+
$context->setDocumentManager($documentManager);
53+
}
54+
}

features/bootstrap/EventContext.php

+26-6
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
namespace Feature;
1919

2020
use Behat\Gherkin\Node\TableNode;
21-
use Behat\MinkExtension\Context\MinkContext;
22-
use Documents\Event;
21+
use Conticket\ApiBundle\Document\Event;
22+
use PHPUnit_Framework_Assert as Assert;
2323

2424
/**
2525
* Event Context.
2626
*
2727
* @author Jefersson Nathan <[email protected]>
2828
*/
29-
class EventContext extends MinkContext
29+
class EventContext extends AbstractContext
3030
{
3131
/**
3232
* @Given /^application has following events:$/
@@ -35,7 +35,16 @@ class EventContext extends MinkContext
3535
*/
3636
public function applicationHasFollowingEvents(TableNode $table)
3737
{
38-
//TODO: fill database with data
38+
$this->getDocumentManager()
39+
->getSchemaManager()
40+
->dropDocumentCollection(Event::class);
41+
42+
foreach ($table->getHash() as $row) {
43+
$event = new Event($row['name'], $row['description'], $row['banner']);
44+
45+
$this->getDocumentManager()->persist($event);
46+
$this->getDocumentManager()->flush();
47+
}
3948
}
4049

4150
/**
@@ -49,8 +58,19 @@ public function iDoARequestToEventListPage()
4958
/**
5059
* @Then /^I should see (\d+) event listed$/
5160
*/
52-
public function iShouldSeeEventListed()
61+
public function iShouldSeeEventListed($amount)
62+
{
63+
Assert::assertSame(
64+
(int) $amount,
65+
count(json_decode($this->getSession()->getPage()->getContent()))
66+
);
67+
}
68+
69+
/**
70+
* @Given /^I should see "([^"]*)" on json response$/
71+
*/
72+
public function iShouldSeeOnJsonResponse($text)
5373
{
54-
echo $this->getSession()->getPage()->getContent();
74+
Assert::assertContains($text, $this->getSession()->getPage()->getContent());
5575
}
5676
}

features/event.feature

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@event
12
Feature: Allows interact and register events
23
In order to share my event
34
As a website user
@@ -12,9 +13,9 @@ Feature: Allows interact and register events
1213
Given I do a request to event list page
1314
Then I should see 1 event listed
1415
And the response status code should be 200
15-
And I should see "PHPeste"
16-
And I should see "The best event of PHP on Brazil northwest"
17-
And I should see "banner.jpg"
16+
And I should see "PHPeste" on json response
17+
And I should see "The best event of PHP on Brazil northwest" on json response
18+
And I should see "banner.jpg" on json response
1819

1920
Scenario: Creating a new event
2021
Given I do a request to event list page

initializer.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
4+
use Symfony\Component\DependencyInjection\ContainerBuilder;
5+
use Behat\Testwork\ServiceContainer\ExtensionManager;
6+
use Behat\Testwork\ServiceContainer\Extension;
7+
use Behat\Behat\Context\ServiceContainer\ContextExtension;
8+
9+
/**
10+
* @author Jefersson Nathan <[email protected]>
11+
*/
12+
class CustomExtension implements Extension
13+
{
14+
/**
15+
* @return string
16+
*/
17+
public function getConfigKey()
18+
{
19+
return 'initializer';
20+
}
21+
22+
/**
23+
* @param ContainerBuilder $container
24+
* @param array $config
25+
*/
26+
public function load(ContainerBuilder $container, array $config)
27+
{
28+
$definition = $container->register('initializer', \Feature\CustomInitializer::class);
29+
$definition->addTag(ContextExtension::INITIALIZER_TAG);
30+
}
31+
32+
/**
33+
* @param ArrayNodeDefinition $builder
34+
*/
35+
public function configure(ArrayNodeDefinition $builder)
36+
{
37+
}
38+
39+
/**
40+
* @param ExtensionManager $extensionManager
41+
*/
42+
public function initialize(ExtensionManager $extensionManager)
43+
{
44+
}
45+
46+
/**
47+
* @param ContainerBuilder $container
48+
*/
49+
public function process(ContainerBuilder $container)
50+
{
51+
}
52+
}
53+
54+
return new CustomExtension;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Conticket\ApiBundle\Command;
4+
5+
/**
6+
* @author Jefersson Nathan <[email protected]>
7+
*/
8+
class EventCommand
9+
{
10+
}

0 commit comments

Comments
 (0)