99
1010namespace Tests \Unit \Services \Servers ;
1111
12- use Exception ;
1312use Mockery as m ;
1413use Tests \TestCase ;
15- use GuzzleHttp \Psr7 \Response ;
1614use Pterodactyl \Models \Server ;
17- use GuzzleHttp \Exception \RequestException ;
1815use Illuminate \Database \ConnectionInterface ;
16+ use Pterodactyl \Repositories \Eloquent \ServerRepository ;
1917use Pterodactyl \Services \Servers \ReinstallServerService ;
20- use Pterodactyl \Contracts \Repository \ServerRepositoryInterface ;
21- use Pterodactyl \Contracts \Repository \Daemon \ServerRepositoryInterface as DaemonServerRepositoryInterface ;
18+ use Pterodactyl \Repositories \Wings \DaemonServerRepository ;
2219
2320class ReinstallServerServiceTest extends TestCase
2421{
2522 /**
26- * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
23+ * @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
2724 */
28- protected $ daemonServerRepository ;
25+ private $ daemonServerRepository ;
2926
3027 /**
3128 * @var \Illuminate\Database\ConnectionInterface
3229 */
33- protected $ database ;
34-
35- /**
36- * @var \GuzzleHttp\Exception\RequestException
37- */
38- protected $ exception ;
30+ private $ connection ;
3931
4032 /**
4133 * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
4234 */
43- protected $ repository ;
44-
45- /**
46- * @var \Pterodactyl\Models\Server
47- */
48- protected $ server ;
49-
50- /**
51- * @var \Pterodactyl\Services\Servers\ReinstallServerService
52- */
53- protected $ service ;
35+ private $ repository ;
5436
5537 /**
5638 * Setup tests.
@@ -59,89 +41,42 @@ public function setUp(): void
5941 {
6042 parent ::setUp ();
6143
62- $ this ->daemonServerRepository = m::mock (DaemonServerRepositoryInterface::class);
63- $ this ->database = m::mock (ConnectionInterface::class);
64- $ this ->exception = m::mock (RequestException::class)->makePartial ();
65- $ this ->repository = m::mock (ServerRepositoryInterface::class);
66-
67- $ this ->server = factory (Server::class)->make (['node_id ' => 1 ]);
68-
69- $ this ->service = new ReinstallServerService (
70- $ this ->database ,
71- $ this ->daemonServerRepository ,
72- $ this ->repository
73- );
44+ $ this ->repository = m::mock (ServerRepository::class);
45+ $ this ->connection = m::mock (ConnectionInterface::class);
46+ $ this ->daemonServerRepository = m::mock (DaemonServerRepository::class);
7447 }
7548
7649 /**
7750 * Test that a server is reinstalled when it's model is passed to the function.
7851 */
7952 public function testServerShouldBeReinstalledWhenModelIsPassed ()
8053 {
81- $ this ->repository ->shouldNotReceive ('find ' );
54+ /** @var \Pterodactyl\Models\Server $server */
55+ $ server = factory (Server::class)->make (['id ' => 123 ]);
56+ $ updated = clone $ server ;
57+ $ updated ->installed = Server::STATUS_INSTALLING ;
8258
83- $ this ->database ->shouldReceive ('beginTransaction ' )->withNoArgs ()->once ()->andReturnNull ();
84- $ this ->repository ->shouldReceive ('withoutFreshModel->update ' )->with ($ this ->server ->id , [
85- 'installed ' => 0 ,
86- ], true , true )->once ()->andReturnNull ();
59+ $ this ->connection ->expects ('transaction ' )->with (m::on (function ($ closure ) use ($ updated ) {
60+ return $ closure () instanceof Server;
61+ }))->andReturn ($ updated );
8762
88- $ this ->daemonServerRepository -> shouldReceive ( ' setServer ' )->with ($ this -> server )-> once ()-> andReturnSelf ()
89- -> shouldReceive ( ' reinstall ' )-> withNoArgs ()-> once ()-> andReturn ( new Response );
90- $ this -> database -> shouldReceive ( ' commit ' )->withNoArgs ()-> once ()-> andReturnNull ( );
63+ $ this ->repository -> expects ( ' update ' )->with ($ server-> id , [
64+ ' installed ' => Server:: STATUS_INSTALLING ,
65+ ] )->andReturns ( $ updated );
9166
92- $ this ->service -> reinstall ( $ this -> server );
93- }
67+ $ this ->daemonServerRepository -> expects ( ' setServer ' )-> with ( $ server)-> andReturnSelf ( );
68+ $ this -> daemonServerRepository -> expects ( ' reinstall ' )-> withNoArgs ();
9469
95- /**
96- * Test that a server is reinstalled when the ID of the server is passed to the function.
97- */
98- public function testServerShouldBeReinstalledWhenServerIdIsPassed ()
99- {
100- $ this ->repository ->shouldReceive ('find ' )->with ($ this ->server ->id )->once ()->andReturn ($ this ->server );
101-
102- $ this ->database ->shouldReceive ('beginTransaction ' )->withNoArgs ()->once ()->andReturnNull ();
103- $ this ->repository ->shouldReceive ('withoutFreshModel->update ' )->with ($ this ->server ->id , [
104- 'installed ' => 0 ,
105- ], true , true )->once ()->andReturnNull ();
106-
107- $ this ->daemonServerRepository ->shouldReceive ('setServer ' )->with ($ this ->server )->once ()->andReturnSelf ()
108- ->shouldReceive ('reinstall ' )->withNoArgs ()->once ()->andReturn (new Response );
109- $ this ->database ->shouldReceive ('commit ' )->withNoArgs ()->once ()->andReturnNull ();
110-
111- $ this ->service ->reinstall ($ this ->server ->id );
112- }
113-
114- /**
115- * Test that an exception thrown by guzzle is rendered as a displayable exception.
116- *
117- * @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
118- */
119- public function testExceptionThrownByGuzzleShouldBeReRenderedAsDisplayable ()
120- {
121- $ this ->database ->shouldReceive ('beginTransaction ' )->withNoArgs ()->once ()->andReturnNull ();
122- $ this ->repository ->shouldReceive ('withoutFreshModel->update ' )->with ($ this ->server ->id , [
123- 'installed ' => 0 ,
124- ], true , true )->once ()->andReturnNull ();
125-
126- $ this ->daemonServerRepository ->shouldReceive ('setServer ' )->with ($ this ->server )->once ()->andThrow ($ this ->exception );
127-
128- $ this ->service ->reinstall ($ this ->server );
70+ $ this ->assertSame ($ updated , $ this ->getService ()->reinstall ($ server ));
12971 }
13072
13173 /**
132- * Test that an exception thrown by something other than guzzle is not transformed to a displayable.
133- *
134- * @expectedException \Exception
74+ * @return \Pterodactyl\Services\Servers\ReinstallServerService
13575 */
136- public function testExceptionNotThrownByGuzzleShouldNotBeTransformedToDisplayable ()
76+ private function getService ()
13777 {
138- $ this ->database ->shouldReceive ('beginTransaction ' )->withNoArgs ()->once ()->andReturnNull ();
139- $ this ->repository ->shouldReceive ('withoutFreshModel->update ' )->with ($ this ->server ->id , [
140- 'installed ' => 0 ,
141- ], true , true )->once ()->andReturnNull ();
142-
143- $ this ->daemonServerRepository ->shouldReceive ('setServer ' )->with ($ this ->server )->once ()->andThrow (new Exception ());
144-
145- $ this ->service ->reinstall ($ this ->server );
78+ return new ReinstallServerService (
79+ $ this ->connection , $ this ->daemonServerRepository , $ this ->repository
80+ );
14681 }
14782}
0 commit comments