1212use Exception ;
1313use Mockery as m ;
1414use Tests \TestCase ;
15+ use Ramsey \Uuid \Uuid ;
1516use Pterodactyl \Models \ServiceOption ;
17+ use Illuminate \Contracts \Config \Repository ;
1618use Pterodactyl \Services \Services \Options \OptionCreationService ;
1719use Pterodactyl \Contracts \Repository \ServiceOptionRepositoryInterface ;
1820use Pterodactyl \Exceptions \Service \ServiceOption \NoParentConfigurationFoundException ;
1921
2022class OptionCreationServiceTest extends TestCase
2123{
2224 /**
23- * @var \Pterodactyl\Models\ServiceOption
25+ * @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
2426 */
25- protected $ model ;
27+ protected $ config ;
2628
2729 /**
28- * @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface
30+ * @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface|\Mockery\Mock
2931 */
3032 protected $ repository ;
3133
@@ -34,57 +36,102 @@ class OptionCreationServiceTest extends TestCase
3436 */
3537 protected $ service ;
3638
39+ /**
40+ * @var \Ramsey\Uuid\Uuid|\Mockery\Mock
41+ */
42+ protected $ uuid ;
43+
3744 /**
3845 * Setup tests.
3946 */
4047 public function setUp ()
4148 {
4249 parent ::setUp ();
4350
44- $ this ->model = factory (ServiceOption ::class)-> make ( );
51+ $ this ->config = m:: mock (Repository ::class);
4552 $ this ->repository = m::mock (ServiceOptionRepositoryInterface::class);
53+ $ this ->uuid = m::mock ('overload: ' . Uuid::class);
4654
47- $ this ->service = new OptionCreationService ($ this ->repository );
55+ $ this ->service = new OptionCreationService ($ this ->config , $ this -> repository );
4856 }
4957
5058 /**
5159 * Test that a new model is created when not using the config from attribute.
5260 */
5361 public function testCreateNewModelWithoutUsingConfigFrom ()
5462 {
55- $ this ->repository ->shouldReceive ('create ' )->with (['name ' => $ this ->model ->name , 'config_from ' => null ])
56- ->once ()->andReturn ($ this ->model );
63+ $ model = factory (ServiceOption::class)->make ();
64+
65+ $ this ->config ->shouldReceive ('get ' )->with ('pterodactyl.service.author ' )->once ()->andReturn ('test@example.com ' );
66+ $ this ->uuid ->shouldReceive ('uuid4->toString ' )->withNoArgs ()->once ()->andReturn ('uuid-string ' );
67+ $ this ->repository ->shouldReceive ('create ' )->with ([
68+ 'name ' => $ model ->name ,
69+ 'config_from ' => null ,
70+ 'tag ' => 'test@example.com: ' . $ model ->tag ,
71+ 'uuid ' => 'uuid-string ' ,
72+ ], true , true )->once ()->andReturn ($ model );
5773
58- $ response = $ this ->service ->handle (['name ' => $ this -> model ->name ]);
74+ $ response = $ this ->service ->handle (['name ' => $ model -> name , ' tag ' => $ model ->tag ]);
5975
6076 $ this ->assertNotEmpty ($ response );
6177 $ this ->assertNull (object_get ($ response , 'config_from ' ));
62- $ this ->assertEquals ($ this ->model ->name , $ response ->name );
78+ $ this ->assertEquals ($ model ->name , $ response ->name );
79+ }
80+
81+ /**
82+ * Test that passing a bad tag into the function will set the correct tag.
83+ */
84+ public function testCreateNewModelUsingLongTagForm ()
85+ {
86+ $ model = factory (ServiceOption::class)->make ([
87+ 'tag ' => 'test@example.com:tag ' ,
88+ ]);
89+
90+ $ this ->config ->shouldReceive ('get ' )->with ('pterodactyl.service.author ' )->once ()->andReturn ('test@example.com ' );
91+ $ this ->uuid ->shouldReceive ('uuid4->toString ' )->withNoArgs ()->once ()->andReturn ('uuid-string ' );
92+ $ this ->repository ->shouldReceive ('create ' )->with ([
93+ 'name ' => $ model ->name ,
94+ 'config_from ' => null ,
95+ 'tag ' => $ model ->tag ,
96+ 'uuid ' => 'uuid-string ' ,
97+ ], true , true )->once ()->andReturn ($ model );
98+
99+ $ response = $ this ->service ->handle (['name ' => $ model ->name , 'tag ' => 'bad@example.com:tag ' ]);
100+
101+ $ this ->assertNotEmpty ($ response );
102+ $ this ->assertNull (object_get ($ response , 'config_from ' ));
103+ $ this ->assertEquals ($ model ->name , $ response ->name );
104+ $ this ->assertEquals ('test@example.com:tag ' , $ response ->tag );
63105 }
64106
65107 /**
66108 * Test that a new model is created when using the config from attribute.
67109 */
68110 public function testCreateNewModelUsingConfigFrom ()
69111 {
112+ $ model = factory (ServiceOption::class)->make ();
113+
70114 $ data = [
71- 'name ' => $ this ->model ->name ,
72- 'service_id ' => $ this ->model ->service_id ,
115+ 'name ' => $ model ->name ,
116+ 'service_id ' => $ model ->service_id ,
117+ 'tag ' => 'test@example.com:tag ' ,
73118 'config_from ' => 1 ,
119+ 'uuid ' => 'uuid-string ' ,
74120 ];
75121
76122 $ this ->repository ->shouldReceive ('findCountWhere ' )->with ([
77123 ['service_id ' , '= ' , $ data ['service_id ' ]],
78124 ['id ' , '= ' , $ data ['config_from ' ]],
79125 ])->once ()->andReturn (1 );
80126
81- $ this ->repository ->shouldReceive ('create ' )->with ($ data )
82- ->once ()->andReturn ($ this ->model );
127+ $ this ->config ->shouldReceive ('get ' )->with ('pterodactyl.service.author ' )->once ()->andReturn ('test@example.com ' );
128+ $ this ->uuid ->shouldReceive ('uuid4->toString ' )->withNoArgs ()->once ()->andReturn ('uuid-string ' );
129+ $ this ->repository ->shouldReceive ('create ' )->with ($ data , true , true )->once ()->andReturn ($ model );
83130
84131 $ response = $ this ->service ->handle ($ data );
85132
86133 $ this ->assertNotEmpty ($ response );
87- $ this ->assertEquals ($ response , $ this -> model );
134+ $ this ->assertEquals ($ response , $ model );
88135 }
89136
90137 /**
0 commit comments