Skip to content
This repository has been archived by the owner on Jun 8, 2020. It is now read-only.

Commit

Permalink
Use test.service_container instead of container
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Oct 11, 2018
1 parent a0109b3 commit e86b543
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 33 deletions.
55 changes: 33 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,45 @@ want to test that updating a user updates its password, you still need to add a
## Install

```bash
composer require --dev vincentchalamon/api-extension:dev-master
composer require --dev vincentchalamon/api-extension
```

As all services in Symfony are private, you need to override them to inject them:
Declare required extensions in your Behat configuration:
```yaml
# behat.yml.dist
default:
# ...
suites:
default:
contexts:
- Behat\MinkExtension\Context\MinkContext
- Behatch\Context\RestContext
- Behatch\Context\JsonContext
- ApiExtension\Context\ApiContext
- ApiExtension\Context\FixturesContext
extensions:
Behat\Symfony2Extension:
kernel:
bootstrap: features/bootstrap/bootstrap.php
class: App\Kernel
Behat\MinkExtension:
base_url: 'http://www.example.com/'
sessions:
default:
symfony2: ~
Behatch\Extension: ~
# ...
ApiExtension: ~
```
## Usage with Symfony FrameworkBundle < 4.1
Running with Symfony FrameworkBundle < 4.1, you need to override some private services:
```yaml
# config/services_test.yaml

# Hack for Behat: allow to inject some private services
# Waiting for Behat/Symfony2Extension to support autowiring (https://goo.gl/z8BPpG)
services:
test.property_info:
parent: property_info
Expand All @@ -43,30 +73,11 @@ services:
public: true
```
Declare required extensions in your Behat configuration:
```yaml
# behat.yml.dist
default:
# ...
suites:
default:
contexts:
- Behat\MinkExtension\Context\MinkContext
- Behatch\Context\RestContext
- Behatch\Context\JsonContext
- ApiExtension\Context\ApiContext
- ApiExtension\Context\FixturesContext
extensions:
Behat\Symfony2Extension:
kernel:
bootstrap: features/bootstrap/bootstrap.php
class: App\Kernel
Behat\MinkExtension:
base_url: 'http://www.example.com/'
sessions:
default:
symfony2: ~
Behatch\Extension: ~
# ...
ApiExtension:
services:
Expand Down
2 changes: 0 additions & 2 deletions src/Context/ApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ public function sendPostRequestToCollection(string $name, $data = null, $groups
*/
public function iGetTheApiDocInFormat(string $format)
{
// todo Do not hard-code url
$this->restContext->iSendARequestTo('GET', $this->helper->getUrl('api_doc', ['_format' => $format]));
}

Expand Down Expand Up @@ -290,7 +289,6 @@ public function responseStatusCodeShouldBe405()
public function itemShouldHaveBeSuccessfullyDeleted()
{
$this->minkContext->assertResponseStatus(204);
// todo Ensure object has been deleted from database
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/SchemaGenerator/ObjectSchemaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public function generate(\ReflectionClass $reflectionClass, array $context = [])
$this->path[$context['depth']] = $property;
}
if (($mapping['targetEntity'] ?? null) === $reflectionClass->name) {
// todo Is there a better way to handle this case?
$schema['properties'][$property] = $this->typeGenerator->generate($mapping, ['serializer_groups' => []] + $context);
} else {
$schema['properties'][$property] = $this->typeGenerator->generate($mapping, $context);
Expand Down
4 changes: 3 additions & 1 deletion src/ServiceContainer/ApiConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function __construct(KernelInterface $kernel, array $parameters)

public function configure($service)
{
$container = $this->container->has('test.service_container') ? $this->container->get('test.service_container') : $this->container;

foreach ($this->parameters as $name => $value) {
$methodName = 'set'.ucfirst($name);
if (!method_exists($service, $methodName)) {
Expand All @@ -44,7 +46,7 @@ public function configure($service)
if (preg_match('/^%(.*)%$/', $value, $matches)) {
$value = $this->container->getParameter($matches[1]);
} else {
$value = $this->container->get('@' === substr($value, 0, 1) ? substr($value, 1) : $value);
$value = $container->get('@' === substr($value, 0, 1) ? substr($value, 1) : $value);
}
}
\call_user_func([$service, $methodName], $value);
Expand Down
14 changes: 7 additions & 7 deletions src/ServiceContainer/ApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,37 +60,37 @@ public function configure(ArrayNodeDefinition $builder)
->defaultValue('fr_FR')
->end()
->arrayNode('services')
->isRequired()
->addDefaultsIfNotSet()
->children()
->scalarNode('metadataFactory')
->info('An instance of ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface')
->cannotBeEmpty()
->isRequired()
->defaultValue('@api_platform.metadata.resource.metadata_factory.annotation')
->end()
->scalarNode('iriConverter')
->info('An instance of ApiPlatform\Core\Api\IriConverterInterface')
->cannotBeEmpty()
->isRequired()
->defaultValue('@api_platform.iri_converter')
->end()
->scalarNode('registry')
->info('An instance of Doctrine\Common\Persistence\ManagerRegistry')
->cannotBeEmpty()
->isRequired()
->defaultValue('@doctrine')
->end()
->scalarNode('propertyInfo')
->info('An instance of Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface')
->cannotBeEmpty()
->isRequired()
->defaultValue('@property_info')
->end()
->scalarNode('annotationReader')
->info('An instance of Doctrine\Common\Annotations\Reader')
->cannotBeEmpty()
->isRequired()
->defaultValue('@annotation_reader')
->end()
->scalarNode('router')
->info('An instance of Symfony\Component\Routing\RouterInterface')
->cannotBeEmpty()
->isRequired()
->defaultValue('@router')
->end()
->end()
->end()
Expand Down

0 comments on commit e86b543

Please sign in to comment.