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

Commit 1bb2508

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

File tree

3 files changed

+50
-45
lines changed

3 files changed

+50
-45
lines changed

generator/src/FluentStageFactoryGenerator.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
use MongoDB\Model\BSONArray;
2929
use Nette\PhpGenerator\ClassType;
3030
use Nette\PhpGenerator\Method;
31+
use Nette\PhpGenerator\Parameter;
3132
use Nette\PhpGenerator\PhpNamespace;
3233
use Nette\PhpGenerator\TraitType;
33-
use RuntimeException;
34+
use ReflectionClass;
3435
use stdClass;
35-
use Throwable;
3636

3737
use function array_key_last;
3838
use function array_map;
@@ -98,37 +98,37 @@ private function createFluentFactoryClass(GeneratorDefinition $definition): PhpN
9898

9999
$staticFactory = ClassType::from(self::FACTORY_CLASS);
100100
assert($staticFactory instanceof ClassType);
101+
102+
// Import the methods customized in the factory class
101103
foreach ($staticFactory->getMethods() as $method) {
102104
if (! $method->isPublic()) {
103105
continue;
104106
}
105107

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-
}
108+
$this->addMethod($method, $class);
111109
}
112110

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);
111+
// Import the other methods generated in the trait
112+
foreach ($staticFactory->getTraits() as $trait) {
113+
$staticFactory = TraitType::from($trait->getName());
114+
assert($staticFactory instanceof TraitType);
115+
foreach ($staticFactory->getMethods() as $method) {
116+
$this->addMethod($method, $class);
124117
}
125118
}
126119

127120
return $namespace;
128121
}
129122

130-
private function addMethod(Method $factoryMethod, PhpNamespace $namespace, ClassType $class): void
123+
private function addMethod(Method $factoryMethod, ClassType $class): void
131124
{
125+
// Non-public methods are not part of the API
126+
if (! $factoryMethod->isPublic()) {
127+
return;
128+
}
129+
130+
// Some methods can be overridden in the class, so we skip them
131+
// when importing the methods provided by the trait.
132132
if ($class->hasMethod($factoryMethod->getName())) {
133133
return;
134134
}
@@ -139,7 +139,7 @@ private function addMethod(Method $factoryMethod, PhpNamespace $namespace, Class
139139
$method->setParameters($factoryMethod->getParameters());
140140

141141
$args = array_map(
142-
fn ($param) => '$' . $param->getName(),
142+
fn (Parameter $param): string => '$' . $param->getName(),
143143
$factoryMethod->getParameters(),
144144
);
145145

@@ -155,9 +155,9 @@ private function addMethod(Method $factoryMethod, PhpNamespace $namespace, Class
155155
156156
return $this;
157157
PHP,
158-
'Stage',
158+
(new ReflectionClass(self::FACTORY_CLASS))->getShortName(),
159159
$factoryMethod->getName(),
160-
implode(',', $args),
160+
implode(', ', $args),
161161
));
162162
}
163163
}

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)