Skip to content

Commit c77f7f3

Browse files
author
xmarchegay
committed
#20 - Add unit tests for EntityManager-related tasks
1 parent 6e993e6 commit c77f7f3

File tree

7 files changed

+533
-1
lines changed

7 files changed

+533
-1
lines changed

GEMINI.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Gemini Code Assistant Context
2+
3+
## Project Overview
4+
5+
This project is a Symfony bundle named `CleverAge/DoctrineProcessBundle`. It is part of the `CleverAge/ProcessBundle` ecosystem and provides Doctrine ORM integration for data processing tasks. The bundle allows developers to create and configure ETL-like processes (Extract, Transform, Load) that can read from and write to a database using Doctrine entities.
6+
7+
The core of the bundle is a set of "tasks" that can be chained together in a process. These tasks include:
8+
9+
* **Reading:** Fetching entities from the database based on criteria.
10+
* **Writing:** Creating or updating entities.
11+
* **Batch Writing:** Writing entities in batches for better performance.
12+
* **Removing:** Deleting entities.
13+
* **Cleaning/Detaching:** Managing the Doctrine entity manager's identity map.
14+
15+
The bundle is built for Symfony and integrates with the Doctrine project.
16+
17+
## Building and Running
18+
19+
The project uses Docker for its development environment. The following commands are available in the `Makefile`:
20+
21+
* **`make start`**: Starts the Docker containers for the development environment.
22+
* **`make stop`**: Stops the Docker containers.
23+
* **`make bash`**: Opens a bash shell inside the PHP container.
24+
25+
### Dependencies
26+
27+
PHP dependencies are managed with Composer. To install them, run:
28+
29+
```bash
30+
make src/vendor
31+
```
32+
33+
This is typically done as part of the `make start` or `make up` commands.
34+
35+
### Testing
36+
37+
The project uses PHPUnit for testing. To run the test suite:
38+
39+
```bash
40+
make tests
41+
```
42+
or
43+
```bash
44+
make phpunit
45+
```
46+
47+
### Quality and Linting
48+
49+
The project has a set of quality tools to ensure code standards. The main command is:
50+
51+
```bash
52+
make quality
53+
```
54+
55+
This command runs the following tools:
56+
57+
* **PHPStan**: Static analysis to find potential bugs.
58+
```bash
59+
make phpstan
60+
```
61+
* **PHP-CS-Fixer**: Enforces a consistent coding style.
62+
```bash
63+
make php-cs-fixer
64+
```
65+
* **Rector**: Provides automated refactoring to improve code quality.
66+
```bash
67+
make rector
68+
```
69+
70+
## Development Conventions
71+
72+
### Coding Style
73+
74+
The project follows the official Symfony coding standards, as enforced by the `.php-cs-fixer.dist.php` configuration. It uses the `@Symfony` and `@DoctrineAnnotation` rule sets. All files should include a license header.
75+
76+
### Branching and Commits
77+
78+
While not explicitly defined in the repository, a standard Git flow (e.g., feature branches, pull requests) is expected, as indicated by the presence of a `PULL_REQUEST_TEMPLATE.md`.
79+
80+
### Documentation
81+
82+
The `docs` directory contains user-facing documentation for the bundle and its tasks. When adding a new task, it should be documented in a corresponding `.md` file.

src/Task/EntityManager/DoctrineBatchWriterTask.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function writeBatch(ProcessState $state): void
7272
throw new \UnexpectedValueException("No manager found for class {$class}");
7373
}
7474
$entityManager->persist($entity);
75-
$entityManagers->attach($entityManager);
75+
$entityManagers->offsetSet($entityManager);
7676
}
7777

7878
foreach ($entityManagers as $entityManager) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the CleverAge/DoctrineProcessBundle package.
7+
*
8+
* Copyright (c) Clever-Age
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace CleverAge\DoctrineProcessBundle\Tests\Task\EntityManager;
15+
16+
use CleverAge\DoctrineProcessBundle\Task\EntityManager\AbstractDoctrineQueryTask;
17+
use Doctrine\ORM\EntityManagerInterface;
18+
use Doctrine\ORM\EntityRepository;
19+
use Doctrine\ORM\QueryBuilder;
20+
use PHPUnit\Framework\Attributes\CoversClass;
21+
use PHPUnit\Framework\TestCase;
22+
23+
#[CoversClass(AbstractDoctrineQueryTask::class)]
24+
class AbstractDoctrineQueryTaskTest extends TestCase
25+
{
26+
public function testGetQueryBuilderWithInvalidField(): void
27+
{
28+
$this->expectException(\UnexpectedValueException::class);
29+
30+
$task = $this->createStub(AbstractDoctrineQueryTask::class);
31+
$repository = $this->createStub(EntityRepository::class);
32+
$repository->method('createQueryBuilder')->willReturn(new QueryBuilder($this->createStub(EntityManagerInterface::class)));
33+
34+
$reflection = new \ReflectionClass(AbstractDoctrineQueryTask::class);
35+
$method = $reflection->getMethod('getQueryBuilder');
36+
37+
$method->invoke($task, $repository, ['e.field; DROP TABLE dummy;' => 'value'], []);
38+
}
39+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the CleverAge/DoctrineProcessBundle package.
7+
*
8+
* Copyright (c) Clever-Age
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace CleverAge\DoctrineProcessBundle\Tests\Task\EntityManager;
15+
16+
use CleverAge\DoctrineProcessBundle\Task\EntityManager\ClearEntityManagerTask;
17+
use CleverAge\ProcessBundle\Model\ProcessState;
18+
use Doctrine\ORM\EntityManagerInterface;
19+
use Doctrine\Persistence\ManagerRegistry;
20+
use PHPUnit\Framework\Attributes\CoversClass;
21+
use PHPUnit\Framework\TestCase;
22+
23+
#[CoversClass(ClearEntityManagerTask::class)]
24+
class ClearEntityManagerTaskTest extends TestCase
25+
{
26+
public function testExecute(): void
27+
{
28+
$entityManager = $this->createMock(EntityManagerInterface::class);
29+
$entityManager->expects($this->once())
30+
->method('clear');
31+
32+
$managerRegistry = $this->createMock(ManagerRegistry::class);
33+
$managerRegistry->expects($this->once())
34+
->method('getManager')
35+
->willReturn($entityManager);
36+
37+
$task = $this->getMockBuilder(ClearEntityManagerTask::class)
38+
->setConstructorArgs([$managerRegistry])
39+
->onlyMethods(['getOption']) // Mock only the getOption method
40+
->getMock();
41+
42+
// Ensure getOption returns null for 'entity_manager', mimicking default behavior
43+
$task->expects($this->once())
44+
->method('getOption')
45+
->with(self::anything(), 'entity_manager')
46+
->willReturn(null);
47+
48+
$processState = $this->createStub(ProcessState::class);
49+
50+
$task->execute($processState);
51+
}
52+
}

0 commit comments

Comments
 (0)