Skip to content
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

Feat/orc 816 add orm context #22

Merged
merged 3 commits into from
Mar 26, 2024
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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ jobs:
- name: Install dependencies
run: composer install

- name: Add doctrine/orm
run: composer require --no-progress --no-interaction --prefer-dist doctrine/orm:^2.0

- name: Run PHPUnit tests
run: composer phpunit
if: matrix.coverage == 'none'
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/static-analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ jobs:
- name: Install dependencies
run: composer install --no-progress --no-interaction --prefer-dist

- name: Add doctrine/orm
run: composer require --no-progress --no-interaction --prefer-dist doctrine/orm:^2.0

- name: Run script
run: composer code-style

Expand All @@ -35,6 +38,9 @@ jobs:
- name: Install dependencies
run: composer install --no-progress --no-interaction --prefer-dist

- name: Add doctrine/orm
run: composer require --no-progress --no-interaction --prefer-dist doctrine/orm:^2.0

- name: Run script
run: composer phpstan

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ Go to `behat.yml`
# ...
```

If you want use orm context add to `behat.yml`
```yaml
# ...
contexts:
- BehatApiContext\Context\ORMContext
# ...
```

Usage
=============

Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
"slevomat/coding-standard": "^7.0",
"squizlabs/php_codesniffer": "^3.6"
},
"suggest": {
"doctrine/orm": "^2.0"
},
"autoload": {
"psr-4": {
"BehatApiContext\\": "src"
Expand Down
82 changes: 82 additions & 0 deletions src/Context/ORMContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace BehatApiContext\Context;

use Behat\Behat\Context\Context;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use RuntimeException;

final class ORMContext implements Context
{
private EntityManagerInterface $manager;

public function __construct(EntityManagerInterface $manager)
{
$this->manager = $manager;
}

/**
* @And I see :count entities :entityClass
*/
public function andISeeInRepository(int $count, string $entityClass): void
{
$this->seeInRepository($count, $entityClass);
}

/**
* @Then I see :count entities :entityClass
*/
public function thenISeeInRepository(int $count, string $entityClass): void
{
$this->seeInRepository($count, $entityClass);
}

/**
* @And I see entity :entity with id :id
*/
public function andISeeEntityInRepositoryWithId(string $entityClass, string $id): void
{
$this->seeInRepository(1, $entityClass, ['id' => $id]);
}

/**
* @Then I see entity :entity with id :id
*/
public function thenISeeEntityInRepositoryWithId(string $entityClass, string $id): void
{
$this->seeInRepository(1, $entityClass, ['id' => $id]);
}

/**
* @param array<string, mixed> $params
*
* @throws NonUniqueResultException
* @throws NoResultException
*/
private function seeInRepository(int $count, string $entityClass, ?array $params = null): void
{
$query = $this->manager->createQueryBuilder()
->from($entityClass, 'e')
->select('count(e)');

if (null !== $params) {
foreach ($params as $columnName => $columnValue) {
$query->where("e.$columnName = :value$columnName")
->setParameter("value$columnName", $columnValue);
}
}

$realCount = $query->getQuery()
->getSingleScalarResult();

if ($count !== $realCount) {
throw new RuntimeException(
sprintf('Real count is %d, not %d', $realCount, $count),
);
}
}
}
25 changes: 25 additions & 0 deletions src/DependencyInjection/BehatApiContextExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
namespace BehatApiContext\DependencyInjection;

use BehatApiContext\Context\ApiContext;
use BehatApiContext\Context\ORMContext;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Container\ContainerInterface;
use RuntimeException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

Expand All @@ -25,6 +30,7 @@
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));

$this->loadApiContext($config, $loader, $container);
$this->enableOrmContext($config, $container);
}

/**
Expand All @@ -49,4 +55,23 @@
}
}
}

private function enableOrmContext(array $config, ContainerBuilder $container): void
{
$config['use_orm_context'] = $config['use_orm_context'] ?? true;

if (!$config['use_orm_context']) {
return;
}

if (!$container->has(EntityManagerInterface::class)) {
throw new RuntimeException('Entity manager does not exists');

Check warning on line 68 in src/DependencyInjection/BehatApiContextExtension.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/BehatApiContextExtension.php#L67-L68

Added lines #L67 - L68 were not covered by tests
}

$entityManagerDef = $container->get(EntityManagerInterface::class);

Check warning on line 71 in src/DependencyInjection/BehatApiContextExtension.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/BehatApiContextExtension.php#L71

Added line #L71 was not covered by tests

$ormContextDef = new Definition(ORMContext::class);
$ormContextDef->setArgument('$manager', $entityManagerDef);
$container->setDefinition(ORMContext::class, $ormContextDef);

Check warning on line 75 in src/DependencyInjection/BehatApiContextExtension.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/BehatApiContextExtension.php#L73-L75

Added lines #L73 - L75 were not covered by tests
}
}
8 changes: 6 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ private function addKernelResetManagersSection(NodeBuilder $builder): void
$builder
->arrayNode('kernel_reset_managers')
->scalarPrototype()->end()
->end()
->end();
->end()

->booleanNode('use_orm_context')
->defaultValue(true)
->end()
->end();
}
}
15 changes: 10 additions & 5 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ final class ConfigurationTest extends TestCase
public function testProcessConfigurationWithEmptyConfiguration(): void
{
$expectedBundleDefaultConfig = [
'kernel_reset_managers' => []
'kernel_reset_managers' => [],
'use_orm_context' => true,
];

$this->assertSame($expectedBundleDefaultConfig, $this->processConfiguration([]));
Expand All @@ -24,12 +25,14 @@ public function testProcessConfigurationWithDefaultConfiguration(): void
{
$config = [
'behat_api_context' => [
'kernel_reset_managers' => []
'kernel_reset_managers' => [],
'use_orm_context' => true,
]
];

$expectedBundleDefaultConfig = [
'kernel_reset_managers' => []
'kernel_reset_managers' => [],
'use_orm_context' => true,
];

$this->assertSame($expectedBundleDefaultConfig, $this->processConfiguration($config));
Expand All @@ -41,14 +44,16 @@ public function testProcessConfigurationWithFilledConfiguration(): void
'behat_api_context' => [
'kernel_reset_managers' => [
DoctrineResetManager::class
]
],
'use_orm_context' => true,
]
];

$expectedBundleDefaultConfig = [
'kernel_reset_managers' => [
DoctrineResetManager::class
]
],
'use_orm_context' => true,
];

$this->assertSame($expectedBundleDefaultConfig, $this->processConfiguration($config));
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
behat_api_context:
use_orm_context: false
3 changes: 2 additions & 1 deletion tests/DependencyInjection/Fixtures/filled_bundle_config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
behat_api_context:
use_orm_context: false
kernel_reset_managers:
- BehatApiContext\Service\ResetManager\DoctrineResetManager
- BehatApiContext\Service\ResetManager\DoctrineResetManager
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BehatApiContext\Tests\Unit\Context;
namespace BehatApiContext\Tests\Unit\Context\Api;

use BehatApiContext\Context\ApiContext;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BehatApiContext\Tests\Unit\Context;
namespace BehatApiContext\Tests\Unit\Context\Api;

use Behat\Gherkin\Node\PyStringNode;
use RuntimeException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BehatApiContext\Tests\Unit\Context;
namespace BehatApiContext\Tests\Unit\Context\Api;

use Behat\Gherkin\Node\PyStringNode;
use ReflectionClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BehatApiContext\Tests\Unit\Context;
namespace BehatApiContext\Tests\Unit\Context\Api;

use BehatApiContext\Service\ResetManager\ResetManagerInterface;
use ReflectionClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BehatApiContext\Tests\Unit\Context;
namespace BehatApiContext\Tests\Unit\Context\Api;

use RuntimeException;
use Symfony\Component\HttpFoundation\Response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BehatApiContext\Tests\Unit\Context;
namespace BehatApiContext\Tests\Unit\Context\Api;

use Behat\Gherkin\Node\PyStringNode;
use JsonException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BehatApiContext\Tests\Unit\Context;
namespace BehatApiContext\Tests\Unit\Context\Api;

use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\HttpFoundation\Request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BehatApiContext\Tests\Unit\Context;
namespace BehatApiContext\Tests\Unit\Context\Api;

use JsonException;
use ReflectionException;
Expand Down
Loading
Loading