Skip to content

Commit

Permalink
Working on tests & demo-app
Browse files Browse the repository at this point in the history
  • Loading branch information
globotech-acc committed Jan 19, 2016
1 parent 497c290 commit 80bde10
Show file tree
Hide file tree
Showing 12 changed files with 506 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Presentation\Framework\Demo;


require __DIR__ . '/../vendor/autoload.php';

define('FIXTURES_DIR', __DIR__ . '/fixtures');

use Dotenv;
use PDO;

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

Dotenv::load(__DIR__);
Dotenv::required([
'DB_DSN',
'DB_NAME',
'DB_USER',
'DB_PASSWORD'
]);

function is_sqlite()
{
return strpos(getenv('DB_DSN'), 'sqlite:') !== false;
}

function db_connection() {
static $db;
if ($db === null) {
$dsn = getenv('DB_DSN');

$selectDb = !is_sqlite();
if ($selectDb) {
$dbName = getenv('DB_NAME');
$dsn.=";dbname=$dbName";
}
$db = new PDO(
$dsn,
getenv('DB_USER'),
getenv('DB_PASSWORD'),
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => 1
]
);
}
return $db;
}
chdir(__DIR__);
40 changes: 40 additions & 0 deletions tests/fixtures/db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
DROP DATABASE IF EXISTS pf_laravel_demo;
CREATE DATABASE IF NOT EXISTS pf_laravel_demo;
USE pf_laravel_demo;

DROP TABLE IF EXISTS users;
CREATE TABLE IF NOT EXISTS users (
id int(10) NOT NULL,
name varchar(255) NOT NULL,
role varchar(31) NOT NULL,
birthday date NOT NULL,
PRIMARY KEY (id)
);

DELETE FROM users;

INSERT INTO users VALUES (1, 'John', 'Admin', '1970-01-16');
INSERT INTO users VALUES (2, 'Max', 'Manager', '1980-11-20');
INSERT INTO users VALUES (3, 'Anna', 'Manager', '1987-03-30');
INSERT INTO users VALUES (4, 'Lisa', 'User', '1989-04-21');
INSERT INTO users VALUES (5, 'Eric', 'User', '1990-10-23');

INSERT INTO users VALUES (6, 'David', 'User', '1990-10-23');
INSERT INTO users VALUES (7, 'Bruce', 'User', '1977-09-14');
INSERT INTO users VALUES (8, 'Julia', 'User', '1994-03-05');
INSERT INTO users VALUES (9, 'Ben', 'User', '1991-11-13');
INSERT INTO users VALUES (10, 'Frank', 'Manager', '1964-10-29');
INSERT INTO users VALUES (11, 'Phil', 'User', '1972-08-17');
INSERT INTO users VALUES (12, 'Nikita', 'User', '1973-04-17');
INSERT INTO users VALUES (13, 'Steven', 'User', '1983-03-21');
INSERT INTO users VALUES (14, 'Ross', 'User', '1982-07-14');
INSERT INTO users VALUES (15, 'Sammy', 'User', '1982-07-24');

INSERT INTO users VALUES (16, 'Victor', 'User', '1979-01-23');
INSERT INTO users VALUES (17, 'Martin', 'Manager', '2001-01-04');
INSERT INTO users VALUES (18, 'Florin', 'User', '2002-06-06');
INSERT INTO users VALUES (19, 'Diego', 'User', '1987-05-11');
INSERT INTO users VALUES (20, 'Robert', 'Admin', '1984-02-01');
INSERT INTO users VALUES (21, 'Peter', 'User', '1994-05-12');
INSERT INTO users VALUES (22, 'Sebastian', 'User', '1991-11-16');
INSERT INTO users VALUES (23, 'Rafael', 'User', '1993-05-04');
14 changes: 14 additions & 0 deletions tests/laravel_bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
require __DIR__ . '/bootstrap.php';

use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'sqlite',
'database' => __DIR__ . '/db.sqlite',
'prefix' => '',
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
// set timezone for timestamps etc
date_default_timezone_set('UTC');
88 changes: 88 additions & 0 deletions tests/phpunit/Acceptance/AbstractTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
namespace Presentation\Laravel\Test\Acceptance;

