Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit 06b8519

Browse files
committed
Improve tests and refactor generator
1 parent 31d537c commit 06b8519

File tree

3 files changed

+48
-44
lines changed

3 files changed

+48
-44
lines changed

generator/src/FluentStageFactoryGenerator.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@
3030
use Nette\PhpGenerator\Method;
3131
use Nette\PhpGenerator\PhpNamespace;
3232
use Nette\PhpGenerator\TraitType;
33-
use RuntimeException;
33+
use ReflectionClass;
3434
use stdClass;
35-
use Throwable;
3635

3736
use function array_key_last;
3837
use function array_map;
@@ -98,37 +97,37 @@ private function createFluentFactoryClass(GeneratorDefinition $definition): PhpN
9897

9998
$staticFactory = ClassType::from(self::FACTORY_CLASS);
10099
assert($staticFactory instanceof ClassType);
100+
101+
// Import the methods customized in the factory class
101102
foreach ($staticFactory->getMethods() as $method) {
102103
if (! $method->isPublic()) {
103104
continue;
104105
}
105106

106-
try {
107-
$this->addMethod($method, $namespace, $class);
108-
} catch (Throwable $e) {
109-
throw new RuntimeException(sprintf('Failed to generate class for operator "%s"', $operator->name), 0, $e);
110-
}
107+
$this->addMethod($method, $class);
111108
}
112109

113-
$staticFactory = TraitType::from(Stage\FactoryTrait::class);
114-
assert($staticFactory instanceof TraitType);
115-
foreach ($staticFactory->getMethods() as $method) {
116-
if (! $method->isPublic()) {
117-
continue;
118-
}
119-
120-
try {
121-
$this->addMethod($method, $namespace, $class);
122-
} catch (Throwable $e) {
123-
throw new RuntimeException(sprintf('Failed to generate class for operator "%s"', $operator->name), 0, $e);
110+
// Import the other methods generated in the trait
111+
foreach ($staticFactory->getTraits() as $trait) {
112+
$staticFactory = TraitType::from($trait->getName());
113+
assert($staticFactory instanceof TraitType);
114+
foreach ($staticFactory->getMethods() as $method) {
115+
$this->addMethod($method, $class);
124116
}
125117
}
126118

127119
return $namespace;
128120
}
129121

130-
private function addMethod(Method $factoryMethod, PhpNamespace $namespace, ClassType $class): void
122+
private function addMethod(Method $factoryMethod, ClassType $class): void
131123
{
124+
// Non-public methods are not part of the API
125+
if (! $factoryMethod->isPublic()) {
126+
return;
127+
}
128+
129+
// Some methods can be overridden in the class, so we skip them
130+
// when importing the methods provided by the trait.
132131
if ($class->hasMethod($factoryMethod->getName())) {
133132
return;
134133
}
@@ -155,9 +154,9 @@ private function addMethod(Method $factoryMethod, PhpNamespace $namespace, Class
155154
156155
return $this;
157156
PHP,
158-
'Stage',
157+
(new ReflectionClass(self::FACTORY_CLASS))->getShortName(),
159158
$factoryMethod->getName(),
160-
implode(',', $args),
159+
implode(', ', $args),
161160
));
162161
}
163162
}

src/Builder/Stage/FluentFactory.php

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Builder/FluentPipelineFactoryTest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
namespace MongoDB\Tests\Builder;
66

7-
use MongoDB\Builder\Pipeline;
87
use MongoDB\Builder\Query;
98
use MongoDB\Builder\Stage\FluentFactory;
109
use MongoDB\Builder\Type\Sort;
11-
use PHPUnit\Framework\TestCase;
1210

13-
class FluentPipelineFactoryTest extends TestCase
11+
class FluentPipelineFactoryTest extends PipelineTestCase
1412
{
1513
public function testFluentPipelineFactory(): void
1614
{
@@ -20,7 +18,14 @@ public function testFluentPipelineFactory(): void
2018
->sort(x: Sort::Asc)
2119
->getPipeline();
2220

23-
$this->assertInstanceof(Pipeline::class, $pipeline);
24-
$this->assertCount(3, $pipeline->getIterator());
21+
$expected = <<<'json'
22+
[
23+
{"$match": {"x": {"$eq": {"$numberInt": "1"}}}},
24+
{"$project": {"_id": false, "x": true}},
25+
{"$sort": {"x": {"$numberInt": "1"}}}
26+
]
27+
json;
28+
29+
$this->assertSamePipeline($expected, $pipeline);
2530
}
2631
}

0 commit comments

Comments
 (0)