Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CircleCI Config with database support #4

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
version: 2

jobs:
backend:
docker:
- image: circleci/php:7.2-stretch
environment:
ag44jc7aqs2rsup2bb6cx7utc: '127.0.0.1' # 'mysql-url'
hp7wz20wu4qfbfcmqywfai1j4: tempfiles # 'mysql-user'
mom8c5hrbn8c1r5lro1imfyax: thisisonlyusedfortestinganyways # 'mysql-password'
qb1yi60nrz3tjjjqqb7l2yqra: tempfiles # 'mysql-db'
rb421p9wniz81ttj7bdgrg0ub: tempfiles # 'mysql-table'
- image: circleci/mariadb:10-bionic-ram
environment:
MYSQL_DATABASE: tempfiles
MYSQL_USER: tempfiles
MYSQL_PASSWORD: thisisonlyusedfortestinganyways
steps:
- checkout

- restore_cache:
key: composer-cache-{{ checksum "backend/composer.lock" }}

- run:
name: Composer install
command: "cd backend && composer install"

- save_cache:
paths:
- backend/vendor
key: composer-cache-{{ checksum "backend/composer.lock" }}

- run:
name: Install PHP extensions
command: sudo docker-php-ext-install -j$(nproc) mysqli

- run:
name: Install MySQL Client
command: |
sudo apt update
sudo apt install -y mysql-client

- run:
name: Waiting for Mariadb to be ready
command: |
for i in `seq 1 10`;
do
nc -z localhost 3306 && echo Success && exit 0
echo -n .
sleep 1
done
echo Failed waiting for Mariadb && exit 1

- run:
name: Create database & table
command: mysql --protocol=tcp --host=localhost --port=3306 --user=$hp7wz20wu4qfbfcmqywfai1j4 --password=$mom8c5hrbn8c1r5lro1imfyax < resources/install_mysql.sql

- run:
name: "Run PHP Unit Tests"
command: "./backend/vendor/phpunit/phpunit/phpunit --configuration ./circleci/phpunit.xml --teamcity"

frontend:
docker:
- image: circleci/ruby:2
steps:
- checkout

- restore_cache:
keys:
- gem-cache-{{ checksum "frontend/Gemfile.lock" }}

- run:
name: Bundle install
command: cd frontend && bundle install --path vendor/bundle

- save_cache:
paths:
- frontend/vendor/bundle
key: gem-cache-{{ checksum "frontend/Gemfile.lock" }}

- run:
name: Build frontend
command: cd frontend && bundle exec rake proof build

# deploy:
# machine:
# enabled: true
# steps:
# - run:
# name: Trigger Webhook
# command: # curl?



workflows:
version: 2
test:
jobs:
- backend
- frontend
# - deploy:
# requires:
# - backend
# - frontend
# filters:
# branches:
# only: master
12 changes: 12 additions & 0 deletions .circleci/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<phpunit bootstrap="./backend/src/com/carlgo11/tempfiles/Autoload.php" colors="true">
<testsuites>
<testsuite name="tempfiles">
<directory>./backend/tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./backend/src</directory>
</whitelist>
</filter>
</phpunit>
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ matrix:
include:
- language: php
php:
- 7.2
- 7.3
- "7.2"
- "7.3"
before_install:
- cd backend
install:
Expand All @@ -20,7 +20,8 @@ matrix:
- 2.4.1
cache:
bundler: true
directories: $TRAVIS_BUILD_DIR/tmp/.htmlproofer
directories:
- $TRAVIS_BUILD_DIR/tmp/.htmlproofer
before_install:
- cd frontend
script:
Expand Down
3 changes: 3 additions & 0 deletions backend/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"require-dev": {
"phpunit/phpunit": "^8.1",
"phpunit/php-code-coverage": "^7.0"
},
"require": {
"ext-mysqli": "*"
}
}
5 changes: 3 additions & 2 deletions backend/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<phpunit bootstrap="src/com/carlgo11/tempfiles/Autoload.php" colors="true">
<testsuites>
<testsuite name="tempfiles">
<testsuite name="tempfiles-no-mysql">
<directory>tests</directory>
<exclude>tests/com/carlgo11/tempfiles/DataStorageTest.php</exclude>
</testsuite>
</testsuites>
<php>
<env name="phpunit" value="true"/>
<env name="ignore-mysql" value="true"/>
</php>
<filter>
<whitelist>
Expand Down
12 changes: 5 additions & 7 deletions backend/src/com/carlgo11/tempfiles/Autoload.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?php

