1313
1414namespace Tarantool \PhpUnit \Client ;
1515
16- use Prophecy \Argument ;
17- use Prophecy \Argument \Token \TokenInterface ;
18- use Prophecy \Prophecy \ObjectProphecy ;
19- use Prophecy \Prophet ;
16+ use PHPUnit \Framework \Constraint \Constraint ;
17+ use PHPUnit \Framework \MockObject \MockObject ;
18+ use PHPUnit \Framework \TestCase ;
2019use Tarantool \Client \Client ;
2120use Tarantool \Client \Connection \Connection ;
2221use Tarantool \Client \Handler \Handler ;
2625
2726final class MockClientBuilder
2827{
29- /** @var \Closure */
30- private $ prophesize ;
28+ /** @var TestCase */
29+ private $ testCase ;
3130
3231 /** @var \SplObjectStorage<object, array<int, Response>> */
3332 private $ requests ;
@@ -38,21 +37,23 @@ final class MockClientBuilder
3837 /** @var Packer|null */
3938 private $ packer ;
4039
41- public function __construct (\ Closure $ prophesize )
40+ public function __construct (TestCase $ testCase )
4241 {
43- $ this ->prophesize = $ prophesize ;
42+ $ this ->testCase = $ testCase ;
4443 $ this ->requests = new \SplObjectStorage ();
4544 }
4645
4746 public static function buildDefault () : Client
4847 {
49- $ self = new self (\Closure::fromCallable ([new Prophet (), 'prophesize ' ]));
48+ /** @psalm-suppress PropertyNotSetInConstructor */
49+ $ self = new self (new class () extends TestCase {
50+ });
5051
5152 return $ self ->build ();
5253 }
5354
5455 /**
55- * @param Request|TokenInterface $request
56+ * @param Request|Constraint $request
5657 * @param Response ...$responses
5758 */
5859 public function shouldHandle ($ request , ...$ responses ) : self
@@ -79,63 +80,77 @@ public function willUsePacker(Packer $packer) : self
7980 public function build () : Client
8081 {
8182 /** @var Handler $handler */
82- $ handler = $ this ->createHandler ()-> reveal () ;
83+ $ handler = $ this ->createHandler ();
8384
8485 return new Client ($ handler );
8586 }
8687
87- private function createHandler () : ObjectProphecy
88+ private function createHandler () : MockObject
8889 {
89- $ handler = ( $ this ->prophesize ) (Handler::class);
90+ $ handler = $ this ->createMock (Handler::class);
9091
9192 $ connection = $ this ->createConnection ();
92- $ handler ->getConnection ( )->willReturn ($ connection );
93+ $ handler ->method ( ' getConnection ' )->willReturn ($ connection );
9394
9495 $ packer = $ this ->createPacker ();
95- $ handler ->getPacker ( )->willReturn ($ packer );
96+ $ handler ->method ( ' getPacker ' )->willReturn ($ packer );
9697
9798 $ defaultResponse = DummyFactory::createEmptyResponse ();
9899
99100 if (!$ this ->requests ->count ()) {
100- $ handler ->handle (Argument:: type (Request::class) )->willReturn ($ defaultResponse );
101+ $ handler ->method ( ' handle ' )->willReturn ($ defaultResponse );
101102
102103 return $ handler ;
103104 }
104105
105106 foreach ($ this ->requests as $ request ) {
106107 if (!$ responses = $ this ->requests ->getInfo ()) {
107- $ handler ->handle ($ request )->willReturn ($ defaultResponse );
108+ $ handler ->method ( ' handle ' )-> with ($ request )->willReturn ($ defaultResponse );
108109 continue ;
109110 }
110111
111- $ handler ->handle ($ request )->willReturn (...$ responses )
112- ->shouldBeCalledTimes (\count ($ responses ));
112+ $ handler ->expects (TestCase::exactly (\count ($ responses )))
113+ ->method ('handle ' )->with ($ request )
114+ ->willReturnOnConsecutiveCalls (...$ responses );
113115 }
114116
115117 return $ handler ;
116118 }
117119
118120 /**
119- * @return Connection|ObjectProphecy
121+ * @return Connection|MockObject
120122 */
121123 private function createConnection ()
122124 {
123125 if ($ this ->connection ) {
124126 return $ this ->connection ;
125127 }
126128
127- return ( $ this ->prophesize ) (Connection::class);
129+ return $ this ->createMock (Connection::class);
128130 }
129131
130132 /**
131- * @return Packer|ObjectProphecy
133+ * @return Packer|MockObject
132134 */
133135 private function createPacker ()
134136 {
135137 if ($ this ->packer ) {
136138 return $ this ->packer ;
137139 }
138140
139- return ($ this ->prophesize )(Packer::class);
141+ return $ this ->createMock (Packer::class);
142+ }
143+
144+ /**
145+ * @param class-string $originalClassName
146+ */
147+ private function createMock (string $ originalClassName ) : MockObject
148+ {
149+ return $ this ->testCase ->getMockBuilder ($ originalClassName )
150+ ->disableOriginalConstructor ()
151+ ->disableOriginalClone ()
152+ ->disableArgumentCloning ()
153+ ->disallowMockingUnknownTypes ()
154+ ->getMock ();
140155 }
141156}
0 commit comments