Skip to content

[Tests] Streamline CompiledUrlGenerator tests #52481

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

Merged
merged 1 commit into from
Dec 29, 2023

Conversation

OskarStark
Copy link
Contributor

@OskarStark OskarStark commented Nov 7, 2023

Q A
Branch? 7.1
Bug fix? no
New feature? no
Deprecations? no
Issues Follows #52402
License MIT

weird, it looks like the expected exception is thrown in $this->generatorDumper->dump() 🤔 So the following code with the CompiledUrlGenerator is not needed, so maybe the test is not needed anymore or the test must be adjusted somehow to test the behavior of CompiledUrlgenerator....

@OskarStark OskarStark self-assigned this Nov 7, 2023
@carsonbot carsonbot added this to the 6.4 milestone Nov 7, 2023

$compiledUrlGenerator = new CompiledUrlGenerator(require $this->testTmpFilepath, new RequestContext());
$compiledUrlGenerator->generate('a');
}

public function testTargetAliasWithNamePrefixNotExisting()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after investigating why testTargetAliasWithNamePrefixNotExisting are now failing I found out, that both, CompiledUrlGeneratorDUMPER and CompiledUrlGenerator throw a RouteNotFoundException. We are testing $compiledUrlGenerator->generate('sub_a) here, but the exception is already thrown in file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump()); when generating the dump to prepare a file.

So now after moving the expectedException method call down, I can say, that we are actually not testing the behavior of CompiledUrlGenerator in this test 🤷‍♂️

Same applies to testTargetAliasNotExisting

@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@OskarStark OskarStark changed the title [Tests] Streamline CompiledUrlGenerator tests [Tests] Streamline CompiledUrlGenerator tests Dec 20, 2023
@OskarStark OskarStark force-pushed the streamline-compiled-url-gen-test branch from 35dec94 to 9aaf1f0 Compare December 27, 2023 08:52
@OskarStark OskarStark added the Help wanted Issues and PRs which are looking for volunteers to complete them. label Dec 27, 2023
@alexandre-daubois
Copy link
Member

What about:

diff --git a/src/Symfony/Component/Routing/Tests/Generator/Dumper/CompiledUrlGeneratorDumperTest.php b/src/Symfony/Component/Routing/Tests/Generator/Dumper/CompiledUrlGeneratorDumperTest.php
index 8603ab6d98..572f524a05 100644
--- a/src/Symfony/Component/Routing/Tests/Generator/Dumper/CompiledUrlGeneratorDumperTest.php
+++ b/src/Symfony/Component/Routing/Tests/Generator/Dumper/CompiledUrlGeneratorDumperTest.php
@@ -267,66 +272,72 @@ class CompiledUrlGeneratorDumperTest extends TestCase
 
     public function testTargetAliasNotExisting()
     {
-        $this->expectException(RouteNotFoundException::class);
-
-        $this->routeCollection->addAlias('a', 'not-existing');
+        $this->routeCollection->add('not-existing', new Route('/not-existing'));
+        $this->routeCollection->addAlias('alias', 'not-existing');
 
         file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump());
 
-        $compiledUrlGenerator = new CompiledUrlGenerator(require $this->testTmpFilepath, new RequestContext());
+        $compiledRoutes = require $this->testTmpFilepath;
+        unset($compiledRoutes['alias']);
 
+        $this->expectException(RouteNotFoundException::class);
+
+        $compiledUrlGenerator = new CompiledUrlGenerator($compiledRoutes, new RequestContext());
         $compiledUrlGenerator->generate('a');
     }
 
     public function testTargetAliasWithNamePrefixNotExisting()
     {
-        $this->expectException(RouteNotFoundException::class);
-
         $subCollection = new RouteCollection();
-        $subCollection->addAlias('a', 'not-existing');
+        $subCollection->add('not-existing', new Route('/not-existing'));
+        $subCollection->addAlias('alias', 'not-existing');
         $subCollection->addNamePrefix('sub_');
 
         $this->routeCollection->addCollection($subCollection);
 
         file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump());
 
-        $compiledUrlGenerator = new CompiledUrlGenerator(require $this->testTmpFilepath, new RequestContext());
+        $compiledRoutes = require $this->testTmpFilepath;
+        unset($compiledRoutes['sub_alias']);
 
-        $compiledUrlGenerator->generate('sub_a');
+        $this->expectException(RouteNotFoundException::class);
+
+        $compiledUrlGenerator = new CompiledUrlGenerator($compiledRoutes, new RequestContext());
+        $compiledUrlGenerator->generate('sub_alias');
     }

We generate the file with the existing route then we remove it from compiled ones. This way, we ensure that we test the exception coming from route generation. Works on my computer 😄

@OskarStark
Copy link
Contributor Author

Thanks, lets see

@alexandre-daubois
Copy link
Member

Seems to do the job!

@OskarStark OskarStark force-pushed the streamline-compiled-url-gen-test branch from cdc20fb to a778254 Compare December 27, 2023 12:05
@OskarStark
Copy link
Contributor Author

@nicolas-grekas I think this should be merged into 5.4

@nicolas-grekas nicolas-grekas modified the milestones: 7.1, 5.4 Dec 27, 2023
@nicolas-grekas
Copy link
Member

Works for me, please rebase.

@OskarStark OskarStark force-pushed the streamline-compiled-url-gen-test branch from a778254 to 807d70e Compare December 27, 2023 12:51
@OskarStark OskarStark changed the base branch from 7.1 to 5.4 December 27, 2023 12:51
@OskarStark
Copy link
Contributor Author

Rebased ✅

@OskarStark OskarStark added Ready and removed Help wanted Issues and PRs which are looking for volunteers to complete them. labels Dec 28, 2023
@fabpot
Copy link
Member

fabpot commented Dec 29, 2023

Thank you @OskarStark.

@fabpot fabpot merged commit 895081d into symfony:5.4 Dec 29, 2023
@OskarStark OskarStark deleted the streamline-compiled-url-gen-test branch December 29, 2023 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants