66use Tests \TestCase ;
77use Pterodactyl \Models \Server ;
88use Pterodactyl \Models \Location ;
9- use Illuminate \Contracts \Config \Repository ;
9+ use Illuminate \Support \Collection ;
10+ use Pterodactyl \Models \EggVariable ;
1011use Pterodactyl \Services \Servers \EnvironmentService ;
1112use Pterodactyl \Contracts \Repository \ServerRepositoryInterface ;
1213
1314class EnvironmentServiceTest extends TestCase
1415{
15- const CONFIG_MAPPING = 'pterodactyl.environment_variables ' ;
16-
17- /**
18- * @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
19- */
20- private $ config ;
21-
2216 /**
2317 * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
2418 */
@@ -30,9 +24,7 @@ class EnvironmentServiceTest extends TestCase
3024 public function setUp (): void
3125 {
3226 parent ::setUp ();
33-
34- $ this ->config = m::mock (Repository::class);
35- $ this ->repository = m::mock (ServerRepositoryInterface::class);
27+ config ()->set ('pterodactyl.environment_variables ' , []);
3628 }
3729
3830 /**
@@ -55,15 +47,17 @@ public function testSettingEnvironmentKeyPersistsItInArray()
5547 */
5648 public function testProcessShouldReturnDefaultEnvironmentVariablesForAServer ()
5749 {
58- $ model = $ this ->getServerModel ();
59- $ this ->config ->shouldReceive ('get ' )->with (self ::CONFIG_MAPPING , [])->once ()->andReturn ([]);
60- $ this ->repository ->shouldReceive ('getVariablesWithValues ' )->with ($ model ->id )->once ()->andReturn ([
61- 'TEST_VARIABLE ' => 'Test Variable ' ,
50+ $ model = $ this ->getServerModel ([
51+ 'TEST_VARIABLE ' => factory (EggVariable::class)->make ([
52+ 'id ' => 987 ,
53+ 'env_variable ' => 'TEST_VARIABLE ' ,
54+ 'default_value ' => 'Test Variable ' ,
55+ ]),
6256 ]);
6357
6458 $ response = $ this ->getService ()->handle ($ model );
6559 $ this ->assertNotEmpty ($ response );
66- $ this ->assertEquals (4 , count ( $ response) );
60+ $ this ->assertCount (4 , $ response );
6761 $ this ->assertArrayHasKey ('TEST_VARIABLE ' , $ response );
6862 $ this ->assertSame ('Test Variable ' , $ response ['TEST_VARIABLE ' ]);
6963 }
@@ -73,10 +67,7 @@ public function testProcessShouldReturnDefaultEnvironmentVariablesForAServer()
7367 */
7468 public function testProcessShouldReturnKeySetAtRuntime ()
7569 {
76- $ model = $ this ->getServerModel ();
77- $ this ->config ->shouldReceive ('get ' )->with (self ::CONFIG_MAPPING , [])->once ()->andReturn ([]);
78- $ this ->repository ->shouldReceive ('getVariablesWithValues ' )->with ($ model ->id )->once ()->andReturn ([]);
79-
70+ $ model = $ this ->getServerModel ([]);
8071 $ service = $ this ->getService ();
8172 $ service ->setEnvironmentKey ('TEST_VARIABLE ' , function ($ server ) {
8273 return $ server ->uuidShort ;
@@ -94,12 +85,11 @@ public function testProcessShouldReturnKeySetAtRuntime()
9485 */
9586 public function testProcessShouldAllowOverwritingVariablesWithConfigurationFile ()
9687 {
97- $ model = $ this ->getServerModel ();
98- $ this ->repository ->shouldReceive ('getVariablesWithValues ' )->with ($ model ->id )->once ()->andReturn ([]);
99- $ this ->config ->shouldReceive ('get ' )->with (self ::CONFIG_MAPPING , [])->once ()->andReturn ([
88+ config ()->set ('pterodactyl.environment_variables ' , [
10089 'P_SERVER_UUID ' => 'name ' ,
10190 ]);
10291
92+ $ model = $ this ->getServerModel ([]);
10393 $ response = $ this ->getService ()->handle ($ model );
10494
10595 $ this ->assertNotEmpty ($ response );
@@ -113,14 +103,13 @@ public function testProcessShouldAllowOverwritingVariablesWithConfigurationFile(
113103 */
114104 public function testVariablesSetInConfigurationAllowForClosures ()
115105 {
116- $ model = $ this ->getServerModel ();
117- $ this ->config ->shouldReceive ('get ' )->with (self ::CONFIG_MAPPING , [])->once ()->andReturn ([
106+ config ()->set ('pterodactyl.environment_variables ' , [
118107 'P_SERVER_UUID ' => function ($ server ) {
119108 return $ server ->id * 2 ;
120109 },
121110 ]);
122- $ this ->repository ->shouldReceive ('getVariablesWithValues ' )->with ($ model ->id )->once ()->andReturn ([]);
123111
112+ $ model = $ this ->getServerModel ([]);
124113 $ response = $ this ->getService ()->handle ($ model );
125114
126115 $ this ->assertNotEmpty ($ response );
@@ -135,12 +124,11 @@ public function testVariablesSetInConfigurationAllowForClosures()
135124 */
136125 public function testProcessShouldAllowOverwritingDefaultVariablesWithRuntimeProvided ()
137126 {
138- $ model = $ this ->getServerModel ();
139- $ this ->config ->shouldReceive ('get ' )->with (self ::CONFIG_MAPPING , [])->once ()->andReturn ([
127+ config ()->set ('pterodactyl.environment_variables ' , [
140128 'P_SERVER_UUID ' => 'overwritten-config ' ,
141129 ]);
142- $ this ->repository ->shouldReceive ('getVariablesWithValues ' )->with ($ model ->id )->once ()->andReturn ([]);
143130
131+ $ model = $ this ->getServerModel ([]);
144132 $ service = $ this ->getService ();
145133 $ service ->setEnvironmentKey ('P_SERVER_UUID ' , function ($ model ) {
146134 return 'overwritten ' ;
@@ -161,18 +149,25 @@ public function testProcessShouldAllowOverwritingDefaultVariablesWithRuntimeProv
161149 */
162150 private function getService (): EnvironmentService
163151 {
164- return new EnvironmentService ( $ this -> config , $ this -> repository ) ;
152+ return new EnvironmentService ;
165153 }
166154
167155 /**
168156 * Return a server model with a location relationship to be used in the tests.
169157 *
158+ * @param array $variables
170159 * @return \Pterodactyl\Models\Server
171160 */
172- private function getServerModel (): Server
161+ private function getServerModel (array $ variables ): Server
173162 {
174- return factory (Server::class)->make ([
163+ /** @var \Pterodactyl\Models\Server $server */
164+ $ server = factory (Server::class)->make ([
165+ 'id ' => 123 ,
175166 'location ' => factory (Location::class)->make (),
176167 ]);
168+
169+ $ server ->setRelation ('variables ' , Collection::make ($ variables ));
170+
171+ return $ server ;
177172 }
178173}
0 commit comments