Skip to content

Commit

Permalink
Testing with docker
Browse files Browse the repository at this point in the history
  • Loading branch information
mbonneau committed Dec 14, 2019
1 parent 67c2060 commit c508ea7
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/composer.lock
/vendor
/docker/database
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4

services:
- postgresql
- docker

install:
- composer install

before_script:
- docker-compose -f docker/docker-compose.yml up -d
- sh docker/waitForPostgres.sh

script:
- vendor/bin/phpunit
15 changes: 15 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3.7'

services:
pgasync-postgres:
image: postgres:11
environment:
- PGDATA=/database
- POSTGRES_PASSWORD=some_password
- TZ=America/New_York
volumes:
- .:/app
- ./database:/database
- ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
ports:
- "5432:5432"
17 changes: 17 additions & 0 deletions docker/docker-entrypoint-initdb.d/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
set -x

echo "Running as $USER in $PWD"

createuser -U postgres --createdb pgasync
createuser -U postgres --createdb pgasyncpw
psql -U postgres -c "ALTER ROLE pgasyncpw PASSWORD 'example_password'"

cd /app
cp pg_hba_new.conf database/pg_hba.conf

createdb -U pgasync pgasync_test

psql -U pgasync -f test_db.sql pgasync_test


16 changes: 16 additions & 0 deletions docker/pg_hba_new.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# TYPE DATABASE USER ADDRESS METHOD

# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust

host all pgasync all trust
host all all all md5
21 changes: 21 additions & 0 deletions docker/test_db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CREATE TABLE thing (
id SERIAL,
thing_type varchar(50),
thing_description TEXT,
thing_cost decimal(10,4),
thing_in_stock bool
);

INSERT INTO thing(thing_type, thing_description, thing_cost, thing_in_stock)
VALUES('pen', NULL, 50.23, 'f');
INSERT INTO thing(thing_type, thing_description, thing_cost, thing_in_stock)
VALUES('pencil', 'something you write with', 27.50, null);
INSERT INTO thing(thing_type, thing_description, thing_cost, thing_in_stock)
VALUES('marker', NULL, 50.23, 't');

CREATE TABLE test_bool_param (
id serial not null,
b boolean,
primary key(id)
);
insert into test_bool_param(b) values(true);
22 changes: 22 additions & 0 deletions docker/waitForPostgres.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

echo "Waiting for database..."

ROW_COUNT=0

TRY_COUNT=0

while [ "$ROW_COUNT" -ne 1 ]; do
if [ "$TRY_COUNT" -ge 60 ]; then
echo "Timeout waiting for database..."
exit 1;
fi
sleep 5
TRY_COUNT=$(($TRY_COUNT+1))
echo "Attempt $TRY_COUNT..."
if ! ROW_COUNT=$(docker exec docker_pgasync-postgres_1 psql -U postgres pgasync_test -c "select count(*) from test_bool_param" -A -t); then
ROW_COUNT=0
fi
done

echo "Database is up..."
41 changes: 41 additions & 0 deletions tests/Integration/Md5PasswordTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace PgAsync\Tests\Integration;

use PgAsync\Client;
use Rx\Observer\CallbackObserver;

class Md5PasswordTest extends TestCase
{
public function testMd5Login()
{
$client = new Client([
"user" => "pgasyncpw",
"database" => $this->getDbName(),
"auto_disconnect" => true,
"password" => "example_password"
], $this->getLoop());

$hello = null;

$client->query("SELECT 'Hello' AS hello")
->subscribe(new CallbackObserver(
function ($x) use (&$hello) {
$this->assertNull($hello);
$hello = $x['hello'];
},
function ($e) {
$this->fail('Unexpected error');
},
function () {
$this->getLoop()->addTimer(0.1, function () {
$this->stopLoop();
});
}
));

$this->runLoopWithTimeout(2);

$this->assertEquals('Hello', $hello);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TestCase extends BaseTestCase
/** @var Timer */
public static $timeoutTimer;

public static $dbUser = "";
public static $dbUser = 'pgasync';

public static function getLoop()
{
Expand Down
17 changes: 0 additions & 17 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,3 @@
} else {
throw new RuntimeException('Install dependencies to run test suite.');
}

\PgAsync\Tests\TestCase::setDbUser(getenv('USER'));
if (getenv('TRAVIS') === 'true') {
\PgAsync\Tests\TestCase::setDbUser('postgres');
}

// cleanup remnants if there are any
exec('dropdb --if-exists ' . \PgAsync\Tests\TestCase::getDbName() . " -U '" . \PgAsync\Tests\TestCase::getDbUser() . "'");

// Create the Test database
exec("psql -c 'create database " . \PgAsync\Tests\TestCase::getDbName() . ";' -U '" . \PgAsync\Tests\TestCase::getDbUser() . "'");

exec('psql -f ' . __DIR__ . '/test_db.sql ' . \PgAsync\Tests\TestCase::getDbName() . ' ' . \PgAsync\Tests\TestCase::getDbUser());

register_shutdown_function(function () {
exec('dropdb --if-exists ' . \PgAsync\Tests\TestCase::getDbName() . " -U '" . \PgAsync\Tests\TestCase::getDbUser() . "'");
});

0 comments on commit c508ea7

Please sign in to comment.