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

Commit ca14b18

Browse files
committed
Add assertion and refactor
1 parent e38d05d commit ca14b18

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

generator/src/FluentStageFactoryGenerator.php

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
use Nette\PhpGenerator\Method;
3131
use Nette\PhpGenerator\PhpNamespace;
3232
use Nette\PhpGenerator\TraitType;
33-
use RuntimeException;
3433
use stdClass;
35-
use Throwable;
3634

3735
use function array_key_last;
3836
use function array_map;
@@ -99,36 +97,26 @@ private function createFluentFactoryClass(GeneratorDefinition $definition): PhpN
9997
$staticFactory = ClassType::from(self::FACTORY_CLASS);
10098
assert($staticFactory instanceof ClassType);
10199
foreach ($staticFactory->getMethods() as $method) {
102-
if (! $method->isPublic()) {
103-
continue;
104-
}
105-
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-
}
100+
$this->addMethod($method, $class);
111101
}
112102

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);
103+
foreach ($staticFactory->getTraits() as $trait) {
104+
$staticFactory = TraitType::from($trait->getName());
105+
assert($staticFactory instanceof TraitType);
106+
foreach ($staticFactory->getMethods() as $method) {
107+
$this->addMethod($method, $class);
124108
}
125109
}
126110

127111
return $namespace;
128112
}
129113

130-
private function addMethod(Method $factoryMethod, PhpNamespace $namespace, ClassType $class): void
114+
private function addMethod(Method $factoryMethod, ClassType $class): void
131115
{
116+
if (! $factoryMethod->isPublic()) {
117+
return;
118+
}
119+
132120
if ($class->hasMethod($factoryMethod->getName())) {
133121
return;
134122
}

tests/Builder/FluentPipelineFactoryTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
use MongoDB\Builder\Query;
99
use MongoDB\Builder\Stage\FluentFactory;
1010
use MongoDB\Builder\Type\Sort;
11-
use PHPUnit\Framework\TestCase;
1211

13-
class FluentPipelineFactoryTest extends TestCase
12+
class FluentPipelineFactoryTest extends PipelineTestCase
1413
{
1514
public function testFluentPipelineFactory(): void
1615
{
@@ -22,5 +21,13 @@ public function testFluentPipelineFactory(): void
2221

2322
$this->assertInstanceof(Pipeline::class, $pipeline);
2423
$this->assertCount(3, $pipeline->getIterator());
24+
25+
$this->assertSamePipeline(<<<'JSON'
26+
[
27+
{ "$match": {"x": {"$eq": {"$numberInt": "1"}}}},
28+
{ "$project": {"_id": false, "x": true}},
29+
{ "$sort": {"x": {"$numberInt": "1"}}}
30+
]
31+
JSON, $pipeline);
2532
}
2633
}

0 commit comments

Comments
 (0)