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

WIP/DNM - Feature/api unit tests #113

Open
wants to merge 2 commits into
base: develop
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
48 changes: 48 additions & 0 deletions tests/Support/Fixtures/site-trees/default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* Default site tree. If added to a new site, has the structure:
* /
* /undergraduates
* /undergraduates/2017
* /undergraduates/2018
* /postgraduates
* /postgraduates/2017
* /postgraduates/2018
*/
return [
[
'slug' => 'undergraduate',
'title' => 'Undergraduates',
'layout' => ['name' => 'test-layout', 'version' => 1],
'children' => [
[
'slug' => '2017',
'title' => '2017 Entry',
'layout' => ['name' => 'test-layout', 'version' => 1]
],
[
'slug' => '2018',
'title' => '2018 Entry',
'layout' => ['name' => 'test-layout', 'version' => 1]
],
]
],
[
'slug' => 'postgraduate',
'title' => 'Postgraduates',
'layout' => ['name' => 'test-layout', 'version' => 1],
'children' => [
[
'slug' => '2017',
'title' => '2017 Entry',
'layout' => ['name' => 'test-layout', 'version' => 1]
],
[
'slug' => '2018',
'title' => '2018 Entry',
'layout' => ['name' => 'test-layout', 'version' => 1]
],
]
]
];
44 changes: 44 additions & 0 deletions tests/Unit/Models/APICommands/APICommandTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@

use App\Models\Contracts\APICommand;
use App\Models\LocalAPIClient;
use App\Models\PublishingGroup;
use Exception;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Config;
use Tests\TestCase;
use Illuminate\Support\Facades\Validator;
use App\Models\User;

/**
* Utility methods for testing the API Commands.
* Includes methods for setting up test fixtures.
* @package Tests\Unit\Models\APICommands
*/
abstract class APICommandTestCase extends TestCase
{
// wraps all database calls within a test in a transaction.
use DatabaseTransactions;

/**
* @var array A valid (existing) layout name and version
*/
Expand All @@ -22,15 +32,49 @@ abstract class APICommandTestCase extends TestCase
];

/**
* Should be implemented to return an array of input data which would be valid for the command
* being tested.
* Used by @see APICommandTestCase::input() to get some valid default data to use in a test.
* @return array Valid input data.
*/
abstract public function getValidData();

/**
* Should return an instance of the command class under test.
* @return APICommand A new instance of the class to test.
*/
abstract public function fixture();

/**
* Create a site with an initial draft page hierarchy to use for testing.
* @param LocalAPIClient $api Used to create the site, etc.
* @param $structure
* @param string $name - The name for the site to add.
* @return Site - The created Site.
*/
public function setupSite(LocalAPIClient $api, $structure, $name)
{
$pubgroup = PublishingGroup::create(['name' => 'test']);
try{
$site = $api->createSite($pubgroup->id, "Test", "dfgkent.ac.uk", "", [ "name" => "test-layout", "version" => 1]);
}catch(\Exception $e){
dd($e);
}
$api->addTree($site->homepage->id, null, $structure);
return $site;
}

/**
* Loads a site structure definition to use with $api->addTree from a php file.
* Assumes that the file returns an array.
* @param string $name - The name of the file without the .php extension.
* @return array - An array defining the site hierarchy.
*/
public function loadSiteStructure($name)
{
return require(dirname(__FILE__) . '/../../../Support/Fixtures/site-trees/' . $name . '.php');
}

/**
* Get an API Client to use.
* @param Authenticatable|null $user Optional Authenticatable. If null, will be auto-provided.
Expand Down
6 changes: 5 additions & 1 deletion tests/Unit/Models/APICommands/AddPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public function validation_whenParentID_IsMissing()
*/
public function validation_whenParent_existsButIsNotDraft_fails()
{
$this->markTestIncomplete();
$api = $this->api();
$site = $this->setupSite($api, $this->loadSiteStructure('default'), 'Test');
$api->publishPage($site->homepage->id);
$api->publishPage($site->homepage->children()->first()->id);

}

/**
Expand Down