Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @noinspection PhpPossiblePolymorphicInvocationInspection
- * @noinspection PhpUnhandledExceptionInspection
- */
- use App\Jobs\ValidateImportExcelJob;
- use App\Models\Instansi;
- use App\Services\ConfigService;
- use App\Services\Excel\ImportState;
- use App\Services\Operator\Sekolah\UploadService;
- use Illuminate\Database\Eloquent\Factories\Sequence;
- use function Pest\Laravel\getJson;
- use function Pest\Laravel\postJson;
- beforeEach(function () {
- url()->forceRootUrl(url()->to('/sekolah'));
- });
- test('tidak bisa akses jika tidak login', function () {
- app('auth')->forgetUser();
- getJson('/')
- ->assertUnauthorized();
- });
- describe('index sekolah', function () {
- test('bisa akses', function () {
- /** @var Instansi\Sekolah $sekolah */
- $sekolah = Instansi\Sekolah::factory()->create();
- getJson('/')
- ->assertJsonCount(1, 'data')
- ->assertJsonPath('data.0.id', $sekolah->id);
- });
- test('bisa akses dengan filters', function (Instansi\Sekolah $sekolah, string $filter) {
- $query = [
- 'filter' => [
- $filter => $sekolah->{$filter},
- ],
- ];
- getJson('/?' . http_build_query($query))
- ->assertJsonCount(1, 'data')
- ->assertJsonPath('data.0.id', $sekolah->id);
- })->with([
- 'filter by k_jenjang' => [fn() => Instansi\Sekolah::factory()->create(), 'k_jenjang'],
- 'filter by npsn' => [fn() => Instansi\Sekolah::factory()->create(), 'npsn'],
- 'filter by nama' => [fn() => Instansi\Sekolah::factory()->create(), 'nama'],
- ]);
- });
- test('fetch sekolah', function () {
- /** @var Instansi\Sekolah $sekolah */
- $sekolah = Instansi\Sekolah::factory()->create();
- getJson("/$sekolah->id")
- ->assertJsonPath('data.id', $sekolah->id);
- });
- describe('create sekolah', function () {
- test('initial create', function () {
- getJson('/init-create')
- ->assertSuccessful();
- });
- test('validate create', function () {
- /** @var Instansi\Sekolah $sekolah */
- $sekolah = Instansi\Sekolah::factory([
- 'npsn' => fake()->numerify('########'),
- 'dapodik_id' => fake()->numerify('########'),
- ])->make();
- postJson('/validate-create', $sekolah->toArray())
- ->assertSuccessful()
- ->assertSuccess($sekolah->only([
- 'nama',
- 'k_jenjang',
- 'npsn',
- 'no_telp',
- 'is_negeri',
- 'k_propinsi',
- 'k_kota',
- ]));
- expect($sekolah->only([
- 'nama',
- ]))
- ->not()
- ->toBeInDatabase('sekolah', 'instansi');
- });
- test('create', function () {
- /** @var Instansi\Sekolah $sekolah */
- $sekolah = Instansi\Sekolah::factory([
- 'k_jenjang' => fake()->randomElement([Instansi\MJenjang::SD, Instansi\MJenjang::SMP, Instansi\MJenjang::SMA, Instansi\MJenjang::SMK]),
- 'npsn' => fake()->numerify('########'),
- 'dapodik_id' => fake()->numerify('########'),
- 'no_telp' => fake()->phoneNumber,
- 'alamat' => fake()->address,
- ])->make()->makeHidden(['id', 'is_sbi', 'is_peserta', 'is_bayar',]);
- postJson('/create', $sekolah->toArray())
- ->assertSuccessful()
- ->assertSuccess($sekolah->toArray());
- expect($sekolah->toArray())
- ->toBeInDatabase('sekolah', 'instansi');
- });
- });
- describe('set bayar sekolah', function () {
- test('set bayar', function () {
- $sekolah = Instansi\Sekolah::factory([
- 'is_bayar' => false,
- ])->create();
- $data = [
- 'is_bayar' => true,
- ];
- postJson("/$sekolah->id/set-bayar", $data)
- ->assertSuccessful()
- ->assertSuccess();
- expect($sekolah->fresh()->is_bayar)
- ->toBeTrue();
- });
- test('set belum bayar', function () {
- $sekolah = Instansi\Sekolah::factory([
- 'is_bayar' => true,
- ])->create();
- $data = [
- 'is_bayar' => false,
- ];
- postJson("/$sekolah->id/set-bayar", $data)
- ->assertSuccessful()
- ->assertSuccess();
- expect($sekolah->fresh()->is_bayar)
- ->toBeFalse();
- });
- });
- test('update', function () {
- /** @var Instansi\Sekolah $sekolah */
- $sekolah = Instansi\Sekolah::factory()->create();
- $data = Instansi\Sekolah::factory([
- 'npsn' => fake()->numerify('########'),
- ])->make();
- postJson("/$sekolah->id/update", $data->toArray())
- ->assertSuccessful()
- ->assertSuccess($data->makeHidden('id')->toArray());
- expect($data->makeHidden('id')->toArray())
- ->toBeInDatabase('sekolah', 'instansi');
- });
- test('hapus sekolah', function () {
- /** @var Instansi\Sekolah $sekolah */
- $sekolah = Instansi\Sekolah::factory()->create();
- expect($sekolah->toArray())
- ->toBeInDatabase('sekolah', 'instansi');
- postJson("/$sekolah->id/delete")
- ->assertSuccessful()
- ->assertSuccess();
- expect($sekolah->toArray())
- ->not()
- ->toBeInDatabase('sekolah', 'instansi')
- ->and($sekolah->fresh())
- ->toBeNull();
- });
- test('request template upload', function () {
- $cfgInstansi = app(ConfigService::class)->instansi($this->domain);
- /** @var Instansi\MKota $mKota */
- $mKota = Instansi\MKota::query()
- ->when(
- $cfgInstansi->kKota,
- fn($q) => $q->where('k_kota', '=', $cfgInstansi->kKota),
- fn($q) => $q->where('k_propinsi', '=', $cfgInstansi->kPropinsi),
- )
- ->first();
- $totalSekolah = 5;
- $sekolahs = Instansi\Sekolah::factory($totalSekolah)
- ->state(new Sequence(
- fn($sequence) => ['urutan' => $sequence->index + 1],
- ))
- ->create([
- 'k_jenjang' => Instansi\MJenjang::SMP,
- 'k_kota' => $mKota->k_kota,
- 'k_propinsi' => $mKota->k_propinsi,
- ]);
- $params = [
- 'k_jenjang' => Instansi\MJenjang::SMP,
- ];
- $res = getJson("/template-upload?" . http_build_query($params))
- ->assertSuccessful();
- /** @phpstan-ignore-next-line */
- $buffers = readDataFromExcel($res->streamedContent());
- expect($buffers)
- ->toHaveCount($totalSekolah + 1); // 5 sekolah + header
- /** @var Instansi\Sekolah $sekolah */
- $sekolah = $sekolahs->first();
- $sekolah->load(['mKota', 'mKecamatan', 'mKelurahan']);
- expect($buffers[1])
- ->toMatchArray([
- $sekolah->urutan,
- $sekolah->id,
- $sekolah->siap_id,
- $sekolah->dapodik_id,
- $sekolah->npsn,
- $sekolah->nama,
- $sekolah->mJenjang->singkat,
- $sekolah->is_negeri ? 'Negeri' : 'Swasta',
- $sekolah->is_bayar ? 'Sudah' : 'Belum',
- $sekolah->alamat,
- $sekolah->no_telp,
- $sekolah->mKota?->keterangan ?? null,
- $sekolah->mKecamatan?->keterangan ?? null,
- $sekolah->mKelurahan?->keterangan ?? null,
- null,
- $sekolah->no_rw,
- $sekolah->no_rt,
- $sekolah->latitude,
- $sekolah->longitude,
- $sekolah->label01,
- $sekolah->label02,
- $sekolah->label03,
- $sekolah->label04,
- $sekolah->label05,
- ]);
- });
- test('request init upload', function () {
- $params = [
- 'k_jenjang' => Instansi\MJenjang::SMP,
- ];
- $res = postJson("/init-upload?" . http_build_query($params))
- ->assertSuccess()
- ->json('data');
- expect($res)
- ->toHaveKey('state')
- ->state
- ->toBe('idle');
- });
- test('request validate upload', function () {
- Queue::fake();
- Storage::fake('excel-import');
- $cfgInstansi = app(ConfigService::class)->instansi($this->domain);
- $rows = [makeUploadSekolahHeader()];
- /** @var Instansi\MKota $mKota */
- $mKota = Instansi\MKota::query()
- ->when(
- $cfgInstansi->kKota,
- fn($q) => $q->where('k_kota', '=', $cfgInstansi->kKota),
- fn($q) => $q->where('k_propinsi', '=', $cfgInstansi->kPropinsi),
- )
- ->first();
- $totalSekolah = 5;
- $sekolahs = Instansi\Sekolah::factory($totalSekolah)
- ->state(new Sequence(
- fn($sequence) => ['urutan' => $sequence->index + 1],
- ))
- ->create([
- 'k_jenjang' => Instansi\MJenjang::SMP,
- 'k_kota' => $mKota->k_kota,
- 'k_propinsi' => $mKota->k_propinsi,
- ]);
- foreach ($sekolahs as $sekolah) {
- $rows[] = makeUploadSekolahData($sekolah);
- }
- /** @phpstan-ignore-next-line */
- $path = writeDataToExcel($rows);
- $filename = 'sekolah.xlsx';
- $file = new Illuminate\Http\Testing\File($filename, fopen($path, 'r'));
- $params = [
- 'k_jenjang' => Instansi\MJenjang::SMP,
- 'file' => $file,
- ];
- postJson("/validate-upload", $params)
- ->assertSuccess();
- $key = implode('::', [
- \App\Http\Controllers\Operator\SekolahController::class,
- $params['k_jenjang'],
- $this->domain,
- ]);
- Queue::assertPushed(fn(ValidateImportExcelJob $job) => $job->key == $key);
- Storage::disk('excel-import')->assertExists('/' . md5($key));
- expect(
- [
- 'key' => $key,
- 'filename' => $filename,
- 'state' => ImportState::QueueValidate,
- 'progress' => null,
- ])
- ->toBeInDatabase('excel_import', 'instansi');
- });
- test('request error upload', function () {
- $cfgInstansi = app(ConfigService::class)->instansi($this->domain);
- $rows = [makeUploadSekolahHeader()];
- /** @var Instansi\MKota $mKota */
- $mKota = Instansi\MKota::query()
- ->when(
- $cfgInstansi->kKota,
- fn($q) => $q->where('k_kota', '=', $cfgInstansi->kKota),
- fn($q) => $q->where('k_propinsi', '=', $cfgInstansi->kPropinsi),
- )
- ->first();
- $kJenjang = Instansi\MJenjang::SMP;
- $totalSekolah = 5;
- $sekolahs = Instansi\Sekolah::factory($totalSekolah)
- ->state(new Sequence(
- fn($sequence) => ['urutan' => $sequence->index + 1],
- ))
- ->create([
- 'k_jenjang' => $kJenjang,
- 'k_kota' => $mKota->k_kota,
- 'k_propinsi' => $mKota->k_propinsi,
- ]);
- foreach ($sekolahs as $sekolah) {
- $rows[] = makeUploadSekolahData($sekolah);
- }
- /** @var Instansi\ExcelImport $excel */
- $excel = Instansi\ExcelImport::create([
- "key" => "App\Http\Controllers\Operator\SekolahController::class::$kJenjang::1",
- "state" => "validated",
- "progress" => null,
- "filename" => "sekolah.xlsx",
- "handler" => UploadService::class,
- "params" => [
- "domain" => "demo",
- "kJenjang" => $kJenjang,
- ],
- "jml_data" => 5,
- "jml_valid" => 0,
- "jml_error" => 10,
- ]);
- foreach ($rows as $index => $row) {
- Instansi\ExcelImportData::create([
- 'excel_import_id' => $excel->id,
- "index" => $index + 1,
- "item" => $index == 0
- ? array_values($row)
- : $row,
- "attribute" => null,
- "is_error" => true,
- "message" => [
- "singkat_jenjang" => [
- "Singkat jenjang seharusnya XXX",
- ],
- "status_bayar" => [
- "Status bayar seharusnya Sudah atau Belum",
- ],
- ],
- ]);
- }
- $params = [
- 'k_jenjang' => $kJenjang,
- ];
- $res = postJson("/download-error", $params)
- ->assertSuccessful();
- $buffers = readDataFromExcel($res->streamedContent());
- expect($buffers)
- ->toHaveCount($totalSekolah + 1);
- });
- function makeUploadSekolahHeader(): array
- {
- return [
- 'No', 'Kode', 'SIAP ID', 'DAPODIK ID', 'NPSN', 'Nama', 'Jenjang', 'Status', 'Bayar', 'Alamat', 'No Telp',
- 'Kota/Kab', 'Kecamatan', 'Kelurahan', 'Rayon', 'No RW', 'No RT', 'Lintang', 'Bujur', 'Label 01', 'Label 02',
- 'Label 03', 'Label 04', 'Label 05',
- ];
- }
- function makeUploadSekolahData(Instansi\Sekolah $sekolah): array
- {
- return [
- $sekolah->urutan,
- $sekolah->id,
- $sekolah->siap_id,
- $sekolah->dapodik_id,
- $sekolah->npsn,
- $sekolah->nama,
- $sekolah->mJenjang->singkat,
- $sekolah->is_negeri ? 'Negeri' : 'Swasta',
- $sekolah->is_bayar ? 'Sudah' : 'Belum',
- $sekolah->alamat,
- $sekolah->no_telp,
- $sekolah->mKota?->keterangan ?? null,
- $sekolah->mKecamatan?->keterangan ?? null,
- $sekolah->mKelurahan?->keterangan ?? null,
- null,
- $sekolah->no_rw,
- $sekolah->no_rt,
- $sekolah->latitude,
- $sekolah->longitude,
- $sekolah->label01,
- $sekolah->label02,
- $sekolah->label03,
- $sekolah->label04,
- $sekolah->label05,
- ];
- }
Advertisement
Add Comment
Please, Sign In to add comment