Submit Your Project / Idea

We are actively looking for cool ideas of packages / projects to develop. Have an idea, or need a specific package developed that you're having trouble finding elsewhere? Drop a message on the Reddit forum and let us know!

Your idea may get developed free of charge and open sourced, or maybe will be developed and released commercially for a small license fee, or maybe will spawn a great discussion and even better ideas! If your idea is picked up, it will be listed on the Projects page.

Jump in and share your ideas! Don't be left behind, get modern and professional with Apex!

Visit Reddit Forum

Admin Screenshot

Apex - Training Create Unit Test

Last thing before we publish our package to the repository, we need to make sure the code works be developing a quick unit test. Within terminal, type:

./apex create test training:admin

This will create a new file at /tests/training/admin_test.php. Open the file, and enter the following contents:

<?php
declare(strict_types = 1);

namespace tests\training;

use apex\app;
use apex\svc\db;
use apex\svc\debug;
use apex\svc\auth;
use apex\svc\components;
use apex\app\tests\test;


/**
 * Handles the unit tests for the training package.
 */
class test_admin extends test
{

/**
 * setUp
 */
public function setUp():void
{

    // Get app
    if (!$app = app::get_instance()) { 
        $app = new app('test');
    }

}

/**
 * tearDown
 */
public function tearDown():void
{

}

/**
 * Login to admin panel.
 */
public function test_login()
{

    // Login
    app::set_area('admin');
    auth::auto_login(1);

    // Assert true, just so phpUnit doesn't throw a warning
    $this->assertTrue(true);


}

/**
 * Page -- Admin -> Settings -> Lottery
 */
public function test_page_admin_settings_lottery()
{

    // Ensure page loads
    $html = $this->http_request('admin/settings/lottery');
    $this->assertPageTitle('Lottery Settings');
    $this->assertHasFormField('daily_award');
    $this->assertHasSubmit('update', 'Update Lottery Settings');

    // Set request to change settings
    $orig_award = app::_config('training:daily_award');
    $request = array(
        'daily_award' => '75', 
        'submit' => 'update'
    );

    // Send http request
    $html = $this->http_request('admin/settings/lottery', 'POST', $request);
    $this->assertPageTitle('Lottery Settings');
    $this->assertHasCallout('success', 'Successfully updated');
    $this->assertHasSubmit('update', 'Update Lottery Settings');

}

/**
 * Check the crontab job
 */
public function test_crontab_pick_winner()
{

    // Load cron job
    $userid = components::call('process', 'cron', 'pick_winner', 'training');

    // Check database row
    $row = db::get_field("SELECT * FROM lotteries WHERE userid = %i AND DATE(date_added) = DATE(now())", $userid);
    $this->assertNotFalse($row);

}

}

The above class doesn't cover all functionality, but is just used as a training example. It contains two methods to test both, the Settings->Lottery menu of the administration panel, and the crontab job that we created. You may view all details on unit tests within Apex at the Unit Tests page of the documentation.

Execute Unit Tests

To execute the unit tests, the first thing you should probably do is quickly modify the /phpunit.xml file, and change line # 12 to:

<directory suffix="_test.php">./tests/training</directory>

This will ensure only unit tests within our "training" package will be executed. To execute the unit tests, within terminal type:

./vendor/phpunit/phpunit/phpunit

This will automatically execute all unit tests, and should come back as successful.

Next

Now that both, development and unit tests are complete, and let's go ahead and Publish the Package to our repository.

<