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

Add simple phpunit tests #1406

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* text=auto

/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/phpcs.xml.dist export-ignore
/tests export-ignore
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package-lock.json
/storage/*.*
/storage/*/*
/config/*
/tests/*
/node_modules
addons/
!.gitignore
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@
"config": {
"vendor-dir": "lib/vendor",
"optimize-autoloader": true
},

"require-dev": {
"phpunit/phpunit": "7"
}
}
21 changes: 21 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.0/phpunit.xsd"
bootstrap="tests/bootstrap.php"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
colors="true"
cacheResult="false"
verbose="true">
<php>
<ini name="display_errors" value="true"/>
<const name="COCKPIT_PHPUNIT" value="true" />
</php>
<testsuites>
<testsuite name="Cockpit Modules">
<directory>tests/modules</directory>
</testsuite>
</testsuites>
</phpunit>
17 changes: 17 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Make use of Cockpit autoloader to include it's own librararies.
*
* usage: open terminal and run { ./lib/vendor/bin/phpunit/ }
*
* @see https://phpunit.readthedocs.io/en/8.3/configuration.html#appendixes-configuration-phpunit-bootstrap
*/

/**
* @see { phpunit.xml.dist }
*/
if (!defined('COCKPIT_PHPUNIT') || !COCKPIT_PHPUNIT ) {
exit('Something goes wrong');
}

require_once __DIR__ . '/../bootstrap.php';
155 changes: 155 additions & 0 deletions tests/modules/FormsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
declare(strict_types=1);

namespace CockpitTests\Test;

use PHPUnit\Framework\TestCase;

/**
* Test Cockpit's \Forms
*/
class FormsTest extends TestCase {

/** @var string - Mock Form id **/
protected static $mockFormId;

/** @var array - Mock Form items **/
protected static $mockFormItems;

// Generate uniqe form id before starting tests
public static function setUpBeforeClass() {
static::$mockFormId = 'test-'.uniqid();
static::$mockFormItems = [
['uid' => uniqid(), 'email' => '[email protected]'],
['uid' => uniqid(), 'email' => '[email protected]'],
['uid' => uniqid(), 'email' => '[email protected]']
];
}

// Create "test" form before each test
protected function setUp() {
cockpit('forms:createForm', static::$mockFormId);
foreach (static::$mockFormItems as $data) {
cockpit('forms:save', static::$mockFormId, $data);
}
}

// Delete "test" form after each test
protected function tearDown() {
cockpit('forms:removeForm', static::$mockFormId);
}

// Clean things after all tests have been executed
public static function tearDownAfterClass() {
}

/**
* @covers \Forms::exists
*/
public function testExits() {
$path = cockpit('forms:exists', static::$mockFormId);
$this->assertTrue(!empty($path));
}

/**
* @covers \Forms::UpdateForm
*/
public function testUpdateForm() {
$form = cockpit('forms:updateForm', static::$mockFormId, ['label' => 'hello']);
$this->assertTrue($form['label'] === 'hello');
}

/**
* @covers \Forms::saveForm
*/
public function testSaveForm() {
$data = ['_id' => static::$mockFormId, 'label' => 'hello'];
$saved = cockpit('forms:saveForm', static::$mockFormId, $data);
$this->assertTrue($saved['label'] === 'hello');
}

/**
* @covers \Forms::removeForm
*/
public function testRemoveForm() {
$removed = cockpit('forms:removeForm', static::$mockFormId);
$this->assertTrue($removed);
}

/**
* @covers \Forms::forms
*/
public function testForms() {
$forms = cockpit('forms:forms', static::$mockFormId.'-'.uniqid());
$this->assertTrue(!empty($forms) && is_array($forms));
}

/**
* @covers \Forms::form
*/
public function testForm() {
$form = cockpit('forms:form', static::$mockFormId);
$this->assertTrue(!empty($form) && $form['_id'] === static::$mockFormId);
}

/**
* @covers \Forms::entries
*/
public function testEntries() {
$entries = cockpit('forms:entries', static::$mockFormId);
$this->assertTrue($entries instanceof \MongoLite\Collection);
}

/**
* @covers \Forms::find
*/
public function testFind() {
$entries = cockpit('forms:find', static::$mockFormId);
$this->assertTrue(count($entries) === count(static::$mockFormItems));
}

/**
* @covers \Forms::findOne
*/
public function testFindOne() {
$criteria = [ 'uid' => static::$mockFormItems[0]['uid'] ];
$entry = cockpit('forms:findOne', static::$mockFormId);
$this->assertTrue($entry['uid'] === $criteria['uid']);
}

/**
* @covers \Forms::save
*/
public function testSave() {
$data = ['uid' => uniqid(), 'email' => '[email protected]'];
$saved = cockpit('forms:save', static::$mockFormId, $data);
$this->assertTrue($saved['uid'] === $data['uid'] && $saved['email'] === $data['email']);
}

/**
* @covers \Forms::remove
*/
public function testRemove() {
$criteria = [ 'uid' => static::$mockFormItems[0]['uid'] ];
$removed = cockpit('forms:remove', static::$mockFormId, $criteria);
$this->assertTrue($removed === 1);
}

/**
* @covers \Forms::count
*/
public function testCount() {
$entries = cockpit('forms:count', static::$mockFormId);
$this->assertTrue($entries === count(static::$mockFormItems));
}

/**
* @covers \Forms::submit
*/
public function testSubmit() {
$data = ['uid' => uniqid(), 'email' => '[email protected]'];
$response = cockpit('forms:submit', static::$mockFormId, $data);
$this->assertTrue($response === $data);
}

}