Skip to content

Commit 09d353b

Browse files
authored
Integration test for laravel using docker and githubworkflow (#119)
This PR adds 1. Sample application, updates them to use sqlcommenter-php library and runs integration test on that. 2. Github workflows for unit and integration tests.
1 parent 73776c5 commit 09d353b

93 files changed

Lines changed: 12760 additions & 11 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
on:
2+
push:
3+
branches:
4+
- master
5+
paths:
6+
- php/sqlcommenter-php/packages/sqlcommenter-laravel/**
7+
- php/sqlcommenter-php/samples/sqlcommenter-laravel/**
8+
9+
name: Laravel Integration Tests
10+
jobs:
11+
phpunit:
12+
runs-on: ubuntu-latest
13+
container:
14+
image: kirschbaumdevelopment/laravel-test-runner:8.0
15+
16+
services:
17+
mysql:
18+
image: mysql:8
19+
env:
20+
MYSQL_ROOT_PASSWORD: password
21+
MYSQL_DATABASE: test
22+
ports:
23+
- 33306:3306
24+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
25+
26+
steps:
27+
- uses: actions/checkout@v1
28+
with:
29+
fetch-depth: 1
30+
31+
- name: Get Composer Cache Directory
32+
working-directory: ./php/sqlcommenter-php/samples/sqlcommenter-laravel
33+
id: composer-cache
34+
run: |
35+
echo "::set-output name=dir::$(composer config cache-files-dir)"
36+
37+
- uses: actions/cache@v1
38+
with:
39+
path: ${{ steps.composer-cache.outputs.dir }}
40+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
41+
restore-keys: |
42+
${{ runner.os }}-composer-
43+
44+
- name: Get yarn cache
45+
working-directory: ./php/sqlcommenter-php/samples/sqlcommenter-laravel
46+
id: yarn-cache
47+
run: echo "::set-output name=dir::$(yarn cache dir)"
48+
49+
- uses: actions/cache@v1
50+
with:
51+
path: ${{ steps.yarn-cache.outputs.dir }}
52+
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
53+
restore-keys: |
54+
${{ runner.os }}-yarn-
55+
56+
- name: Install composer dependencies
57+
working-directory: ./php/sqlcommenter-php/samples/sqlcommenter-laravel
58+
run: |
59+
composer install --no-scripts
60+
61+
- name: Prepare Laravel Application
62+
working-directory: ./php/sqlcommenter-php/samples/sqlcommenter-laravel
63+
run: |
64+
cp .env.ci .env
65+
php artisan key:generate
66+
67+
- name: Install front-end dependencies
68+
working-directory: ./php/sqlcommenter-php/samples/sqlcommenter-laravel
69+
run: |
70+
npm install
71+
npm run dev
72+
73+
- name: Apply migrations
74+
working-directory: ./php/sqlcommenter-php/samples/sqlcommenter-laravel
75+
run: |
76+
php artisan migrate
77+
78+
- name: Publish SQLCommenter Configurations
79+
working-directory: ./php/sqlcommenter-php/samples/sqlcommenter-laravel
80+
run: |
81+
php artisan vendor:publish --provider="Google\GoogleSqlCommenterLaravel\GoogleSqlCommenterServiceProvider"
82+
83+
- name: Run Testsuite
84+
working-directory: ./php/sqlcommenter-php/samples/sqlcommenter-laravel
85+
run: vendor/bin/phpunit tests/
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
on:
2+
push:
3+
branches:
4+
- master
5+
paths:
6+
- php/sqlcommenter-php/packages/sqlcommenter-laravel/**
7+
- php/sqlcommenter-php/samples/sqlcommenter-laravel/**
8+
9+
name: Laravel Unit Tests
10+
jobs:
11+
phpunit:
12+
runs-on: ubuntu-latest
13+
container:
14+
image: kirschbaumdevelopment/laravel-test-runner:8.0
15+
16+
steps:
17+
- uses: actions/checkout@v1
18+
with:
19+
fetch-depth: 1
20+
21+
- name: Get Composer Cache Directory
22+
working-directory: ./php/sqlcommenter-php/packages/sqlcommenter-laravel
23+
id: composer-cache
24+
run: |
25+
echo "::set-output name=dir::$(composer config cache-files-dir)"
26+
27+
- uses: actions/cache@v1
28+
with:
29+
path: ${{ steps.composer-cache.outputs.dir }}
30+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
31+
restore-keys: |
32+
${{ runner.os }}-composer-
33+
34+
- name: Get yarn cache
35+
working-directory: ./php/sqlcommenter-php/packages/sqlcommenter-laravel
36+
id: yarn-cache
37+
run: echo "::set-output name=dir::$(yarn cache dir)"
38+
39+
- uses: actions/cache@v1
40+
with:
41+
path: ${{ steps.yarn-cache.outputs.dir }}
42+
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
43+
restore-keys: |
44+
${{ runner.os }}-yarn-
45+
46+
- name: Install composer dependencies
47+
working-directory: ./php/sqlcommenter-php/packages/sqlcommenter-laravel
48+
run: |
49+
composer install --no-scripts
50+
51+
- name: Run Testsuite
52+
working-directory: ./php/sqlcommenter-php/packages/sqlcommenter-laravel
53+
run: vendor/bin/phpunit tests/

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
php/sqlcommenter-php/packages/sqlcommenter-laravel/vendor/*
2+
.idea/**

.phpunit.result.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":{"UtilsTest::testFormatCommentsWithKeys":4,"UtilsTest::testFormatCommentsWithoutKeys":4,"UtilsTest::testFormatCommentsWithSpecialCharKeys":4},"times":{"Tests\\Unit\\ExampleTest::test_that_true_is_true":0,"UtilsTest::testFormatCommentsWithKeys":0,"UtilsTest::testFormatCommentsWithoutKeys":0,"UtilsTest::testFormatCommentsWithSpecialCharKeys":0}}

php/sqlcommenter-php/packages/sqlcommenter-laravel/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ composer require "google/sqlcommenter-laravel"
2424
Publish the config file from library to into laravel app using below command
2525

2626
```shell
27-
php artisan vendor:publish --provider="google\GoogleSqlCommenterLaravel\GoogleSqlCommenterServiceProvider"
27+
php artisan vendor:publish --provider="Google\GoogleSqlCommenterLaravel\GoogleSqlCommenterServiceProvider"
28+
2829
```
2930

3031
Add the following class above ``Illuminate\Database\DatabaseServiceProvider::class,

php/sqlcommenter-php/packages/sqlcommenter-laravel/src/Database/Connection.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Connection extends BaseConnection
3535
*/
3636
public function selectOne($query, $bindings = [], $useReadPdo = true)
3737
{
38-
$query .= $this->getSqlComments();
38+
$query = $this->getSqlComments($query);
3939
$records = parent::select($query, $bindings, $useReadPdo);
4040

4141
if (count($records) > 0) {
@@ -54,7 +54,7 @@ public function selectOne($query, $bindings = [], $useReadPdo = true)
5454
*/
5555
public function select($query, $bindings = [], $useReadPdo = true)
5656
{
57-
$query .= $this->getSqlComments();
57+
$query = $this->getSqlComments($query);
5858
$records = parent::select($query, $bindings, $useReadPdo);
5959
return $records;
6060
}
@@ -68,8 +68,7 @@ public function select($query, $bindings = [], $useReadPdo = true)
6868
*/
6969
public function insert($query, $bindings = [])
7070
{
71-
$query .= $this->getSqlComments();
72-
71+
$query = $this->getSqlComments($query);
7372
$records = parent::insert($query, $bindings);
7473

7574
return $records;
@@ -84,8 +83,8 @@ public function insert($query, $bindings = [])
8483
*/
8584
public function update($query, $bindings = [])
8685
{
87-
$query .= $this->getSqlComments();
88-
86+
$query = $this->getSqlComments($query);
87+
8988
return $this->affectingStatement($query, $bindings);
9089
}
9190

@@ -98,12 +97,13 @@ public function update($query, $bindings = [])
9897
*/
9998
public function delete($query, $bindings = [])
10099
{
101-
$query .= $this->getSqlComments();
100+
101+
$query = $this->getSqlComments($query);
102102

103103
return $this->affectingStatement($query, $bindings);
104104
}
105105

106-
private function getSqlComments()
106+
private function getSqlComments($query)
107107
{
108108
$configurationKey = 'google_sqlcommenter.include.';
109109
$comment = [];
@@ -118,7 +118,7 @@ private function getSqlComments()
118118
if (config($configurationKey . 'controller', true) and !empty($action['controller'])) {
119119
$comment['controller'] = explode("@", class_basename($action['controller']))[0];
120120
}
121-
if (config($configurationKey . 'action', true) and !empty($action['controller'] and str_contains($action['controller'], '@'))) {
121+
if (config($configurationKey . 'action', true) and !empty($action and $action['controller'] and str_contains($action['controller'], '@'))) {
122122
$comment['action'] = explode("@", class_basename($action['controller']))[1];
123123
}
124124
if (config($configurationKey . 'route', true)) {
@@ -132,6 +132,13 @@ private function getSqlComments()
132132
$carrier = Opentelemetry::getOpentelemetryValues();
133133
$comment = array_merge($comment, $carrier);
134134
}
135-
return Utils::formatComments(array_filter(($comment)));
135+
136+
$query=trim($query);
137+
138+
if ($query[-1] == ';'){
139+
return rtrim($query ,";"). Utils::formatComments(array_filter(($comment))). ';';
140+
}
141+
return $query . Utils::formatComments(array_filter(($comment)));
142+
136143
}
137144
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.{yml,yaml}]
15+
indent_size = 2
16+
17+
[docker-compose.yml]
18+
indent_size = 4
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
APP_NAME=Laravel
2+
APP_ENV=local
3+
APP_KEY=base64:JqM8ZOydXLT3tcQRl+GPEWuEoCFnXNOZ51DgjoV6FfM=
4+
APP_DEBUG=true
5+
APP_URL=http://localhost
6+
7+
LOG_CHANNEL=stack
8+
LOG_DEPRECATIONS_CHANNEL=null
9+
LOG_LEVEL=debug
10+
11+
DB_CONNECTION=mysql
12+
DB_HOST=mysql
13+
DB_PORT=3306
14+
DB_DATABASE=test
15+
DB_USERNAME=root
16+
DB_PASSWORD=password
17+
18+
BROADCAST_DRIVER=log
19+
CACHE_DRIVER=file
20+
FILESYSTEM_DISK=local
21+
QUEUE_CONNECTION=sync
22+
SESSION_DRIVER=file
23+
SESSION_LIFETIME=120
24+
25+
MEMCACHED_HOST=127.0.0.1
26+
27+
REDIS_HOST=127.0.0.1
28+
REDIS_PASSWORD=null
29+
REDIS_PORT=6379
30+
31+
MAIL_MAILER=smtp
32+
MAIL_HOST=mailhog
33+
MAIL_PORT=1025
34+
MAIL_USERNAME=null
35+
MAIL_PASSWORD=null
36+
MAIL_ENCRYPTION=null
37+
MAIL_FROM_ADDRESS="hello@example.com"
38+
MAIL_FROM_NAME="${APP_NAME}"
39+
40+
AWS_ACCESS_KEY_ID=
41+
AWS_SECRET_ACCESS_KEY=
42+
AWS_DEFAULT_REGION=us-east-1
43+
AWS_BUCKET=
44+
AWS_USE_PATH_STYLE_ENDPOINT=false
45+
46+
PUSHER_APP_ID=
47+
PUSHER_APP_KEY=
48+
PUSHER_APP_SECRET=
49+
PUSHER_APP_CLUSTER=mt1
50+
51+
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
52+
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
APP_NAME=Laravel
2+
APP_ENV=local
3+
APP_KEY=
4+
APP_DEBUG=true
5+
APP_URL=http://localhost
6+
7+
LOG_CHANNEL=stack
8+
LOG_DEPRECATIONS_CHANNEL=null
9+
LOG_LEVEL=debug
10+
11+
DB_CONNECTION=mysql
12+
DB_HOST=127.0.0.1
13+
DB_PORT=3306
14+
DB_DATABASE=laravel
15+
DB_USERNAME=root
16+
DB_PASSWORD=
17+
18+
BROADCAST_DRIVER=log
19+
CACHE_DRIVER=file
20+
FILESYSTEM_DISK=local
21+
QUEUE_CONNECTION=sync
22+
SESSION_DRIVER=file
23+
SESSION_LIFETIME=120
24+
25+
MEMCACHED_HOST=127.0.0.1
26+
27+
REDIS_HOST=127.0.0.1
28+
REDIS_PASSWORD=null
29+
REDIS_PORT=6379
30+
31+
MAIL_MAILER=smtp
32+
MAIL_HOST=mailhog
33+
MAIL_PORT=1025
34+
MAIL_USERNAME=null
35+
MAIL_PASSWORD=null
36+
MAIL_ENCRYPTION=null
37+
MAIL_FROM_ADDRESS="hello@example.com"
38+
MAIL_FROM_NAME="${APP_NAME}"
39+
40+
AWS_ACCESS_KEY_ID=
41+
AWS_SECRET_ACCESS_KEY=
42+
AWS_DEFAULT_REGION=us-east-1
43+
AWS_BUCKET=
44+
AWS_USE_PATH_STYLE_ENDPOINT=false
45+
46+
PUSHER_APP_ID=
47+
PUSHER_APP_KEY=
48+
PUSHER_APP_SECRET=
49+
PUSHER_APP_CLUSTER=mt1
50+
51+
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
52+
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
* text=auto
2+
3+
*.blade.php diff=html
4+
*.css diff=css
5+
*.html diff=html
6+
*.md diff=markdown
7+
*.php diff=php
8+
9+
/.github export-ignore
10+
CHANGELOG.md export-ignore

0 commit comments

Comments
 (0)