Skip to content

Commit

Permalink
Merge pull request #108 from creme332/github-testing
Browse files Browse the repository at this point in the history
setup database before running tests in GH action
  • Loading branch information
creme332 authored Apr 18, 2024
2 parents 57f991a + 94e10f8 commit b1f3049
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 33 deletions.
42 changes: 38 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,59 @@ permissions:

jobs:
build:

runs-on: ubuntu-latest

env:
TEST_DB_NAME: cafe_test
DB_USER: root # do not change
DB_PASSWORD: root # do not change

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: mbstring, intl
ini-values: post_max_size=256M, max_execution_time=180, variables_order="EGPCS"
coverage: xdebug
tools: php-cs-fixer, phpunit

- name: Setup database
run: |
sudo /etc/init.d/mysql start
mysql -e "CREATE DATABASE IF NOT EXISTS $TEST_DB_NAME;" -u$DB_USER -p$DB_PASSWORD
mysql -D$TEST_DB_NAME -u$DB_USER -p$DB_PASSWORD -hlocalhost -P3306 < "resources/database/dump/cafe.sql"
mysql -e "USE cafe_test; SHOW TABLES;" -u$DB_USER -p$DB_PASSWORD
- name: Create .env file
env:
ENV: |
PUBLIC_ROOT="http://localhost/steamy-sips/public"
DB_HOST="localhost"
DB_USERNAME="root"
DB_PASSWORD="root"
TEST_DB_NAME="cafe_test"
BUSINESS_GMAIL=""
BUSINESS_GMAIL_PASSWORD=""
run: |
echo "$ENV" > src/core/.env
cat src/core/.env
- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
- name: Install Composer dependencies
run: composer install --prefer-dist --no-progress

- name: Run test suite
Expand Down
44 changes: 25 additions & 19 deletions docs/INSTALLATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ some commands may differ. Please adapt accordingly.
- MySQL (v15.1 preferred)
- Composer with its executable on your $PATH
- Git
- NPM (optional)

## Setup project
## Project setup

Navigate to the document root of your server:

Expand Down Expand Up @@ -56,14 +57,14 @@ BUSINESS_GMAIL_PASSWORD=""
Some important notes:

- Update the values assigned to `DB_USERNAME` and `DB_PASSWORD` with your MySQL login details.
- If your Apache server is serving from a port other than the default one, add the new port number to `PUBLIC_ROOT` (
- If your Apache server is serving from a port other than the default one, include the port number to `PUBLIC_ROOT` (
e.g., `http://localhost:443/steamy-sips/public`) .
- `BUSINESS_GMAIL` and `BUSINESS_GMAIL_PASSWORD` are the credentials of the Gmail account from which emails will be sent
whenever a client places an order. It is recommended to use
a [Gmail App password](https://knowledge.workspace.google.com/kb/how-to-create-app-passwords-000009237)
for `BUSINESS_GMAIL_PASSWORD` instead of your actual gmail account password.

## Setup production database
## Database setup

Start your MySQL server and connect to its monitor:

Expand All @@ -76,32 +77,37 @@ mysql -u <username> -p
Create a database `cafe`:

```bash
```sql
create database cafe;
```

Select the database:

```
use cafe;
source resources/database/dump/cafe.sql;
exit;
```

Import data to the database from the SQL dump:
The path to the SQL dump might must be modified if you are not in the root directory of the project.

```bash
source resources/database/dump/cafe.sql
```
If you want to run unit tests with composer, you must first set up a separate database for testing. To do so, repeat the
same
instructions as above except name the testing database `cafe_test`:

The path to the SQL dump might must be modified if you are not in the root directory of the project.
```sql
create database cafe_test;
use cafe_test;
source resources/database/dump/cafe.sql;
exit;
```

## Setup testing database
## PHP setup

If you want to run tests for the application, you must set up a database for testing. To do so, repeat the same
instructions as the setup for the production database except name the testing database `cafe_test`.
Ensure that the [`variables_order`](https://www.php.net/manual/en/ini.core.php#ini.variables-) directive in
your `php.ini`
file is set to `"EGPCS"`. Without this, the application will
not be able to load environment variables properly in `src/core/config.php` and you will get an array key error.
You can use `php --ini` to find the location of your `php.ini` file.

## Setup linting and formatting
## Linting and formatting setup

This step is optional if you do not plan on editing the JS and CSS files. Node.js is required to install the linter and
This step is optional if you do not plan on editing the JS and CSS files. NPM is required to install the linter and
formatter for JS and CSS files. For more details on the linters and formatters used, see
our [coding standards](CODING_STANDARDS.md).

Expand Down
6 changes: 3 additions & 3 deletions resources/database/dump/cafe.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ CREATE TABLE `client` (
KEY `client_district_district_id_fk` (`district_id`),
CONSTRAINT `client_district_district_id_fk` FOREIGN KEY (`district_id`) REFERENCES `district` (`district_id`) ON UPDATE CASCADE,
CONSTRAINT `client_fk` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `city_length` CHECK (char_length(`city`) > 2),
CONSTRAINT `street_length` CHECK (char_length(`street`) > 3)
CONSTRAINT `client_city_length` CHECK (char_length(`city`) > 2),
CONSTRAINT `client_street_length` CHECK (char_length(`street`) > 3)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

Expand Down Expand Up @@ -330,4 +330,4 @@ UNLOCK TABLES;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2024-04-09 11:14:04
-- Dump completed on 2024-04-18 13:18:52
17 changes: 10 additions & 7 deletions src/core/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ private static function connect(): PDO
{
$string = "mysql:hostname=" . DB_HOST . ";dbname=" . DB_NAME;
try {
$pdo_object = new PDO($string, DB_USERNAME, DB_PASSWORD);
return new PDO($string, DB_USERNAME, DB_PASSWORD);
} catch (PDOException $e) {
// TODO: Create a page to display the error
Utility::show(
"Error establishing a connection to the database."
);
die();
// if PHPUnit is not running, handle the exception
if (!defined('PHPUNIT_STEAMY_TESTSUITE')) {
// TODO: Display a user-friendly error message
Utility::show("Sorry, we're unable to process your request at the moment. Please try again later.");
die();
} else {
// if PHPUnit is running, re-throw the exception to allow it to propagate
throw $e;
}
}
return $pdo_object;
}

/**
Expand Down

0 comments on commit b1f3049

Please sign in to comment.