From a89212d7c2368efc59d0041b370fcec9ece44935 Mon Sep 17 00:00:00 2001 From: giang Date: Thu, 13 Aug 2020 18:21:28 -0700 Subject: [PATCH 1/9] added tests for routes --- composer.json | 1 + composer.lock | 192 ++++++++++++++++++++++++++- database/factories/UserFactory.php | 1 + tests/Browser/ExampleTest.php | 23 ++++ tests/Browser/Pages/HomePage.php | 41 ++++++ tests/Browser/Pages/Page.php | 20 +++ tests/Browser/console/.gitignore | 2 + tests/Browser/screenshots/.gitignore | 2 + tests/DuskTestCase.php | 44 ++++++ tests/Unit/HTTPTest.php | 82 ++++++++++++ 10 files changed, 404 insertions(+), 4 deletions(-) create mode 100644 tests/Browser/ExampleTest.php create mode 100644 tests/Browser/Pages/HomePage.php create mode 100644 tests/Browser/Pages/Page.php create mode 100644 tests/Browser/console/.gitignore create mode 100644 tests/Browser/screenshots/.gitignore create mode 100644 tests/DuskTestCase.php create mode 100644 tests/Unit/HTTPTest.php diff --git a/composer.json b/composer.json index d418fef..abac5ce 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "barryvdh/laravel-debugbar": "^3.3", "facade/ignition": "^2.0", "fzaninotto/faker": "^1.9.1", + "laravel/dusk": "^6.4", "mockery/mockery": "^1.3.1", "nunomaduro/collision": "^4.1", "phpunit/phpunit": "^8.5" diff --git a/composer.lock b/composer.lock index 4213af2..b9ec0b0 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "73d802409456a1a52d8c3c867ff2626b", + "content-hash": "33bfd0057d61d787a9206b2111c17bbd", "packages": [ { "name": "asm89/stack-cors", @@ -2177,7 +2177,7 @@ "email": "fabien@symfony.com" }, { - "name": "Jean-Fran??ois Simon", + "name": "Jean-François Simon", "email": "jeanfrancois.simon@sensiolabs.com" }, { @@ -3867,6 +3867,57 @@ "homepage": "https://github.com/tijsverkoyen/CssToInlineStyles", "time": "2020-07-13T06:12:54+00:00" }, + { + "name": "twbs/bootstrap", + "version": "v4.5.2", + "source": { + "type": "git", + "url": "https://github.com/twbs/bootstrap.git", + "reference": "5f2480a90ab911babc53039835fe78c6fc12646d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twbs/bootstrap/zipball/5f2480a90ab911babc53039835fe78c6fc12646d", + "reference": "5f2480a90ab911babc53039835fe78c6fc12646d", + "shasum": "" + }, + "replace": { + "twitter/bootstrap": "self.version" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mark Otto", + "email": "markdotto@gmail.com" + }, + { + "name": "Jacob Thornton", + "email": "jacobthornton@gmail.com" + } + ], + "description": "The most popular front-end framework for developing responsive, mobile first projects on the web.", + "homepage": "https://getbootstrap.com/", + "keywords": [ + "JS", + "css", + "framework", + "front-end", + "mobile-first", + "responsive", + "sass", + "web" + ], + "time": "2020-08-06T16:22:43+00:00" + }, { "name": "vlucas/phpdotenv", "version": "v4.1.8", @@ -4382,7 +4433,7 @@ ], "authors": [ { - "name": "Fran??ois Zaninotto" + "name": "François Zaninotto" } ], "description": "Faker is a PHP library that generates fake data for you.", @@ -4440,6 +4491,74 @@ ], "time": "2020-07-09T08:09:16+00:00" }, + { + "name": "laravel/dusk", + "version": "v6.4.1", + "source": { + "type": "git", + "url": "https://github.com/laravel/dusk.git", + "reference": "db10ac70d01f332614a54cd7f1fe4757066db7cc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/dusk/zipball/db10ac70d01f332614a54cd7f1fe4757066db7cc", + "reference": "db10ac70d01f332614a54cd7f1fe4757066db7cc", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-zip": "*", + "illuminate/console": "^6.0|^7.0", + "illuminate/support": "^6.0|^7.0", + "nesbot/carbon": "^2.0", + "php": "^7.2", + "php-webdriver/webdriver": "^1.8.1", + "symfony/console": "^4.3|^5.0", + "symfony/finder": "^4.3|^5.0", + "symfony/process": "^4.3|^5.0", + "vlucas/phpdotenv": "^3.0|^4.0" + }, + "require-dev": { + "mockery/mockery": "^1.0", + "phpunit/phpunit": "^7.5.15|^8.4|^9.0" + }, + "suggest": { + "ext-pcntl": "Used to gracefully terminate Dusk when tests are running." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.x-dev" + }, + "laravel": { + "providers": [ + "Laravel\\Dusk\\DuskServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Laravel\\Dusk\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "Laravel Dusk provides simple end-to-end testing and browser automation.", + "keywords": [ + "laravel", + "testing", + "webdriver" + ], + "time": "2020-07-14T18:02:46+00:00" + }, { "name": "maximebf/debugbar", "version": "v1.16.3", @@ -4543,7 +4662,7 @@ ], "authors": [ { - "name": "P??draic Brady", + "name": "Pádraic Brady", "email": "padraic.brady@gmail.com", "homepage": "http://blog.astrumfutura.com" }, @@ -4789,6 +4908,71 @@ "description": "Library for handling version information and constraints", "time": "2018-07-08T19:19:57+00:00" }, + { + "name": "php-webdriver/webdriver", + "version": "1.8.2", + "source": { + "type": "git", + "url": "https://github.com/php-webdriver/php-webdriver.git", + "reference": "3308a70be084d6d7fd1ee5787b4c2e6eb4b70aab" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-webdriver/php-webdriver/zipball/3308a70be084d6d7fd1ee5787b4c2e6eb4b70aab", + "reference": "3308a70be084d6d7fd1ee5787b4c2e6eb4b70aab", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "ext-json": "*", + "ext-zip": "*", + "php": "^5.6 || ~7.0", + "symfony/polyfill-mbstring": "^1.12", + "symfony/process": "^2.8 || ^3.1 || ^4.0 || ^5.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.0", + "jakub-onderka/php-parallel-lint": "^1.0", + "php-coveralls/php-coveralls": "^2.0", + "php-mock/php-mock-phpunit": "^1.1", + "phpunit/phpunit": "^5.7", + "sebastian/environment": "^1.3.4 || ^2.0 || ^3.0", + "sminnee/phpunit-mock-objects": "^3.4", + "squizlabs/php_codesniffer": "^3.5", + "symfony/var-dumper": "^3.3 || ^4.0 || ^5.0" + }, + "suggest": { + "ext-SimpleXML": "For Firefox profile creation" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.8.x-dev" + } + }, + "autoload": { + "psr-4": { + "Facebook\\WebDriver\\": "lib/" + }, + "files": [ + "lib/Exception/TimeoutException.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A PHP client for Selenium WebDriver. Previously facebook/webdriver.", + "homepage": "https://github.com/php-webdriver/php-webdriver", + "keywords": [ + "Chromedriver", + "geckodriver", + "php", + "selenium", + "webdriver" + ], + "time": "2020-03-04T14:40:12+00:00" + }, { "name": "phpdocumentor/reflection-common", "version": "2.2.0", diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index ff871dd..02ded29 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -30,5 +30,6 @@ 'email_verified_at' => now(), 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password 'remember_token' => Str::random(10), + 'is_admin' => true, ]; }); diff --git a/tests/Browser/ExampleTest.php b/tests/Browser/ExampleTest.php new file mode 100644 index 0000000..9d8968b --- /dev/null +++ b/tests/Browser/ExampleTest.php @@ -0,0 +1,23 @@ +browse(function (Browser $browser) { + $browser->visit('/') + ->assertSee('Laravel'); + }); + } +} diff --git a/tests/Browser/Pages/HomePage.php b/tests/Browser/Pages/HomePage.php new file mode 100644 index 0000000..26bf174 --- /dev/null +++ b/tests/Browser/Pages/HomePage.php @@ -0,0 +1,41 @@ + '#selector', + ]; + } +} diff --git a/tests/Browser/Pages/Page.php b/tests/Browser/Pages/Page.php new file mode 100644 index 0000000..f8d7622 --- /dev/null +++ b/tests/Browser/Pages/Page.php @@ -0,0 +1,20 @@ + '#selector', + ]; + } +} diff --git a/tests/Browser/console/.gitignore b/tests/Browser/console/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/tests/Browser/console/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/tests/Browser/screenshots/.gitignore b/tests/Browser/screenshots/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/tests/Browser/screenshots/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/tests/DuskTestCase.php b/tests/DuskTestCase.php new file mode 100644 index 0000000..b1dd1bc --- /dev/null +++ b/tests/DuskTestCase.php @@ -0,0 +1,44 @@ +addArguments([ + '--disable-gpu', + '--headless', + '--window-size=1920,1080', + ]); + + return RemoteWebDriver::create( + 'http://localhost:9515', DesiredCapabilities::chrome()->setCapability( + ChromeOptions::CAPABILITY, $options + ) + ); + } +} diff --git a/tests/Unit/HTTPTest.php b/tests/Unit/HTTPTest.php new file mode 100644 index 0000000..1a0af81 --- /dev/null +++ b/tests/Unit/HTTPTest.php @@ -0,0 +1,82 @@ +get('/'); + $response->assertStatus(200); + } + + /** @test */ + public function getHomePageTest() + { + $response = $this->get('/home'); + $response->assertStatus(200); + } + + /** @test */ + public function getAboutPageTest() + { + $response = $this->get('/about'); + $response->assertStatus(200); + } + + + /** This test is used to verify a user a redirected to the login + * if they attempt to access the rent a locker page without + * being authenticated + */ + /** @test */ + public function getRentalPageWithoutLoginTest() + { + $response = $this->get('/rent'); + + $response->assertRedirect('/login'); + } + + /** This test is used to verify that the admin dashboard page + * does not work without authentication + */ + /** @test */ + public function adminPageWithoutAdminRightsTest() + { + $response = $this->get('/admin/dashboard'); + + $response->assertRedirect('/login'); + } + + /** This test is used to verify that accessing the rental page + * as a user works + */ + /** @test */ + public function rentalPageAsUserTest() + { + $user = factory(User::class)->create(); + + $response = $this->actingAs($user)->get('/rent'); + + $response->assertStatus(200); + } + + /** This test is used to verify that accessing the rental page + * as a user works + */ + /** @test */ + public function adminDashboardAsAdmin() + { + $user = factory(User::class)->create(); + + $response = $this->actingAs($user)->get('/admin/dashboard'); + + $response->assertStatus(200); + } +} From b72b7283f7755a9b6cbfd2b038d5519d82ce42d5 Mon Sep 17 00:00:00 2001 From: Dmitri Date: Thu, 13 Aug 2020 21:53:56 -0700 Subject: [PATCH 2/9] Add build step --- .github/workflows/laravel.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/laravel.yml b/.github/workflows/laravel.yml index 63863eb..2c01363 100644 --- a/.github/workflows/laravel.yml +++ b/.github/workflows/laravel.yml @@ -25,6 +25,7 @@ jobs: run: | mkdir -p database touch database/database.pgsql + php artisan migrate - name: Execute tests (Unit and Feature tests) via PHPUnit env: DB_CONNECTION: pgsql From edffb8c359a0eeffc7d72d980eca8fa6c16e8e78 Mon Sep 17 00:00:00 2001 From: Dmitri Date: Thu, 13 Aug 2020 21:59:20 -0700 Subject: [PATCH 3/9] Make new .env for testing --- .env.test | 42 +++++++++++++++++++++++++++++++++++ .github/workflows/laravel.yml | 4 +++- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 .env.test diff --git a/.env.test b/.env.test new file mode 100644 index 0000000..cbd3cb2 --- /dev/null +++ b/.env.test @@ -0,0 +1,42 @@ +APP_NAME=Laravel +APP_ENV=local +APP_KEY= +APP_DEBUG=true +APP_URL=http://localhost + +LOG_CHANNEL=stack + +DB_CONNECTION=pgsql +DB_DATABASE=database/database.psql + +BROADCAST_DRIVER=log +CACHE_DRIVER=file +QUEUE_CONNECTION=sync +SESSION_DRIVER=file +SESSION_LIFETIME=120 + +REDIS_HOST=127.0.0.1 +REDIS_PASSWORD=null +REDIS_PORT=6379 + +MAIL_MAILER=smtp +MAIL_HOST=smtp.mailtrap.io +MAIL_PORT=2525 +MAIL_USERNAME=null +MAIL_PASSWORD=null +MAIL_ENCRYPTION=null +MAIL_FROM_ADDRESS=null +MAIL_FROM_NAME="${APP_NAME}" + +AWS_ACCESS_KEY_ID= +AWS_SECRET_ACCESS_KEY= +AWS_DEFAULT_REGION=us-east-1 +AWS_BUCKET= + +PUSHER_APP_ID= +PUSHER_APP_KEY= +PUSHER_APP_SECRET= +PUSHER_APP_CLUSTER=mt1 + +MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" +MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" diff --git a/.github/workflows/laravel.yml b/.github/workflows/laravel.yml index 2c01363..c117d6b 100644 --- a/.github/workflows/laravel.yml +++ b/.github/workflows/laravel.yml @@ -13,8 +13,10 @@ jobs: steps: - uses: actions/checkout@v2 + + - name: Copy .env - run: php -r "file_exists('.env') || copy('.env.example', '.env');" + run: php -r "file_exists('.env') || copy('.env.test', '.env');" - name: Install Dependencies run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist - name: Generate key From e95a03580c508b15f4013a1ee505868e3cdf5cf8 Mon Sep 17 00:00:00 2001 From: Dmitri Date: Thu, 13 Aug 2020 22:02:16 -0700 Subject: [PATCH 4/9] Try SQL-Lite --- .github/workflows/laravel.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/laravel.yml b/.github/workflows/laravel.yml index c117d6b..9bf9df7 100644 --- a/.github/workflows/laravel.yml +++ b/.github/workflows/laravel.yml @@ -26,10 +26,10 @@ jobs: - name: Create Database run: | mkdir -p database - touch database/database.pgsql + touch database/database.sqlite php artisan migrate - name: Execute tests (Unit and Feature tests) via PHPUnit env: - DB_CONNECTION: pgsql - DB_DATABASE: database/database.pgsql + DB_CONNECTION: sqlite + DB_DATABASE: database/database.sqlite run: vendor/bin/phpunit From 9930f6b449c999b98b87a79fe5dd3270a607ecf5 Mon Sep 17 00:00:00 2001 From: Dmitri Date: Thu, 13 Aug 2020 22:02:16 -0700 Subject: [PATCH 5/9] Try SQL-Lite --- .env.test | 6 +++++- .github/workflows/laravel.yml | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.env.test b/.env.test index cbd3cb2..04ee271 100644 --- a/.env.test +++ b/.env.test @@ -7,7 +7,11 @@ APP_URL=http://localhost LOG_CHANNEL=stack DB_CONNECTION=pgsql -DB_DATABASE=database/database.psql +DB_HOST=postgres +DB_PORT=5432 +DB_DATABASE=pg_user +DB_USERNAME=pg_user +DB_PASSWORD=pg_user_unit_test_pass BROADCAST_DRIVER=log CACHE_DRIVER=file diff --git a/.github/workflows/laravel.yml b/.github/workflows/laravel.yml index 9bf9df7..a12daa6 100644 --- a/.github/workflows/laravel.yml +++ b/.github/workflows/laravel.yml @@ -14,6 +14,16 @@ jobs: steps: - uses: actions/checkout@v2 + services: + postgres: + image: postgres:11 + env: + POSTGRES_USER: pg_user + POSTGRES_PASSWORD: pg_user + POSTGRES_DB: pg_user_unit_test_pass + ports: + - 5432:5432 + - name: Copy .env run: php -r "file_exists('.env') || copy('.env.test', '.env');" From 8bf40c43af71b8b03e1d3a266f0cbf5f7a85a3c4 Mon Sep 17 00:00:00 2001 From: Dmitri Date: Thu, 13 Aug 2020 22:12:08 -0700 Subject: [PATCH 6/9] Re-test --- .env.test | 1 + 1 file changed, 1 insertion(+) diff --git a/.env.test b/.env.test index 04ee271..e3906d7 100644 --- a/.env.test +++ b/.env.test @@ -13,6 +13,7 @@ DB_DATABASE=pg_user DB_USERNAME=pg_user DB_PASSWORD=pg_user_unit_test_pass + BROADCAST_DRIVER=log CACHE_DRIVER=file QUEUE_CONNECTION=sync From 4e5594b549f6ba1e0c9eb31f1d0350576ca9f889 Mon Sep 17 00:00:00 2001 From: Dmitri Date: Thu, 13 Aug 2020 22:14:28 -0700 Subject: [PATCH 7/9] Fix test --- .github/workflows/laravel.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/laravel.yml b/.github/workflows/laravel.yml index a12daa6..50c898e 100644 --- a/.github/workflows/laravel.yml +++ b/.github/workflows/laravel.yml @@ -11,9 +11,6 @@ jobs: runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - services: postgres: image: postgres:11 @@ -24,7 +21,9 @@ jobs: ports: - 5432:5432 - + steps: + - uses: actions/checkout@v2 + - name: Copy .env run: php -r "file_exists('.env') || copy('.env.test', '.env');" - name: Install Dependencies From 959b537ebc2fcc6157e895f00803eb313ad617ef Mon Sep 17 00:00:00 2001 From: Dmitri Date: Thu, 13 Aug 2020 22:18:53 -0700 Subject: [PATCH 8/9] Try to fix test --- .env.test | 2 +- .github/workflows/laravel.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.env.test b/.env.test index e3906d7..e324e2f 100644 --- a/.env.test +++ b/.env.test @@ -7,7 +7,7 @@ APP_URL=http://localhost LOG_CHANNEL=stack DB_CONNECTION=pgsql -DB_HOST=postgres +DB_HOST=localhost DB_PORT=5432 DB_DATABASE=pg_user DB_USERNAME=pg_user diff --git a/.github/workflows/laravel.yml b/.github/workflows/laravel.yml index 50c898e..8a36459 100644 --- a/.github/workflows/laravel.yml +++ b/.github/workflows/laravel.yml @@ -35,7 +35,7 @@ jobs: - name: Create Database run: | mkdir -p database - touch database/database.sqlite + touch database/database.pgsql php artisan migrate - name: Execute tests (Unit and Feature tests) via PHPUnit env: From 981503f3564d76772e2a1ee2f47dcfa087edcf72 Mon Sep 17 00:00:00 2001 From: Dmitri Date: Sat, 15 Aug 2020 16:29:38 -0700 Subject: [PATCH 9/9] Try switching DB to SQL Lite --- .env.test | 9 ++------- .github/workflows/laravel.yml | 12 +----------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/.env.test b/.env.test index e324e2f..f66de36 100644 --- a/.env.test +++ b/.env.test @@ -6,13 +6,8 @@ APP_URL=http://localhost LOG_CHANNEL=stack -DB_CONNECTION=pgsql -DB_HOST=localhost -DB_PORT=5432 -DB_DATABASE=pg_user -DB_USERNAME=pg_user -DB_PASSWORD=pg_user_unit_test_pass - +DB_CONNECTION=sqlite +DB_DATABASE=database/database.sqlite BROADCAST_DRIVER=log CACHE_DRIVER=file diff --git a/.github/workflows/laravel.yml b/.github/workflows/laravel.yml index 8a36459..b6f4004 100644 --- a/.github/workflows/laravel.yml +++ b/.github/workflows/laravel.yml @@ -11,16 +11,6 @@ jobs: runs-on: ubuntu-latest - services: - postgres: - image: postgres:11 - env: - POSTGRES_USER: pg_user - POSTGRES_PASSWORD: pg_user - POSTGRES_DB: pg_user_unit_test_pass - ports: - - 5432:5432 - steps: - uses: actions/checkout@v2 @@ -35,7 +25,7 @@ jobs: - name: Create Database run: | mkdir -p database - touch database/database.pgsql + touch database/database.sqlite php artisan migrate - name: Execute tests (Unit and Feature tests) via PHPUnit env: