44
55use Mockery as m ;
66use Tests \TestCase ;
7+ use BadMethodCallException ;
78use Pterodactyl \Models \EggVariable ;
9+ use Illuminate \Contracts \Validation \Factory ;
810use Pterodactyl \Services \Eggs \Variables \VariableCreationService ;
911use Pterodactyl \Contracts \Repository \EggVariableRepositoryInterface ;
1012
@@ -13,12 +15,12 @@ class VariableCreationServiceTest extends TestCase
1315 /**
1416 * @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
1517 */
16- protected $ repository ;
18+ private $ repository ;
1719
1820 /**
19- * @var \Pterodactyl\Services\Eggs\Variables\VariableCreationService
21+ * @var \Illuminate\Contracts\Validation\Factory|\Mockery\Mock
2022 */
21- protected $ service ;
23+ private $ validator ;
2224
2325 /**
2426 * Setup tests.
@@ -28,8 +30,7 @@ public function setUp()
2830 parent ::setUp ();
2931
3032 $ this ->repository = m::mock (EggVariableRepositoryInterface::class);
31-
32- $ this ->service = new VariableCreationService ($ this ->repository );
33+ $ this ->validator = m::mock (Factory::class);
3334 }
3435
3536 /**
@@ -46,7 +47,7 @@ public function testVariableIsCreatedAndStored()
4647 'env_variable ' => 'TEST_VAR_123 ' ,
4748 ]))->once ()->andReturn (new EggVariable );
4849
49- $ this ->assertInstanceOf (EggVariable::class, $ this ->service ->handle (1 , $ data ));
50+ $ this ->assertInstanceOf (EggVariable::class, $ this ->getService () ->handle (1 , $ data ));
5051 }
5152
5253 /**
@@ -62,7 +63,7 @@ public function testOptionsPassedInArrayKeyAreParsedProperly()
6263 'env_variable ' => 'TEST_VAR_123 ' ,
6364 ]))->once ()->andReturn (new EggVariable );
6465
65- $ this ->assertInstanceOf (EggVariable::class, $ this ->service ->handle (1 , $ data ));
66+ $ this ->assertInstanceOf (EggVariable::class, $ this ->getService () ->handle (1 , $ data ));
6667 }
6768
6869 /**
@@ -81,18 +82,20 @@ public function testNullOptionValueIsPassedAsArray()
8182 'user_editable ' => false ,
8283 ]))->once ()->andReturn (new EggVariable );
8384
84- $ this ->assertInstanceOf (EggVariable::class, $ this ->service ->handle (1 , $ data ));
85+ $ this ->assertInstanceOf (EggVariable::class, $ this ->getService () ->handle (1 , $ data ));
8586 }
8687
8788 /**
8889 * Test that all of the reserved variables defined in the model trigger an exception.
8990 *
91+ * @param string $variable
92+ *
9093 * @dataProvider reservedNamesProvider
9194 * @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
9295 */
9396 public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames (string $ variable )
9497 {
95- $ this ->service ->handle (1 , ['env_variable ' => $ variable ]);
98+ $ this ->getService () ->handle (1 , ['env_variable ' => $ variable ]);
9699 }
97100
98101 /**
@@ -106,7 +109,49 @@ public function testEggIdPassedInDataIsNotApplied()
106109 'egg_id ' => 1 ,
107110 ]))->once ()->andReturn (new EggVariable );
108111
109- $ this ->assertInstanceOf (EggVariable::class, $ this ->service ->handle (1 , $ data ));
112+ $ this ->assertInstanceOf (EggVariable::class, $ this ->getService ()->handle (1 , $ data ));
113+ }
114+
115+ /**
116+ * Test that validation errors due to invalid rules are caught and handled properly.
117+ *
118+ * @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException
119+ * @expectedExceptionMessage The validation rule "hodor_door" is not a valid rule for this application.
120+ */
121+ public function testInvalidValidationRulesResultInException ()
122+ {
123+ $ data = ['env_variable ' => 'TEST_VAR_123 ' , 'rules ' => 'string|hodorDoor ' ];
124+
125+ $ this ->validator ->shouldReceive ('make ' )->once ()
126+ ->with (['__TEST ' => 'test ' ], ['__TEST ' => 'string|hodorDoor ' ])
127+ ->andReturnSelf ();
128+
129+ $ this ->validator ->shouldReceive ('fails ' )->once ()
130+ ->withNoArgs ()
131+ ->andThrow (new BadMethodCallException ('Method [validateHodorDoor] does not exist. ' ));
132+
133+ $ this ->getService ()->handle (1 , $ data );
134+ }
135+
136+ /**
137+ * Test that an exception not stemming from a bad rule is not caught.
138+ *
139+ * @expectedException \BadMethodCallException
140+ * @expectedExceptionMessage Received something, but no expectations were specified.
141+ */
142+ public function testExceptionNotCausedByBadRuleIsNotCaught ()
143+ {
144+ $ data = ['env_variable ' => 'TEST_VAR_123 ' , 'rules ' => 'string ' ];
145+
146+ $ this ->validator ->shouldReceive ('make ' )->once ()
147+ ->with (['__TEST ' => 'test ' ], ['__TEST ' => 'string ' ])
148+ ->andReturnSelf ();
149+
150+ $ this ->validator ->shouldReceive ('fails ' )->once ()
151+ ->withNoArgs ()
152+ ->andThrow (new BadMethodCallException ('Received something, but no expectations were specified. ' ));
153+
154+ $ this ->getService ()->handle (1 , $ data );
110155 }
111156
112157 /**
@@ -124,4 +169,14 @@ public function reservedNamesProvider()
124169
125170 return $ data ;
126171 }
172+
173+ /**
174+ * Return an instance of the service with mocked dependencies for testing.
175+ *
176+ * @return \Pterodactyl\Services\Eggs\Variables\VariableCreationService
177+ */
178+ private function getService (): VariableCreationService
179+ {
180+ return new VariableCreationService ($ this ->repository , $ this ->validator );
181+ }
127182}
0 commit comments