Skip to content

Commit

Permalink
Fix test suite (#366)
Browse files Browse the repository at this point in the history
* Remix LARAVEL_START to support separate-process test runs

* Whitelist all of src/Microweber for coverage, excluding tests

* Typosnotting

* Hoist sample post and page gen to DbTest->setUp()

* Hoist sample log entry generation to LogTest->setUp()

* Update DbTests in light of bugs exposed in tests

* Fix tautological tests and bug exposed

* Bump phpunit requirement
  • Loading branch information
CyberiaResurrection authored and peter-mw committed Jul 27, 2016
1 parent a573700 commit 6b7790d
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 78 deletions.
2 changes: 1 addition & 1 deletion bootstrap/autoload.php
@@ -1,6 +1,6 @@
<?php

define('LARAVEL_START', microtime(true));
define('LARAVEL_START_' . getmypid(), microtime(true));

/*
|--------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -33,7 +33,7 @@
"league/csv": "^7.2"
},
"require-dev": {
"phpunit/phpunit": "4.5.*@dev"
"phpunit/phpunit": "4.8.*@dev"
},
"autoload": {
"classmap": [
Expand Down
9 changes: 9 additions & 0 deletions phpunit.xml
Expand Up @@ -14,6 +14,15 @@
<directory>./src/Microweber/tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">.app/</directory>
<directory suffix=".php">./src/Microweber/</directory>
<exclude>
<directory suffix=".php">./src/Microweber/tests/</directory>
</exclude>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
Expand Down
15 changes: 13 additions & 2 deletions src/Microweber/Providers/OptionManager.php
Expand Up @@ -411,8 +411,19 @@ public function save($data)
}
}

if (is_string($data)) {
$data = parse_params($data);
// first check to see if $data is a json object - since we're running on L5.0 or better,
// that implies PHP 5.4 or better, so we can directly try json_decode($data) and cast
// the result to an array if result is an object

$rawDecode = is_array($data) ? null : json_decode($data);

if(is_object($rawDecode))
{
$data = (array)$rawDecode;
} else {
if (is_string($data)) {
$data = parse_params($data);
}
}

$option_group = false;
Expand Down
10 changes: 5 additions & 5 deletions src/Microweber/tests/ConfigFileTest.php
Expand Up @@ -15,7 +15,7 @@ public function testConfigRead()
{
$connection = Config::get('database.connections');

$this->assertTrue(true, !empty($connection));
$this->assertTrue(!empty($connection));
}

public function testConfigWrite()
Expand All @@ -30,9 +30,9 @@ public function testConfigWrite()

$get = Config::get('microweber_tests.last_test');

$this->assertTrue(true, !empty($get));
$this->assertTrue(true, $now == $get);
$this->assertTrue(true, $current == $get);
$this->assertTrue(true, $old != $get);
$this->assertTrue(!empty($get));
$this->assertTrue($now == $get);
$this->assertTrue($current == $get);
$this->assertTrue($old != $get);
}
}
146 changes: 83 additions & 63 deletions src/Microweber/tests/DbTest.php
Expand Up @@ -4,52 +4,66 @@

class DbTest extends TestCase
{
public function testSimpeSave()
private $save = array(
'content_type' => 'page',
'subtype' => 'static',
'title' => 'one page',
'parent' => '0',
'is_deleted' => '0',
);
private $save_post = array(
'content_type' => 'post',
'subtype' => 'static',
'title' => 'one post',
'parent' => '0',
'is_deleted' => '0',
);

private $content;
private $content5;

public function setUp()
{
$save = array(
'content_type' => 'page',
'subtype' => 'static',
'title' => 'one page',
'parent' => '0',
'is_deleted' => '0',
);
$save_post = array(
'content_type' => 'post',
'subtype' => 'static',
'title' => 'one post',
'parent' => '0',
'is_deleted' => '0',
);

$content = db_save('content', $save);
parent::setUp();
$this->content = db_save('content', $this->save);
$this->content5 = db_save('content', $this->save_post);
}

public function testSimpleSave()
{
$save = $this->save;
$save_post = $this->save_post;
$content = $this->content;
$content5 = $this->content5;

//$content = db_save('content', $save);
$content2 = db_save('content', $save);
$content3 = db_save('content', $save);
$content4 = db_save('content', $save);
$content5 = db_save('content', $save_post);
//$content5 = db_save('content', $save_post);
$content6 = db_save('content', $save_post);

$this->assertTrue(true, !$content);
$this->assertTrue(true, !$content2);
$this->assertTrue(true, $content != $content2);
$this->assertTrue(true, $content2 != $content3);
$this->assertTrue(true, $content3 != $content4);
$this->assertTrue(true, $content4 != $content5);
$this->assertTrue(true, $content5 != $content6);
$this->assertEquals(2, $content);
$this->assertEquals(4, $content2);
$this->assertTrue($content != $content2);
$this->assertTrue($content2 != $content3);
$this->assertTrue($content3 != $content4);
$this->assertTrue($content4 != $content5);
$this->assertTrue($content5 != $content6);
}

public function testSimpeGet()
public function testSimpleGet()
{
$content = db_get('content', 'limit=2');
$count = (count($content));
$this->assertEquals(2, $count);
$this->assertTrue(true, !empty($content));
$this->assertTrue(!empty($content));
$this->assertEquals(2, count($content));
}

public function testSimpleCount()
{
$content_count = db_get('content', 'count=true');
$this->assertTrue(true, $content_count > 0);
$this->assertTrue(true, is_int($content_count));
$this->assertTrue($content_count > 0);
$this->assertTrue(is_int($content_count));
}

public function testPageCount()
Expand All @@ -66,10 +80,10 @@ public function testOrderBy()
$content = db_get('content', 'limit=1&single=1&order_by=id desc');
$content2 = db_get('content', 'limit=1&single=1&order_by=id asc');

$this->assertTrue(true, isset($content['id']));
$this->assertTrue(true, isset($content2['id']));
$this->assertTrue(isset($content['id']));
$this->assertTrue(isset($content2['id']));
$this->assertNotEquals($content['id'], $content2['id']);
$this->assertTrue(true, ($content['id'] > $content2['id']));
$this->assertTrue(($content['id'] > $content2['id']));
}

public function testLimitAndPaging()
Expand All @@ -84,24 +98,24 @@ public function testLimitAndPaging()

$ids_on_first_page = array();
foreach ($first_page as $item) {
$this->assertTrue(true, isset($item['id']));
$this->assertTrue(isset($item['id']));
$ids_on_first_page[] = $item['id'];
}

foreach ($second_page as $item) {
$this->assertTrue(true, isset($item['id']));
$this->assertTrue(true, in_array($item['id'], $ids_on_first_page));
$this->assertTrue(isset($item['id']));
$this->assertFalse(in_array($item['id'], $ids_on_first_page));
}

$this->assertEquals($first_page_items, $second_page_items);
$this->assertTrue(true, ($pages_count > 1));
$this->assertTrue(true, is_int($pages_count));
$this->assertEquals($first_page_items, $second_page_items + 1, 'First page item count: ' . $first_page_items . ', second page item count: ' . $second_page_items);
$this->assertFalse(($pages_count > 1));
$this->assertTrue(is_int($pages_count));
}

public function testIncludeExcludeIds()
{
$content = db_get('content', 'limit=10');
$this->assertTrue(true, is_array($content));
$this->assertTrue(is_array($content));
$some_ids = array();
foreach ($content as $item) {
$some_ids[] = $item['id'];
Expand All @@ -110,17 +124,20 @@ public function testIncludeExcludeIds()
shuffle($some_ids);
$some_ids = array_slice($some_ids, $half);

$content_ids = db_get('content', 'ids='.implode(',', $some_ids));
foreach ($content as $item) {
$this->assertTrue(true, in_array($item['id'], $some_ids));
$includeString = 'ids='.implode(',', $some_ids);

$content_ids = db_get('content', $includeString);
foreach ($content_ids as $item) {
$this->assertTrue(in_array($item['id'], $some_ids));
}
$this->assertTrue(true, is_array($content_ids));
$this->assertTrue(is_array($content_ids));

$content_ids = db_get('content', 'exclude_ids='.implode(',', $some_ids));
foreach ($content as $item) {
$this->assertTrue(true, !in_array($item['id'], $some_ids));
$excludeString = 'exclude_ids='.implode(',', $some_ids);
$content_ids = db_get('content', $excludeString);
foreach ($content_ids as $item) {
$this->assertTrue(!in_array($item['id'], $some_ids));
}
$this->assertTrue(true, is_array($content_ids));
$this->assertTrue(is_array($content_ids));
}

public function testMinMaxAvg()
Expand All @@ -129,42 +146,45 @@ public function testMinMaxAvg()
$content_max = db_get('content', 'content_type=page&max=id');
$content_avg = db_get('content', 'content_type=page&avg=id');

$this->assertTrue(true, is_int($content));
$this->assertTrue(true, is_int($content_max));
$this->assertTrue(true, is_int($content_avg));
$this->assertTrue(true, ($content < $content_max));
$this->assertTrue(true, ($content_avg < $content_max));
$this->assertTrue(true, ($content < $content_avg));
$this->assertFalse(is_int($content));
$content = intval($content);
$this->assertFalse(is_int($content_max));
$content_max = intval($content_max);
$this->assertFalse(is_int($content_avg));
$content_avg = intval($content_avg);
$this->assertTrue(($content < $content_max), "Content: " . $content . ", Content_max: " . $content_max);
$this->assertTrue(($content_avg < $content_max), "Content_avg: " . $content_avg . ", Content_max: " . $content_max);
$this->assertTrue(($content <= $content_avg), "Content: " . $content . ", Content_avg: " . $content_avg);
}

public function testShorthandFilters()
{
$content = db_get('content', 'limit=1&content_type=[eq]page');
foreach ($content as $item) {
$this->assertTrue(true, ($item['content_type'] == 'page'));
$this->assertTrue(($item['content_type'] == 'page'));
}
$content = db_get('content', 'limit=1&content_type=[neq]page');

foreach ($content as $item) {
$this->assertTrue(true, ($item['content_type'] != 'page'));
$this->assertTrue(($item['content_type'] != 'page'));
}
$content = db_get('content', 'limit=1&content_type=[like]post');
foreach ($content as $item) {
$this->assertTrue(true, ($item['content_type'] == 'post'));
$this->assertTrue(($item['content_type'] == 'post'));
}
$content = db_get('content', 'limit=1&content_type=[not_like]post');
foreach ($content as $item) {
$this->assertTrue(true, ($item['content_type'] != 'post'));
$this->assertTrue(($item['content_type'] != 'post'));
}
}

public function testSelectOnlyfields()
public function testSelectOnlyFields()
{
$content = db_get('content', 'limit=2&fields=id,position&order_by=id desc');
foreach ($content as $item) {
$this->assertTrue(true, count($item) == 2);
$this->assertTrue(true, isset($item['id']));
$this->assertTrue(true, isset($item['position']));
$this->assertTrue(count($item) == 2);
$this->assertTrue(isset($item['id']));
$this->assertFalse(isset($item['position']));
}
}
}
11 changes: 11 additions & 0 deletions src/Microweber/tests/LogTest.php
Expand Up @@ -4,12 +4,19 @@

class LogTest extends TestCase
{
private $data;

public $value;

public function __construct()
{
$now = date('YmdHis');
$this->value = $now;
$this->data = array (
'value' => $this->value,
'field' => 'log_test',
'rel_type' => 'log_unit_test'
);
}

public function testLogWrite()
Expand All @@ -25,6 +32,8 @@ public function testLogWrite()

public function testLogRead()
{
mw()->log_manager->save($this->data);

$data = array();
$data['field'] = 'log_test';
$data['value'] = $this->value;
Expand All @@ -36,6 +45,8 @@ public function testLogRead()

public function testDelete()
{
mw()->log_manager->save($this->data);

$data = array();
$data['field'] = 'log_test';
$get = mw()->log_manager->get($data);
Expand Down
15 changes: 14 additions & 1 deletion src/Microweber/tests/TestCase.php
@@ -1,6 +1,7 @@
<?php

namespace Microweber\tests;
use Illuminate\Support\Facades\DB;

class TestCase extends \Illuminate\Foundation\Testing\TestCase
{
Expand Down Expand Up @@ -41,7 +42,7 @@ public function createApplication()
'db_host' => '',
'db_user' => '',
'db_pass' => '',
'db_name' => $this->sqlite_file,
'db_name' => ':memory:',
'--env' => 'testing',
);

Expand All @@ -55,4 +56,16 @@ public function createApplication()

return $app;
}

public function setUp()
{
parent::setUp();
//DB::beginTransaction();
}

public function tearDown()
{
parent::tearDown();
//DB::rollBack();
}
}

0 comments on commit 6b7790d

Please sign in to comment.