// Load resources.

$conf = checkFile(__DIR__ . '/config.php');
checkFile(__DIR__ . '/DataStorage.php');
checkFile(__DIR__ . '/Encryption.php');
checkFile(__DIR__ . '/Misc.php');
checkFile(__DIR__ . '/File.php');
checkFile(__DIR__ . '/API.php');

// Load MySQL connection unless the script is run by PHPUnit.
if (!isset($_ENV['phpunit']))
$mysql_connection = mysqli_connect($conf['mysql-url'], $conf['mysql-user'], $conf['mysql-password'], $conf['mysql-db'])
or mySQLError($mysql_connection);

// Connect to the database unless run by test.
if(!isset($_ENV['ignore-mysql']))
$mysql_connection = mysqli_connect($conf['mysql-url'], $conf['mysql-user'], $conf['mysql-password'], $conf['mysql-db']) or mySQLError($mysql_connection);

/**
* Outputs MySQL error.
Expand All @@ -23,7 +20,8 @@
function mySQLError($mysql_connection) {
error_log('MySQL connection failed: ' . htmlspecialchars($mysql_connection->error));
http_response_code(500);
die("Connection to our database failed.");
die('Connection to our database failed.');
throw new Exeption('Connection to our database failed.');
}

/**
Expand Down
23 changes: 23 additions & 0 deletions backend/src/com/carlgo11/tempfiles/DataStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,27 @@ public static function uploadFile(File $file, string $password) {
}
}

// TODO: Remove. Just an example.
public static function getMariaDBVersion() {
global $mysql_connection;

try {

$query = $mysql_connection->prepare('SELECT VERSION()');

$query->execute();
$query->store_result();
$query->bind_result($version);
$query->fetch();
$query->close();

return $version;

} catch (Exception $e) {
error_log($e);
return FALSE;
}

}

}
15 changes: 7 additions & 8 deletions backend/src/com/carlgo11/tempfiles/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
* If you'd prefer to just store the credentials in this document then,
* replace my variables definitions with the ones I've commented.
*/

return array(
return [
# Allowed formats <n>MB, <n>GB, <n>TB, <n>PB.
'max-file-size' => '12MB',
#'mysql-url' => 'localhost',
# 'mysql-url' => 'localhost',
'mysql-url' => getenv('ag44jc7aqs2rsup2bb6cx7utc'),
#'mysql-user => 'tempfiles',
# 'mysql-user' => 'tempfiles',
'mysql-user' => getenv('hp7wz20wu4qfbfcmqywfai1j4'),
#'mysql-password' => 'password',
# 'mysql-password' => 'password',
'mysql-password' => getenv('mom8c5hrbn8c1r5lro1imfyax'),
#'mysql-db' => 'tempfiles',
# 'mysql-db' => 'tempfiles',
'mysql-db' => getenv('qb1yi60nrz3tjjjqqb7l2yqra'),
#'mysql-table' => 'files',
# 'mysql-table' => 'files',
'mysql-table' => getenv('rb421p9wniz81ttj7bdgrg0ub'),
# Encryption algorithm to use for encrypting uploads.
'Encryption-Method' => 'aes-256-gcm'
);
];
16 changes: 16 additions & 0 deletions backend/tests/com/carlgo11/tempfiles/DataStorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace com\carlgo11\tempfiles;

use com\carlgo11\tempfiles;
use PHPUnit\Framework\TestCase;

class DataStorageTest extends TestCase
{

// TODO: Remove. Just an example.
public function testGetMariaDBVersion() {
$this->assertIsString(DataStorage::getMariaDBVersion());
}

}