TestVouchersController.php 11 KB

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