TestVouchersController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\User;
  4. use App\Models\Voucher;
  5. use Illuminate\Foundation\Testing\DatabaseTransactions;
  6. use Illuminate\Support\Str;
  7. use Tests\TestCase;
  8. /**
  9. * Class TestUsefulLinksController
  10. * @package Tests\Feature
  11. */
  12. class TestVouchersController extends TestCase
  13. {
  14. use DatabaseTransactions;
  15. /**
  16. * @dataProvider accessibleRoutesDataProvider
  17. * @param string $method
  18. * @param string $route
  19. * @param int $expectedStatus
  20. */
  21. function test_accessible_routes(string $method, string $route, int $expectedStatus)
  22. {
  23. Voucher::factory()->create([
  24. 'id' => 1
  25. ]);
  26. $response = $this->actingAs(User::factory()->create([
  27. 'role' => 'admin',
  28. 'pterodactyl_id' => '1'
  29. ]))->{$method}($route);
  30. $response->assertStatus($expectedStatus);
  31. }
  32. /**
  33. * @dataProvider VoucherDataProvider
  34. * @param array $dataSet
  35. * @param int $expectedCount
  36. * @param bool $assertValidationErrors
  37. */
  38. function test_creating_vouchers(array $dataSet, int $expectedCount, bool $assertValidationErrors)
  39. {
  40. $response = $this->actingAs($this->getTestUser())->post(route('admin.vouchers.store'), $dataSet);
  41. if ($assertValidationErrors) $response->assertSessionHasErrors();
  42. else $response->assertSessionHasNoErrors();
  43. $response->assertRedirect();
  44. $this->assertDatabaseCount('vouchers', $expectedCount);
  45. }
  46. /**
  47. * @return User
  48. */
  49. private function getTestUser(): User
  50. {
  51. return User::factory()->create([
  52. 'role' => 'admin',
  53. 'pterodactyl_id' => '1'
  54. ]);
  55. }
  56. /**
  57. * @dataProvider VoucherDataProvider
  58. * @param array $dataSet
  59. * @param int $expectedCount
  60. * @param bool $assertValidationErrors
  61. */
  62. function test_updating_voucher(array $dataSet, int $expectedCount, bool $assertValidationErrors)
  63. {
  64. $voucher = Voucher::factory()->create([
  65. 'id' => 1
  66. ]);
  67. $response = $this->actingAs($this->getTestUser())->patch(route('admin.vouchers.update', $voucher->id), $dataSet);
  68. if ($assertValidationErrors) $response->assertSessionHasErrors();
  69. else $response->assertSessionHasNoErrors();
  70. $response->assertRedirect();
  71. $this->assertDatabaseCount('vouchers', 1);
  72. }
  73. /**
  74. *
  75. */
  76. function test_deleting_vouchers()
  77. {
  78. $voucher = Voucher::factory()->create([
  79. 'id' => 1
  80. ]);
  81. $response = $this->actingAs($this->getTestUser())->delete(route('admin.vouchers.update', $voucher->id));
  82. $response->assertRedirect();
  83. $this->assertDatabaseCount('vouchers', 0);
  84. }
  85. /**
  86. * @return array
  87. */
  88. function VoucherDataProvider(): array
  89. {
  90. return [
  91. 'Valid dataset 1' => [
  92. 'dataSet' => [
  93. "memo" => "TESTING",
  94. "code" => Str::random(20),
  95. "credits" => 500,
  96. "uses" => 500,
  97. "expires_at" => now()->addDay()->format('d-m-Y'),
  98. ],
  99. 'expectedCount' => 1,
  100. 'assertValidationErrors' => false
  101. ],
  102. 'Valid dataset 2' => [
  103. 'dataSet' => [
  104. "code" => Str::random(36),
  105. "credits" => 500,
  106. "uses" => 500,
  107. ],
  108. 'expectedCount' => 1,
  109. 'assertValidationErrors' => false
  110. ],
  111. 'Valid dataset 3' => [
  112. 'dataSet' => [
  113. "memo" => "TESTING",
  114. "code" => Str::random(4),
  115. "credits" => 1000000,
  116. "uses" => 1,
  117. "expires_at" => now()->addYears(6)->format('d-m-Y'),
  118. ],
  119. 'expectedCount' => 1,
  120. 'assertValidationErrors' => false
  121. ],
  122. 'Invalid dataset (memo to long)' => [
  123. 'dataSet' => [
  124. "memo" => Str::random(250),
  125. "code" => Str::random(20),
  126. "credits" => 500,
  127. "uses" => 500,
  128. "expires_at" => now()->addDay()->format('d-m-Y'),
  129. ],
  130. 'expectedCount' => 0,
  131. 'assertValidationErrors' => true
  132. ],
  133. 'Invalid dataset (code to short)' => [
  134. 'dataSet' => [
  135. "memo" => Str::random(250),
  136. "code" => Str::random(1),
  137. "credits" => 500,
  138. "uses" => 500,
  139. "expires_at" => now()->addDay()->format('d-m-Y'),
  140. ],
  141. 'expectedCount' => 0,
  142. 'assertValidationErrors' => true
  143. ],
  144. 'Invalid dataset (code missing)' => [
  145. 'dataSet' => [
  146. "memo" => Str::random(250),
  147. "credits" => 500,
  148. "uses" => 500,
  149. "expires_at" => now()->addDay()->format('d-m-Y'),
  150. ],
  151. 'expectedCount' => 0,
  152. 'assertValidationErrors' => true
  153. ],
  154. 'Invalid dataset (code to long)' => [
  155. 'dataSet' => [
  156. "memo" => Str::random(250),
  157. "code" => Str::random(60),
  158. "credits" => 500,
  159. "uses" => 500,
  160. "expires_at" => now()->addDay()->format('d-m-Y'),
  161. ],
  162. 'expectedCount' => 0,
  163. 'assertValidationErrors' => true
  164. ],
  165. 'Invalid dataset (credits missing)' => [
  166. 'dataSet' => [
  167. "memo" => Str::random(250),
  168. "code" => Str::random(1),
  169. "uses" => 500,
  170. "expires_at" => now()->addDay()->format('d-m-Y'),
  171. ],
  172. 'expectedCount' => 0,
  173. 'assertValidationErrors' => true
  174. ],
  175. 'Invalid dataset (0 credits)' => [
  176. 'dataSet' => [
  177. "memo" => Str::random(250),
  178. "code" => Str::random(1),
  179. "credits" => 0,
  180. "uses" => 500,
  181. "expires_at" => now()->addDay()->format('d-m-Y'),
  182. ],
  183. 'expectedCount' => 0,
  184. 'assertValidationErrors' => true
  185. ],
  186. 'Invalid dataset (to many credits)' => [
  187. 'dataSet' => [
  188. "memo" => Str::random(250),
  189. "code" => Str::random(1),
  190. "credits" => 99999999999,
  191. "uses" => 500,
  192. "expires_at" => now()->addDay()->format('d-m-Y'),
  193. ],
  194. 'expectedCount' => 0,
  195. 'assertValidationErrors' => true
  196. ],
  197. 'Invalid dataset (uses missing)' => [
  198. 'dataSet' => [
  199. "memo" => Str::random(250),
  200. "code" => Str::random(1),
  201. "credits" => 99999999999,
  202. "expires_at" => now()->addDay()->format('d-m-Y'),
  203. ],
  204. 'expectedCount' => 0,
  205. 'assertValidationErrors' => true
  206. ],
  207. 'Invalid dataset (0 uses)' => [
  208. 'dataSet' => [
  209. "memo" => Str::random(250),
  210. "code" => Str::random(1),
  211. "credits" => 99999999999,
  212. "uses" => 0,
  213. "expires_at" => now()->addDay()->format('d-m-Y'),
  214. ],
  215. 'expectedCount' => 0,
  216. 'assertValidationErrors' => true
  217. ],
  218. 'Invalid dataset (expires_at today)' => [
  219. 'dataSet' => [
  220. "memo" => Str::random(250),
  221. "code" => Str::random(1),
  222. "credits" => 99999999999,
  223. "uses" => 500,
  224. "expires_at" => now()->format('d-m-Y'),
  225. ],
  226. 'expectedCount' => 0,
  227. 'assertValidationErrors' => true
  228. ],
  229. 'Invalid dataset (expires_at earlier)' => [
  230. 'dataSet' => [
  231. "memo" => Str::random(250),
  232. "code" => Str::random(1),
  233. "credits" => 99999999999,
  234. "uses" => 500,
  235. "expires_at" => now()->subDays(5)->format('d-m-Y'),
  236. ],
  237. 'expectedCount' => 0,
  238. 'assertValidationErrors' => true
  239. ],
  240. 'Invalid dataset (expires_at to far)' => [
  241. 'dataSet' => [
  242. "memo" => Str::random(250),
  243. "code" => Str::random(1),
  244. "credits" => 99999999999,
  245. "uses" => 500,
  246. "expires_at" => now()->addYears(100)->format('d-m-Y'),
  247. ],
  248. 'expectedCount' => 0,
  249. 'assertValidationErrors' => true
  250. ],
  251. 'Invalid dataset (expires_at invalid format 1)' => [
  252. 'dataSet' => [
  253. "memo" => Str::random(250),
  254. "code" => Str::random(1),
  255. "credits" => 99999999999,
  256. "uses" => 500,
  257. "expires_at" => now()->addYears(100)->format('Y-m-d'),
  258. ],
  259. 'expectedCount' => 0,
  260. 'assertValidationErrors' => true
  261. ],
  262. 'Invalid dataset (expires_at invalid value)' => [
  263. 'dataSet' => [
  264. "memo" => Str::random(250),
  265. "code" => Str::random(1),
  266. "credits" => 99999999999,
  267. "uses" => 500,
  268. "expires_at" => Str::random(20),
  269. ],
  270. 'expectedCount' => 0,
  271. 'assertValidationErrors' => true
  272. ],
  273. ];
  274. }
  275. /**
  276. * @return array[]
  277. */
  278. public function accessibleRoutesDataProvider(): array
  279. {
  280. return [
  281. 'index page' => [
  282. 'method' => 'get',
  283. 'route' => '/admin/vouchers',
  284. 'expectedStatus' => 200
  285. ],
  286. 'Create page' => [
  287. 'method' => 'get',
  288. 'route' => '/admin/vouchers/create',
  289. 'expectedStatus' => 200
  290. ],
  291. 'Edit page' => [
  292. 'method' => 'get',
  293. 'route' => '/admin/vouchers/1/edit',
  294. 'expectedStatus' => 200
  295. ],
  296. ];
  297. }
  298. }