Skip to content

Commit ac10c2f

Browse files
authored
feat(db): init boundaries data using cahyadsn/wilayah_boundaries (#104)
2 parents 44d3d31 + 3cbff63 commit ac10c2f

18 files changed

+226
-113
lines changed

.codeclimate.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: "2"
2+
3+
checks:
4+
similar-code:
5+
enabled: false
6+
method-complexity:
7+
config:
8+
threshold: 9
9+
10+
plugins:
11+
fixme:
12+
enabled: true
13+
phpcodesniffer:
14+
enabled: false
15+
phpmd:
16+
enabled: true
17+
config:
18+
rulesets: workbench/phpmd.xml
19+
20+
ratings:
21+
paths:
22+
- src/**.php

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# Exclude unused files
55
# see: https://redd.it/2jzp6k
6+
.codeclimate.yml export-ignore
67
.editorconfig export-ignore
78
.env.example export-ignore
89
.gitattributes export-ignore

.github/workflows/test.yml

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ on:
99
branches: [main]
1010
# paths: ['.github/**.yml', 'src/*', 'tests/*']
1111

12-
env:
13-
DB_USERNAME: ${{ github.repository_owner }}
14-
DB_PASSWORD: secret
15-
1612
concurrency:
1713
group: ${{ github.workflow }}-${{ github.ref }}
1814
cancel-in-progress: true
@@ -31,33 +27,23 @@ jobs:
3127
runs-on: ubuntu-latest
3228
needs: configs
3329
env:
34-
DB_NUSA: nusantara
35-
36-
services:
37-
mysql:
38-
image: mysql:8.0
39-
env:
40-
MYSQL_ROOT_PASSWORD: ${{ env.DB_PASSWORD }}
41-
MYSQL_DATABASE: ${{ env.DB_NUSA }}
42-
MYSQL_USER: ${{ env.DB_USERNAME }}
43-
MYSQL_PASSWORD: ${{ env.DB_PASSWORD }}
44-
options: >-
45-
--health-cmd="mysqladmin ping"
46-
--health-interval=10s
47-
--health-timeout=5s
48-
--health-retries=3
49-
ports:
50-
- 3306:3306
51-
52-
permissions:
53-
actions: read
30+
DB_USERNAME: root
31+
DB_PASSWORD: root
32+
DB_DATABASE: ${{ github.repository_owner }}
33+
UPSTREAM_DB_DATABASE: nusantara
5434

5535
steps:
5636
- name: Checkout
5737
uses: actions/checkout@v4
5838
with:
5939
submodules: true
6040

41+
- name: Setup MySQL
42+
run: |
43+
sudo /etc/init.d/mysql start
44+
mysql -e 'CREATE DATABASE ${{ env.DB_DATABASE }};' -u${{ env.DB_USERNAME }} -p${{ env.DB_PASSWORD }}
45+
mysql -e 'CREATE DATABASE ${{ env.UPSTREAM_DB_DATABASE }};' -u${{ env.DB_USERNAME }} -p${{ env.DB_PASSWORD }}
46+
6147
- name: Setup PHP
6248
uses: shivammathur/setup-php@v2
6349
with:
@@ -86,29 +72,39 @@ jobs:
8672
run: |
8773
./vendor/bin/testbench nusa:import --ansi
8874
./vendor/bin/testbench nusa:stat -dw --ansi
89-
git status -s resources
75+
git status -s {database,resources}
9076
9177
# - name: Get file changed
9278
# uses: tj-actions/changed-files@v45
9379
# with:
9480
# exclude_submodules: true
9581
# # files: resources/**
9682

97-
- name: Upload current stat
83+
- name: Store current stat
9884
if: ${{ github.event_name != 'pull_request' }}
9985
uses: actions/upload-artifact@v4
10086
with:
10187
name: upstream-stats
10288
path: tests/stats.json
10389
if-no-files-found: ignore
10490

91+
- name: Store static resources
92+
uses: actions/upload-artifact@v4
93+
with:
94+
name: static-resources
95+
path: |
96+
database/nusa.sqlite
97+
resources/static/**
98+
10599
tests:
106100
name: Test on PHP ${{ matrix.php }} Laravel ${{ matrix.laravel }} DB ${{ matrix.db }}
107101
needs: [configs, prepare]
108102
runs-on: ubuntu-latest
109103
env:
110104
DB_CONNECTION: ${{ matrix.db }}
111105
DB_DATABASE: ${{ github.repository_owner }}
106+
DB_USERNAME: ${{ github.repository_owner }}
107+
DB_PASSWORD: secret
112108

113109
services:
114110
postgresql:
@@ -177,13 +173,20 @@ jobs:
177173
composer require "laravel/framework=${{ matrix.laravel }}" --no-update
178174
composer update --prefer-dist --no-interaction --no-progress
179175
176+
- name: Restore static resources
177+
uses: actions/download-artifact@v4
178+
with:
179+
name: static-resources
180+
180181
- name: Run migrations
181182
run: |
182-
composer testbench vendor:publish -- --tag creasi-migrations
183-
composer testbench migrate
183+
./vendor/bin/testbench vendor:publish --ansi --tag creasi-migrations
184+
./vendor/bin/testbench migrate --ansi
184185
185186
- name: Run tests
186-
run: composer test -- --coverage
187+
run: |
188+
./vendor/bin/testbench config:show database --ansi
189+
./vendor/bin/testbench package:test --ansi --coverage
187190
188191
- name: Generate reports for CodeClimate
189192
id: reports
@@ -195,7 +198,7 @@ jobs:
195198
curl -LSs $CC_TEST_REPORTER_URL > ./cc-test-reporter && chmod +x ./cc-test-reporter
196199
./cc-test-reporter format-coverage -t clover -o $CODECLIMATE_REPORT tests/reports/clover.xml
197200
198-
- name: Upload tests reports
201+
- name: Store tests reports
199202
uses: actions/upload-artifact@v4
200203
if: needs.configs.outputs.has-codeclimate == '1'
201204
with:
@@ -212,4 +215,3 @@ jobs:
212215
with:
213216
has-coveralls: ${{ needs.configs.outputs.has-coveralls == '1' }}
214217
has-codeclimate: ${{ needs.configs.outputs.has-codeclimate == '1' }}
215-

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "workbench/submodules/cahyadsn-wilayah_kodepos"]
55
path = workbench/submodules/cahyadsn-wilayah_kodepos
66
url = https://github.com/cahyadsn/wilayah_kodepos.git
7+
[submodule "workbench/submodules/cahyadsn-wilayah_boundaries"]
8+
path = workbench/submodules/cahyadsn-wilayah_boundaries
9+
url = https://github.com/cahyadsn/wilayah_boundaries.git

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -883,26 +883,39 @@ using `sqlite` driver. Let us know if you had any issue using another database d
883883
884884
```sh
885885
composer install
886+
pnpm install
886887
```
887888
888-
3. Create new database, by default we use the following configuration :
889+
3. Copy `workbench/.env.example` to `workbench/.env` and update the content you desire
890+
891+
```sh
892+
DB_CONNECTION=mysql # Your main db connection to test againsts
893+
DB_HOST=127.0.0.1
894+
DB_DATABASE=creasi_test # To store the testing data
895+
DB_USERNAME=creasico
896+
DB_PASSWORD=secret
889897
890-
- `dbname` : `nusantara`
891-
- `dbhost` : `127.0.0.1`
892-
- `dbuser` : `root`
893-
- `dbpass` : `secret`
898+
UPSTREAM_DB_DATABASE=nusantara # To store the upstream data
899+
```
900+
901+
4. Create new database to store our upstream and testing data:
894902
895903
```sh
896-
mysql -e 'create database nusantara;'
904+
mysql -e 'create database creasi_test;' # Based on the value of `DB_DATABASE`
905+
mysql -e 'create database nusantara;' # Based on the value of `UPSTREAM_DB_DATABASE`
897906
```
898907
899-
4. Last but not least, run `db:import` command
908+
5. Last but not least, run `db:import` command
900909
901910
```sh
902911
composer db:import
903912
```
904913
905-
If you were using different configuration you can edit [this file](https://github.com/creasico/laravel-nusa/blob/94cd261d7726b9a5cb46cdef4aa9914522a33b4a/tests/NusaTest.php#L16-L19) but please don't submit your changes.
914+
As you might noticed that we use 3 different databases to develop and maintain this library. Here's the explanation :
915+
916+
- `database/nusa.sqlite` is the main database in this library that will be included when you install this library as a dependency
917+
- `DB_DATABASE` is mainly for testing purposes, it simulate the actual application where this library installed on
918+
- `UPSTREAM_DB_DATABASE` is contains the upstream database tables that will be used as source of truth for this library
906919

907920
Once you've done with your meaningful contributions, run the following command to ensure everythings is works as expected.
908921

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
"issues": "https://github.com/creasico/laravel-nusa/issues"
3333
},
3434
"scripts": {
35+
"post-autoload-dump": [
36+
"testbench vendor:publish --tag=creasi-migrations"
37+
],
3538
"fix": [
3639
"pint --preset laravel"
3740
],

database/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ classDiagram
3535
+int regency_code
3636
+int province_code
3737
+string name
38+
+double latitude
39+
+double longitude
3840
+province() Province
3941
+regency() Regency
4042
+villages() Village[]
@@ -45,6 +47,8 @@ classDiagram
4547
+int regency_code
4648
+int province_code
4749
+string name
50+
+double latitude
51+
+double longitude
4852
+int postal_code
4953
+province() Province
5054
+regency() Regency
@@ -84,6 +88,8 @@ classDiagram
8488
| `regency_code` | `char(4)` | `foreign` | - |
8589
| `province_code` | `char(2)` | `foreign` | - |
8690
| `name` | `varchar` | - | - |
91+
| `latitude` | `double`, `nullable` | - | - |
92+
| `longitude` | `double`, `nullable` | - | - |
8793

8894
**Relation Properties**
8995
- `regency_code` : reference `regencies`
@@ -98,6 +104,8 @@ classDiagram
98104
| `regency_code` | `char(4)` | `foreign` | - |
99105
| `province_code` | `char(2)` | `foreign` | - |
100106
| `name` | `varchar` | - | - |
107+
| `latitude` | `double`, `nullable` | - | - |
108+
| `longitude` | `double`, `nullable` | - | - |
101109
| `postal_code` | `char(5)`, `nullable` | - | - |
102110

103111
**Relation Properties**

database/migrations/0000_00_00_000000_create_nusa_tables.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public function up(): void
1818
{
1919
$tableNames = config('creasi.nusa.table_names');
2020

21+
if (Schema::hasTable($tableNames['provinces'])) {
22+
return;
23+
}
24+
2125
Schema::create($tableNames['provinces'], function (Blueprint $table) {
2226
$table->char('code', 2)->primary();
2327
$table->string('name', 50)->index();

tests/Models/DistrictTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public function it_should_has_many_districts(Collection $districts): Collection
4949
{
5050
$districts->each(function (District $district) {
5151
$this->assertIsInt($district->code, 'Code should be int');
52+
$this->assertIsFloat($district->latitude, \sprintf('Latitude of district "%s" should be float', $district->code));
53+
$this->assertIsFloat($district->longitude, \sprintf('Longitude of district "%s" should be float', $district->code));
5254
$this->assertInstanceOf(Collection::class, $district->postal_codes, 'Postal Codes should be instance of collection');
5355

5456
$this->assertInstanceOf(DistrictContract::class, $district);

tests/Models/RegencyTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public function it_should_has_many_regencies(Collection $regencies): Collection
5050
$regencies->each(function (Regency $regency) {
5151
$this->assertIsInt($regency->code, 'Code should be int');
5252
// Comment this out due to https://github.com/cahyadsn/wilayah/pull/47
53-
// $this->assertIsFloat($regency->latitude, 'Latitude should be float');
54-
// $this->assertIsFloat($regency->longitude, 'Longitude should be float');
53+
// $this->assertIsFloat($regency->latitude, \sprintf('Latitude of regency "%s" should be float', $regency->code));
54+
// $this->assertIsFloat($regency->longitude, \sprintf('Longitude of regency "%s" should be float', $regency->code));
5555
$this->assertInstanceOf(Collection::class, $regency->postal_codes, 'Postal Codes should be instance of collection');
5656

5757
$this->assertInstanceOf(RegencyContract::class, $regency);

tests/Models/VillageTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public function it_should_has_many_villages(Collection $villages): Collection
4949
{
5050
$villages->each(function (Village $village) {
5151
$this->assertIsInt($village->code, 'Code should be int');
52+
$this->assertIsFloat($village->latitude, \sprintf('Latitude of village "%s" should be float', $village->code));
53+
$this->assertIsFloat($village->longitude, \sprintf('Longitude of village "%s" should be float', $village->code));
5254
if ($village->postal_code) {
5355
$this->assertIsInt($village->postal_code, 'Postal Code should be int');
5456
}

tests/TestCase.php

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,15 @@
44

55
namespace Creasi\Tests;
66

7-
use Database\Seeders\DatabaseSeeder;
8-
use Illuminate\Config\Repository;
9-
use Illuminate\Foundation\Testing\DatabaseMigrations;
107
use Orchestra\Testbench\Concerns\WithWorkbench;
118
use Orchestra\Testbench\TestCase as Orchestra;
129

1310
class TestCase extends Orchestra
1411
{
15-
use DatabaseMigrations;
1612
use WithWorkbench;
1713

18-
private static $shouldMigrate = true;
19-
20-
protected function defineDatabaseMigrations()
21-
{
22-
$nusa = \config('database.connections.nusa', []);
23-
24-
if (self::$shouldMigrate) {
25-
$this->recreateDatabase($nusa['database']);
26-
27-
$this->loadMigrationsWithoutRollbackFrom(
28-
\realpath(\dirname($nusa['database'])).'/migrations'
29-
);
30-
}
31-
}
32-
33-
protected function defineDatabaseSeeders()
34-
{
35-
if (self::$shouldMigrate) {
36-
$this->seed(DatabaseSeeder::class);
37-
38-
self::$shouldMigrate = false;
39-
}
40-
}
41-
42-
private function recreateDatabase(string $path)
43-
{
44-
if (\file_exists($path)) {
45-
\unlink($path);
46-
}
47-
48-
\touch($path);
49-
50-
return $path;
51-
}
52-
53-
private function mergeConfig(Repository $config, string $key, array $value)
14+
protected function defineEnvironment($app)
5415
{
55-
$config->set($key, array_merge($config->get($key, []), $value));
16+
// $app['config']->set('database.default', env('DB_CONNECTION'));
5617
}
5718
}

workbench/.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
# DB_CONNECTION=mysql
12
DB_HOST=127.0.0.1
23
DB_DATABASE=creasi_test
34
DB_USERNAME=root
45
DB_PASSWORD=
56

6-
DB_NUSA=nusantara
7+
UPSTREAM_DB_DATABASE=nusantara

0 commit comments

Comments
 (0)