BiawakBakar

Unit test

Nov 24th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.44 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @noinspection PhpPossiblePolymorphicInvocationInspection
  4.  * @noinspection PhpUnhandledExceptionInspection
  5.  */
  6.  
  7. use App\Jobs\ValidateImportExcelJob;
  8. use App\Models\Instansi;
  9. use App\Services\ConfigService;
  10. use App\Services\Excel\ImportState;
  11. use App\Services\Operator\Sekolah\UploadService;
  12. use Illuminate\Database\Eloquent\Factories\Sequence;
  13.  
  14. use function Pest\Laravel\getJson;
  15. use function Pest\Laravel\postJson;
  16.  
  17. beforeEach(function () {
  18.     url()->forceRootUrl(url()->to('/sekolah'));
  19. });
  20.  
  21. test('tidak bisa akses jika tidak login', function () {
  22.     app('auth')->forgetUser();
  23.  
  24.     getJson('/')
  25.         ->assertUnauthorized();
  26. });
  27.  
  28. describe('index sekolah', function () {
  29.     test('bisa akses', function () {
  30.         /** @var Instansi\Sekolah $sekolah */
  31.         $sekolah = Instansi\Sekolah::factory()->create();
  32.  
  33.         getJson('/')
  34.             ->assertJsonCount(1, 'data')
  35.             ->assertJsonPath('data.0.id', $sekolah->id);
  36.     });
  37.  
  38.     test('bisa akses dengan filters', function (Instansi\Sekolah $sekolah, string $filter) {
  39.         $query = [
  40.             'filter' => [
  41.                 $filter => $sekolah->{$filter},
  42.             ],
  43.         ];
  44.  
  45.         getJson('/?' . http_build_query($query))
  46.             ->assertJsonCount(1, 'data')
  47.             ->assertJsonPath('data.0.id', $sekolah->id);
  48.     })->with([
  49.         'filter by k_jenjang' => [fn() => Instansi\Sekolah::factory()->create(), 'k_jenjang'],
  50.         'filter by npsn'      => [fn() => Instansi\Sekolah::factory()->create(), 'npsn'],
  51.         'filter by nama'      => [fn() => Instansi\Sekolah::factory()->create(), 'nama'],
  52.     ]);
  53. });
  54.  
  55. test('fetch sekolah', function () {
  56.     /** @var Instansi\Sekolah $sekolah */
  57.     $sekolah = Instansi\Sekolah::factory()->create();
  58.  
  59.     getJson("/$sekolah->id")
  60.         ->assertJsonPath('data.id', $sekolah->id);
  61. });
  62.  
  63. describe('create sekolah', function () {
  64.     test('initial create', function () {
  65.         getJson('/init-create')
  66.             ->assertSuccessful();
  67.     });
  68.  
  69.     test('validate create', function () {
  70.         /** @var Instansi\Sekolah $sekolah */
  71.         $sekolah = Instansi\Sekolah::factory([
  72.             'npsn'       => fake()->numerify('########'),
  73.             'dapodik_id' => fake()->numerify('########'),
  74.         ])->make();
  75.  
  76.         postJson('/validate-create', $sekolah->toArray())
  77.             ->assertSuccessful()
  78.             ->assertSuccess($sekolah->only([
  79.                 'nama',
  80.                 'k_jenjang',
  81.                 'npsn',
  82.                 'no_telp',
  83.                 'is_negeri',
  84.                 'k_propinsi',
  85.                 'k_kota',
  86.             ]));
  87.  
  88.         expect($sekolah->only([
  89.             'nama',
  90.         ]))
  91.             ->not()
  92.             ->toBeInDatabase('sekolah', 'instansi');
  93.     });
  94.  
  95.     test('create', function () {
  96.         /** @var Instansi\Sekolah $sekolah */
  97.         $sekolah = Instansi\Sekolah::factory([
  98.             'k_jenjang'  => fake()->randomElement([Instansi\MJenjang::SD, Instansi\MJenjang::SMP, Instansi\MJenjang::SMA, Instansi\MJenjang::SMK]),
  99.             'npsn'       => fake()->numerify('########'),
  100.             'dapodik_id' => fake()->numerify('########'),
  101.             'no_telp'    => fake()->phoneNumber,
  102.             'alamat'     => fake()->address,
  103.         ])->make()->makeHidden(['id', 'is_sbi', 'is_peserta', 'is_bayar',]);
  104.  
  105.         postJson('/create', $sekolah->toArray())
  106.             ->assertSuccessful()
  107.             ->assertSuccess($sekolah->toArray());
  108.  
  109.         expect($sekolah->toArray())
  110.             ->toBeInDatabase('sekolah', 'instansi');
  111.     });
  112. });
  113.  
  114. describe('set bayar sekolah', function () {
  115.     test('set bayar', function () {
  116.         $sekolah = Instansi\Sekolah::factory([
  117.             'is_bayar' => false,
  118.         ])->create();
  119.  
  120.         $data = [
  121.             'is_bayar' => true,
  122.         ];
  123.  
  124.         postJson("/$sekolah->id/set-bayar", $data)
  125.             ->assertSuccessful()
  126.             ->assertSuccess();
  127.  
  128.         expect($sekolah->fresh()->is_bayar)
  129.             ->toBeTrue();
  130.     });
  131.  
  132.     test('set belum bayar', function () {
  133.         $sekolah = Instansi\Sekolah::factory([
  134.             'is_bayar' => true,
  135.         ])->create();
  136.  
  137.         $data = [
  138.             'is_bayar' => false,
  139.         ];
  140.  
  141.         postJson("/$sekolah->id/set-bayar", $data)
  142.             ->assertSuccessful()
  143.             ->assertSuccess();
  144.  
  145.         expect($sekolah->fresh()->is_bayar)
  146.             ->toBeFalse();
  147.     });
  148. });
  149.  
  150. test('update', function () {
  151.     /** @var Instansi\Sekolah $sekolah */
  152.     $sekolah = Instansi\Sekolah::factory()->create();
  153.  
  154.     $data = Instansi\Sekolah::factory([
  155.         'npsn' => fake()->numerify('########'),
  156.     ])->make();
  157.  
  158.     postJson("/$sekolah->id/update", $data->toArray())
  159.         ->assertSuccessful()
  160.         ->assertSuccess($data->makeHidden('id')->toArray());
  161.  
  162.     expect($data->makeHidden('id')->toArray())
  163.         ->toBeInDatabase('sekolah', 'instansi');
  164. });
  165.  
  166. test('hapus sekolah', function () {
  167.     /** @var Instansi\Sekolah $sekolah */
  168.     $sekolah = Instansi\Sekolah::factory()->create();
  169.  
  170.     expect($sekolah->toArray())
  171.         ->toBeInDatabase('sekolah', 'instansi');
  172.  
  173.     postJson("/$sekolah->id/delete")
  174.         ->assertSuccessful()
  175.         ->assertSuccess();
  176.  
  177.     expect($sekolah->toArray())
  178.         ->not()
  179.         ->toBeInDatabase('sekolah', 'instansi')
  180.         ->and($sekolah->fresh())
  181.         ->toBeNull();
  182. });
  183.  
  184. test('request template upload', function () {
  185.     $cfgInstansi = app(ConfigService::class)->instansi($this->domain);
  186.  
  187.     /** @var Instansi\MKota $mKota */
  188.     $mKota = Instansi\MKota::query()
  189.         ->when(
  190.             $cfgInstansi->kKota,
  191.             fn($q) => $q->where('k_kota', '=', $cfgInstansi->kKota),
  192.             fn($q) => $q->where('k_propinsi', '=', $cfgInstansi->kPropinsi),
  193.         )
  194.         ->first();
  195.  
  196.     $totalSekolah = 5;
  197.     $sekolahs     = Instansi\Sekolah::factory($totalSekolah)
  198.         ->state(new Sequence(
  199.             fn($sequence) => ['urutan' => $sequence->index + 1],
  200.         ))
  201.         ->create([
  202.             'k_jenjang'  => Instansi\MJenjang::SMP,
  203.             'k_kota'     => $mKota->k_kota,
  204.             'k_propinsi' => $mKota->k_propinsi,
  205.         ]);
  206.  
  207.     $params = [
  208.         'k_jenjang' => Instansi\MJenjang::SMP,
  209.     ];
  210.  
  211.     $res = getJson("/template-upload?" . http_build_query($params))
  212.         ->assertSuccessful();
  213.  
  214.     /** @phpstan-ignore-next-line */
  215.     $buffers = readDataFromExcel($res->streamedContent());
  216.     expect($buffers)
  217.         ->toHaveCount($totalSekolah + 1); // 5 sekolah + header
  218.  
  219.     /** @var Instansi\Sekolah $sekolah */
  220.     $sekolah = $sekolahs->first();
  221.     $sekolah->load(['mKota', 'mKecamatan', 'mKelurahan']);
  222.  
  223.     expect($buffers[1])
  224.         ->toMatchArray([
  225.             $sekolah->urutan,
  226.             $sekolah->id,
  227.             $sekolah->siap_id,
  228.             $sekolah->dapodik_id,
  229.             $sekolah->npsn,
  230.             $sekolah->nama,
  231.             $sekolah->mJenjang->singkat,
  232.             $sekolah->is_negeri ? 'Negeri' : 'Swasta',
  233.             $sekolah->is_bayar ? 'Sudah' : 'Belum',
  234.             $sekolah->alamat,
  235.             $sekolah->no_telp,
  236.             $sekolah->mKota?->keterangan ?? null,
  237.             $sekolah->mKecamatan?->keterangan ?? null,
  238.             $sekolah->mKelurahan?->keterangan ?? null,
  239.             null,
  240.             $sekolah->no_rw,
  241.             $sekolah->no_rt,
  242.             $sekolah->latitude,
  243.             $sekolah->longitude,
  244.             $sekolah->label01,
  245.             $sekolah->label02,
  246.             $sekolah->label03,
  247.             $sekolah->label04,
  248.             $sekolah->label05,
  249.         ]);
  250. });
  251.  
  252. test('request init upload', function () {
  253.     $params = [
  254.         'k_jenjang' => Instansi\MJenjang::SMP,
  255.     ];
  256.  
  257.     $res = postJson("/init-upload?" . http_build_query($params))
  258.         ->assertSuccess()
  259.         ->json('data');
  260.  
  261.     expect($res)
  262.         ->toHaveKey('state')
  263.         ->state
  264.         ->toBe('idle');
  265. });
  266.  
  267. test('request validate upload', function () {
  268.     Queue::fake();
  269.     Storage::fake('excel-import');
  270.  
  271.     $cfgInstansi = app(ConfigService::class)->instansi($this->domain);
  272.     $rows        = [makeUploadSekolahHeader()];
  273.  
  274.  
  275.     /** @var Instansi\MKota $mKota */
  276.     $mKota = Instansi\MKota::query()
  277.         ->when(
  278.             $cfgInstansi->kKota,
  279.             fn($q) => $q->where('k_kota', '=', $cfgInstansi->kKota),
  280.             fn($q) => $q->where('k_propinsi', '=', $cfgInstansi->kPropinsi),
  281.         )
  282.         ->first();
  283.  
  284.     $totalSekolah = 5;
  285.     $sekolahs     = Instansi\Sekolah::factory($totalSekolah)
  286.         ->state(new Sequence(
  287.             fn($sequence) => ['urutan' => $sequence->index + 1],
  288.         ))
  289.         ->create([
  290.             'k_jenjang'  => Instansi\MJenjang::SMP,
  291.             'k_kota'     => $mKota->k_kota,
  292.             'k_propinsi' => $mKota->k_propinsi,
  293.         ]);
  294.  
  295.     foreach ($sekolahs as $sekolah) {
  296.         $rows[] = makeUploadSekolahData($sekolah);
  297.     }
  298.  
  299.     /** @phpstan-ignore-next-line */
  300.     $path = writeDataToExcel($rows);
  301.  
  302.     $filename = 'sekolah.xlsx';
  303.     $file     = new Illuminate\Http\Testing\File($filename, fopen($path, 'r'));
  304.  
  305.     $params = [
  306.         'k_jenjang' => Instansi\MJenjang::SMP,
  307.         'file'      => $file,
  308.     ];
  309.  
  310.     postJson("/validate-upload", $params)
  311.         ->assertSuccess();
  312.  
  313.     $key = implode('::', [
  314.         \App\Http\Controllers\Operator\SekolahController::class,
  315.         $params['k_jenjang'],
  316.         $this->domain,
  317.     ]);
  318.  
  319.     Queue::assertPushed(fn(ValidateImportExcelJob $job) => $job->key == $key);
  320.     Storage::disk('excel-import')->assertExists('/' . md5($key));
  321.  
  322.     expect(
  323.         [
  324.             'key'      => $key,
  325.             'filename' => $filename,
  326.             'state'    => ImportState::QueueValidate,
  327.             'progress' => null,
  328.         ])
  329.         ->toBeInDatabase('excel_import', 'instansi');
  330. });
  331.  
  332. test('request error upload', function () {
  333.     $cfgInstansi = app(ConfigService::class)->instansi($this->domain);
  334.     $rows        = [makeUploadSekolahHeader()];
  335.  
  336.  
  337.     /** @var Instansi\MKota $mKota */
  338.     $mKota = Instansi\MKota::query()
  339.         ->when(
  340.             $cfgInstansi->kKota,
  341.             fn($q) => $q->where('k_kota', '=', $cfgInstansi->kKota),
  342.             fn($q) => $q->where('k_propinsi', '=', $cfgInstansi->kPropinsi),
  343.         )
  344.         ->first();
  345.  
  346.     $kJenjang = Instansi\MJenjang::SMP;
  347.  
  348.     $totalSekolah = 5;
  349.     $sekolahs     = Instansi\Sekolah::factory($totalSekolah)
  350.         ->state(new Sequence(
  351.             fn($sequence) => ['urutan' => $sequence->index + 1],
  352.         ))
  353.         ->create([
  354.             'k_jenjang'  => $kJenjang,
  355.             'k_kota'     => $mKota->k_kota,
  356.             'k_propinsi' => $mKota->k_propinsi,
  357.         ]);
  358.  
  359.     foreach ($sekolahs as $sekolah) {
  360.         $rows[] = makeUploadSekolahData($sekolah);
  361.     }
  362.  
  363.     /** @var Instansi\ExcelImport $excel */
  364.     $excel = Instansi\ExcelImport::create([
  365.         "key"       => "App\Http\Controllers\Operator\SekolahController::class::$kJenjang::1",
  366.         "state"     => "validated",
  367.         "progress"  => null,
  368.         "filename"  => "sekolah.xlsx",
  369.         "handler"   => UploadService::class,
  370.         "params"    => [
  371.             "domain"   => "demo",
  372.             "kJenjang" => $kJenjang,
  373.         ],
  374.         "jml_data"  => 5,
  375.         "jml_valid" => 0,
  376.         "jml_error" => 10,
  377.     ]);
  378.  
  379.     foreach ($rows as $index => $row) {
  380.         Instansi\ExcelImportData::create([
  381.             'excel_import_id' => $excel->id,
  382.             "index"           => $index + 1,
  383.             "item"            => $index == 0
  384.                 ? array_values($row)
  385.                 : $row,
  386.             "attribute"       => null,
  387.             "is_error"        => true,
  388.             "message"         => [
  389.                 "singkat_jenjang"    => [
  390.                     "Singkat jenjang seharusnya XXX",
  391.                 ],
  392.                 "status_bayar" => [
  393.                     "Status bayar seharusnya Sudah atau Belum",
  394.                 ],
  395.             ],
  396.         ]);
  397.     }
  398.  
  399.     $params = [
  400.         'k_jenjang' => $kJenjang,
  401.     ];
  402.  
  403.     $res = postJson("/download-error", $params)
  404.         ->assertSuccessful();
  405.  
  406.     $buffers = readDataFromExcel($res->streamedContent());
  407.     expect($buffers)
  408.         ->toHaveCount($totalSekolah + 1);
  409. });
  410.  
  411. function makeUploadSekolahHeader(): array
  412. {
  413.     return [
  414.         'No', 'Kode', 'SIAP ID', 'DAPODIK ID', 'NPSN', 'Nama', 'Jenjang', 'Status', 'Bayar', 'Alamat', 'No Telp',
  415.         'Kota/Kab', 'Kecamatan', 'Kelurahan', 'Rayon', 'No RW', 'No RT', 'Lintang', 'Bujur', 'Label 01', 'Label 02',
  416.         'Label 03', 'Label 04', 'Label 05',
  417.     ];
  418. }
  419.  
  420. function makeUploadSekolahData(Instansi\Sekolah $sekolah): array
  421. {
  422.     return [
  423.         $sekolah->urutan,
  424.         $sekolah->id,
  425.         $sekolah->siap_id,
  426.         $sekolah->dapodik_id,
  427.         $sekolah->npsn,
  428.         $sekolah->nama,
  429.         $sekolah->mJenjang->singkat,
  430.         $sekolah->is_negeri ? 'Negeri' : 'Swasta',
  431.         $sekolah->is_bayar ? 'Sudah' : 'Belum',
  432.         $sekolah->alamat,
  433.         $sekolah->no_telp,
  434.         $sekolah->mKota?->keterangan ?? null,
  435.         $sekolah->mKecamatan?->keterangan ?? null,
  436.         $sekolah->mKelurahan?->keterangan ?? null,
  437.         null,
  438.         $sekolah->no_rw,
  439.         $sekolah->no_rt,
  440.         $sekolah->latitude,
  441.         $sekolah->longitude,
  442.         $sekolah->label01,
  443.         $sekolah->label02,
  444.         $sekolah->label03,
  445.         $sekolah->label04,
  446.         $sekolah->label05,
  447.     ];
  448. }
  449.  
Advertisement
Add Comment
Please, Sign In to add comment