77
88class MockBuilder
99{
10+ private const PRIORITY_ANY = 0 ;
11+ private const PRIORITY_EXACTLY = 10 ;
12+ private const PRIORITY_NTH = 100 ;
13+
1014 /** @var Expectation[] */
1115 private $ expectations = [];
1216
@@ -19,6 +23,9 @@ class MockBuilder
1923 /** @var ExtractorFactory */
2024 private $ extractorFactory ;
2125
26+ /** @var int */
27+ private $ priority ;
28+
2229 public function __construct (MatcherFactory $ matcherFactory , ExtractorFactory $ extractorFactory )
2330 {
2431 $ this ->matcherFactory = $ matcherFactory ;
@@ -46,6 +53,32 @@ public function exactly($times)
4653 $ this ->limiter = static function ($ runs ) use ($ times ) {
4754 return $ runs < $ times ;
4855 };
56+ $ this ->priority = self ::PRIORITY_EXACTLY ;
57+
58+ return $ this ;
59+ }
60+
61+ public function first ()
62+ {
63+ return $ this ->nth (1 );
64+ }
65+
66+ public function second ()
67+ {
68+ return $ this ->nth (2 );
69+ }
70+
71+ public function third ()
72+ {
73+ return $ this ->nth (3 );
74+ }
75+
76+ public function nth ($ position )
77+ {
78+ $ this ->limiter = static function ($ runs ) use ($ position ) {
79+ return $ runs === ($ position - 1 );
80+ };
81+ $ this ->priority = $ position * self ::PRIORITY_NTH ;
4982
5083 return $ this ;
5184 }
@@ -55,14 +88,21 @@ public function any()
5588 $ this ->limiter = static function () {
5689 return true ;
5790 };
91+ $ this ->priority = self ::PRIORITY_ANY ;
5892
5993 return $ this ;
6094 }
6195
6296 /** @return Expectation */
6397 public function when ()
6498 {
65- $ this ->expectations [] = new Expectation ($ this , $ this ->matcherFactory , $ this ->extractorFactory , $ this ->limiter );
99+ $ this ->expectations [] = new Expectation (
100+ $ this ,
101+ $ this ->matcherFactory ,
102+ $ this ->extractorFactory ,
103+ $ this ->limiter ,
104+ $ this ->priority
105+ );
66106
67107 $ this ->any ();
68108
@@ -74,6 +114,13 @@ public function flushExpectations()
74114 $ expectations = $ this ->expectations ;
75115 $ this ->expectations = [];
76116
117+ usort (
118+ $ expectations ,
119+ static function (Expectation $ left , Expectation $ right ): int {
120+ return $ left ->getPriority () <=> $ right ->getPriority ();
121+ }
122+ );
123+
77124 return $ expectations ;
78125 }
79126}
0 commit comments