@@ -30,6 +30,81 @@ public function testPriority()
3030 ), $ this ->router ->all ());
3131 }
3232
33+ /**
34+ * Routers are supposed to be sorted only once.
35+ * This test will check that by trying to get all routers several times.
36+ *
37+ * @covers \Symfony\Cmf\Component\Routing\ChainRouter::sortRouters
38+ * @covers \Symfony\Cmf\Component\Routing\ChainRouter::all
39+ */
40+ public function testSortRouters ()
41+ {
42+ list ($ low , $ medium , $ high ) = $ this ->createRouterMocks ();
43+ // We're using a mock here and not $this->router because we need to ensure that the sorting operation is done only once.
44+ $ router = $ this ->buildMock ('Symfony \\Cmf \\Component \\Routing \\ChainRouter ' , array ('sortRouters ' ));
45+ $ router
46+ ->expects ($ this ->once ())
47+ ->method ('sortRouters ' )
48+ ->will (
49+ $ this ->returnValue (
50+ array ($ high , $ medium , $ low )
51+ )
52+ )
53+ ;
54+
55+ $ router ->add ($ low , 10 );
56+ $ router ->add ($ medium , 50 );
57+ $ router ->add ($ high , 100 );
58+ $ expectedSortedRouters = array ($ high , $ medium , $ low );
59+ // Let's get all routers 5 times, we should only sort once.
60+ for ($ i = 0 ; $ i < 5 ; ++$ i ) {
61+ $ this ->assertSame ($ expectedSortedRouters , $ router ->all ());
62+ }
63+ }
64+
65+ /**
66+ * This test ensures that if a router is being added on the fly, the sorting is reset.
67+ *
68+ * @covers \Symfony\Cmf\Component\Routing\ChainRouter::sortRouters
69+ * @covers \Symfony\Cmf\Component\Routing\ChainRouter::all
70+ * @covers \Symfony\Cmf\Component\Routing\ChainRouter::add
71+ */
72+ public function testReSortRouters ()
73+ {
74+ list ($ low , $ medium , $ high ) = $ this ->createRouterMocks ();
75+ $ highest = clone $ high ;
76+ // We're using a mock here and not $this->router because we need to ensure that the sorting operation is done only once.
77+ $ router = $ this ->buildMock ('Symfony \\Cmf \\Component \\Routing \\ChainRouter ' , array ('sortRouters ' ));
78+ $ router
79+ ->expects ($ this ->at (0 ))
80+ ->method ('sortRouters ' )
81+ ->will (
82+ $ this ->returnValue (
83+ array ($ high , $ medium , $ low )
84+ )
85+ )
86+ ;
87+ // The second time sortRouters() is called, we're supposed to get the newly added router ($highest)
88+ $ router
89+ ->expects ($ this ->at (1 ))
90+ ->method ('sortRouters ' )
91+ ->will (
92+ $ this ->returnValue (
93+ array ($ highest , $ high , $ medium , $ low )
94+ )
95+ )
96+ ;
97+
98+ $ router ->add ($ low , 10 );
99+ $ router ->add ($ medium , 50 );
100+ $ router ->add ($ high , 100 );
101+ $ this ->assertSame (array ($ high , $ medium , $ low ), $ router ->all ());
102+
103+ // Now adding another router on the fly, sorting must have been reset
104+ $ router ->add ($ highest , 101 );
105+ $ this ->assertSame (array ($ highest , $ high , $ medium , $ low ), $ router ->all ());
106+ }
107+
33108 /**
34109 * context must be propagated to chained routers and be stored locally
35110 */
0 commit comments