use Exception;
use GuzzleHttp\Client;
use PHPUnit_Framework_TestCase;

abstract class AbstractTest extends PHPUnit_Framework_TestCase
{
/**
* @var Client
*/
protected $client;
protected $baseUri = 'http://localhost:8000';
protected $connectionChecked = false;
protected $process;
public function setUp()
{
$this->client = new Client([
'base_uri' => $this->baseUri,
]);


$this->checkConnection();
}

protected function startServer()
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
echo "\r\n===[ Starting WEB-SERVER... ]===\r\n";
$this->process = popen("start php -S localhost:8000 tests/webapp/index.php", "r");
} else {
echo "\r\n===[ Can't start WEB-SERVER, disabled for unix ]===\r\n";
$this->process = false;
}
}

public function tearDown()
{
if ($this->process) {
pclose($this->process);
}
}

protected function checkConnection()
{
if ($this->connectionChecked) {
return;
}
$this->connectionChecked = true;
try {
if ($this->client->get('/')->getStatusCode() !== 200) {
throw new Exception();
}
} catch(Exception $e) {
if ($this->process === null) {
$this->startServer();
if ($this->process !== null) {
$this->checkConnection();
return;
}
}
$this->markTestSkipped("Can't find working server at $this->baseUri");
}
}

protected function get($uri, $options = [])
{
return $this->client->get($uri, $options)->getBody()->getContents();
}

protected function assertPageContains($expected, $uri, $options = [])
{
self::assertContains($expected, $this->get($uri, $options));
}

protected function assertPageWorks($uri, $options = [])
{
self::assertTrue($this->client->get($uri, $options)->getStatusCode() == 200);
}

protected function assertPagesWorks(array $uris)
{
foreach($uris as $uri) {
$this->assertPageWorks($uri);
}
}
}
18 changes: 18 additions & 0 deletions tests/phpunit/Acceptance/ServerRunTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Presentation\Laravel\Test\Acceptance;

use PHPUnit_Framework_TestCase;
use Presentation\Laravel\Demo\Controller;

class ServerRunTest extends AbstractTest
{
public function testPages()
{
$this->assertPagesWorks(get_class_methods(Controller::class));
}

public function testIndex()
{
$this->assertPageContains('Laravel Integration', '/');
}
}
26 changes: 26 additions & 0 deletions tests/phpunit/Data/DbTableProcessingServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Presentation\Laravel\Test\Data;

use Presentation\Framework\Data\OperationsCollection;
use Presentation\Framework\Test\Data\AbstractProcessingServiceTest;
use Presentation\Laravel\Data\EloquentProcessingService;
use Presentation\Laravel\Data\EloquentProcessorResolver;
use Presentation\Laravel\Demo\Model\User;

require __DIR__ .'/../../../vendor/presentation/framework/tests/phpunit/Data/AbstractProcessingServiceTest.php';

class DbTableProcessingServiceTest extends AbstractProcessingServiceTest
{
public function setUp()
{
$this->data = (new User())->newQuery();
$this->operations = new OperationsCollection();
$this->service = new EloquentProcessingService(
new EloquentProcessorResolver(),
$this->operations,
$this->data
);
$this->totalCount = (new User())->newQuery()->get()->count();
}
}
42 changes: 42 additions & 0 deletions tests/webapp/AbstractController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
namespace Presentation\Laravel\Demo;

use Presentation\Framework\Base\ComponentInterface;
use ReflectionClass;
use ReflectionMethod;

abstract class AbstractController
{
/**
* @return \ReflectionMethod[]
*/
protected function getActions()
{
$class = new ReflectionClass($this);
return $class->getMethods(ReflectionMethod::IS_PUBLIC);

}

protected function render($tpl, array $data = [])
{
extract($data);
ob_start();
$resourcesDir = __DIR__ . '/resources';
include "$resourcesDir/views/$tpl.php";
return ob_get_clean();
}


protected function renderMenu()
{
return $this->render('menu/menu');
}

protected function page($content, $title = '')
{
if ($content instanceof ComponentInterface) {
$content = $content->render();
}
return $this->render('layout', compact('content', 'title'));
}
}
Loading

0 comments on commit 80bde10

Please sign in to comment.