1212use Exception ;
1313use Mockery as m ;
1414use Tests \TestCase ;
15- use Ramsey \Uuid \Uuid ;
1615use Pterodactyl \Models \Egg ;
16+ use Tests \Traits \MocksUuids ;
1717use Illuminate \Contracts \Config \Repository ;
18+ use Pterodactyl \Exceptions \PterodactylException ;
19+ use Pterodactyl \Services \Eggs \EggCreationService ;
1820use Pterodactyl \Contracts \Repository \EggRepositoryInterface ;
19- use Pterodactyl \Services \Services \Options \EggCreationService ;
20- use Pterodactyl \Exceptions \Service \ServiceOption \NoParentConfigurationFoundException ;
21+ use Pterodactyl \Exceptions \Service \Egg \NoParentConfigurationFoundException ;
2122
22- class OptionCreationServiceTest extends TestCase
23+ class EggCreationServiceTest extends TestCase
2324{
25+ use MocksUuids;
26+
2427 /**
2528 * @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
2629 */
@@ -32,15 +35,10 @@ class OptionCreationServiceTest extends TestCase
3235 protected $ repository ;
3336
3437 /**
35- * @var \Pterodactyl\Services\Services\Options \EggCreationService
38+ * @var \Pterodactyl\Services\Eggs \EggCreationService
3639 */
3740 protected $ service ;
3841
39- /**
40- * @var \Ramsey\Uuid\Uuid|\Mockery\Mock
41- */
42- protected $ uuid ;
43-
4442 /**
4543 * Setup tests.
4644 */
@@ -50,7 +48,6 @@ public function setUp()
5048
5149 $ this ->config = m::mock (Repository::class);
5250 $ this ->repository = m::mock (EggRepositoryInterface::class);
53- $ this ->uuid = m::mock ('overload: ' . Uuid::class);
5451
5552 $ this ->service = new EggCreationService ($ this ->config , $ this ->repository );
5653 }
@@ -60,80 +57,73 @@ public function setUp()
6057 */
6158 public function testCreateNewModelWithoutUsingConfigFrom ()
6259 {
63- $ model = factory (Egg::class)->make ([
64- 'tag ' => str_random (10 ),
65- ]);
60+ $ model = factory (Egg::class)->make ();
6661
6762 $ this ->config ->shouldReceive ('get ' )->with ('pterodactyl.service.author ' )->once ()->andReturn ('test@example.com ' );
68- $ this ->uuid ->shouldReceive ('uuid4->toString ' )->withNoArgs ()->once ()->andReturn ('uuid-string ' );
6963 $ this ->repository ->shouldReceive ('create ' )->with ([
70- 'name ' => $ model -> name ,
71- 'tag ' => 'test@example.com: ' . $ model -> tag ,
64+ 'uuid ' => $ this -> getKnownUuid () ,
65+ 'author ' => 'test@example.com ' ,
7266 'config_from ' => null ,
73- 'uuid ' => ' uuid-string ' ,
67+ 'name ' => $ model -> name ,
7468 ], true , true )->once ()->andReturn ($ model );
7569
76- $ response = $ this ->service ->handle (['name ' => $ model ->name , ' tag ' => $ model -> tag ]);
70+ $ response = $ this ->service ->handle (['name ' => $ model ->name ]);
7771
7872 $ this ->assertNotEmpty ($ response );
7973 $ this ->assertNull (object_get ($ response , 'config_from ' ));
8074 $ this ->assertEquals ($ model ->name , $ response ->name );
8175 }
8276
8377 /**
84- * Test that passing a bad tag into the function will set the correct tag .
78+ * Test that a new model is created when using the config from attribute .
8579 */
86- public function testCreateNewModelUsingLongTagForm ()
80+ public function testCreateNewModelUsingConfigFrom ()
8781 {
88- $ model = factory (Egg::class)->make ([
89- 'tag ' => 'test@example.com:tag ' ,
90- ]);
82+ $ model = factory (Egg::class)->make ();
83+
84+ $ this ->repository ->shouldReceive ('findCountWhere ' )->with ([
85+ ['nest_id ' , '= ' , $ model ->nest_id ],
86+ ['id ' , '= ' , 12345 ],
87+ ])->once ()->andReturn (1 );
9188
9289 $ this ->config ->shouldReceive ('get ' )->with ('pterodactyl.service.author ' )->once ()->andReturn ('test@example.com ' );
93- $ this ->uuid ->shouldReceive ('uuid4->toString ' )->withNoArgs ()->once ()->andReturn ('uuid-string ' );
9490 $ this ->repository ->shouldReceive ('create ' )->with ([
95- 'name ' => $ model ->name ,
96- 'config_from ' => null ,
97- 'tag ' => $ model -> tag ,
98- 'uuid ' => 'uuid-string ' ,
91+ 'nest_id ' => $ model ->nest_id ,
92+ 'config_from ' => 12345 ,
93+ 'uuid ' => $ this -> getKnownUuid () ,
94+ 'author ' => 'test@example.com ' ,
9995 ], true , true )->once ()->andReturn ($ model );
10096
101- $ response = $ this ->service ->handle (['name ' => $ model ->name , 'tag ' => 'bad@example.com:tag ' ]);
97+ $ response = $ this ->service ->handle ([
98+ 'nest_id ' => $ model ->nest_id ,
99+ 'config_from ' => 12345 ,
100+ ]);
102101
103102 $ this ->assertNotEmpty ($ response );
104- $ this ->assertNull (object_get ($ response , 'config_from ' ));
105- $ this ->assertEquals ($ model ->name , $ response ->name );
106- $ this ->assertEquals ('test@example.com:tag ' , $ response ->tag );
103+ $ this ->assertEquals ($ response , $ model );
107104 }
108105
109106 /**
110- * Test that a new model is created when using the config from attribute.
107+ * Test that certain data, such as the UUID or author takes priority over data
108+ * that is passed into the function.
111109 */
112- public function testCreateNewModelUsingConfigFrom ()
110+ public function testDataProvidedByHandlerTakesPriorityOverPassedData ()
113111 {
114112 $ model = factory (Egg::class)->make ();
115113
116- $ data = [
117- 'name ' => $ model ->name ,
118- 'service_id ' => $ model ->service_id ,
119- 'tag ' => 'test@example.com:tag ' ,
120- 'config_from ' => 1 ,
121- 'uuid ' => 'uuid-string ' ,
122- ];
123-
124- $ this ->repository ->shouldReceive ('findCountWhere ' )->with ([
125- ['service_id ' , '= ' , $ data ['service_id ' ]],
126- ['id ' , '= ' , $ data ['config_from ' ]],
127- ])->once ()->andReturn (1 );
128-
129114 $ this ->config ->shouldReceive ('get ' )->with ('pterodactyl.service.author ' )->once ()->andReturn ('test@example.com ' );
130- $ this ->uuid ->shouldReceive ('uuid4->toString ' )->withNoArgs ()->once ()->andReturn ('uuid-string ' );
131- $ this ->repository ->shouldReceive ('create ' )->with ($ data , true , true )->once ()->andReturn ($ model );
115+ $ this ->repository ->shouldReceive ('create ' )->with ([
116+ 'uuid ' => $ this ->getKnownUuid (),
117+ 'author ' => 'test@example.com ' ,
118+ 'config_from ' => null ,
119+ 'name ' => $ model ->name ,
120+ ], true , true )->once ()->andReturn ($ model );
132121
133- $ response = $ this ->service ->handle ($ data );
122+ $ response = $ this ->service ->handle ([ ' name ' => $ model -> name , ' uuid ' => ' should-be-ignored ' , ' author ' => ' should-be-ignored ' ] );
134123
135124 $ this ->assertNotEmpty ($ response );
136- $ this ->assertEquals ($ response , $ model );
125+ $ this ->assertNull (object_get ($ response , 'config_from ' ));
126+ $ this ->assertEquals ($ model ->name , $ response ->name );
137127 }
138128
139129 /**
@@ -142,15 +132,15 @@ public function testCreateNewModelUsingConfigFrom()
142132 public function testExceptionIsThrownIfNoParentConfigurationIsFound ()
143133 {
144134 $ this ->repository ->shouldReceive ('findCountWhere ' )->with ([
145- ['service_id ' , '= ' , null ],
135+ ['nest_id ' , '= ' , null ],
146136 ['id ' , '= ' , 1 ],
147137 ])->once ()->andReturn (0 );
148138
149139 try {
150140 $ this ->service ->handle (['config_from ' => 1 ]);
151- } catch (Exception $ exception ) {
141+ } catch (PterodactylException $ exception ) {
152142 $ this ->assertInstanceOf (NoParentConfigurationFoundException::class, $ exception );
153- $ this ->assertEquals (trans ('exceptions.service.options .must_be_child ' ), $ exception ->getMessage ());
143+ $ this ->assertEquals (trans ('exceptions.nest.egg .must_be_child ' ), $ exception ->getMessage ());
154144 }
155145 }
156146}
0 commit comments