Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Eichhorn committed Sep 25, 2023
1 parent 442cc71 commit e0f9878
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 48 deletions.
33 changes: 4 additions & 29 deletions Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,18 @@ public function apiProfileCreate(RequestAbstract $request, ResponseAbstract $res
{
$profiles = $this->createProfilesFromRequest($request);
$created = [];
$status = true;

foreach ($profiles as $profile) {
$status = $status && $this->apiProfileCreateDbEntry($profile, $request);
if ($profile->id === 0) {
$this->createModel($request->header->account, $profile, ProfileMapper::class, 'profile', $request->getOrigin());
}

$created[] = $profile;
}

if (!$status) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidCreateResponse($request, $response, $created);

return;
}

$this->createStandardCreateResponse($request, $response, $created);
}

/**
* @param Profile $profile Profile to create in the database
* @param RequestAbstract $request Request
*
* @return bool
*
* @since 1.0.0
*/
public function apiProfileCreateDbEntry(Profile $profile, RequestAbstract $request) : bool
{
if ($profile->id !== 0) {
return false;
}

$this->createModel($request->header->account, $profile, ProfileMapper::class, 'profile', $request->getOrigin());

return true;
}

/**
* Routing end-point for application behaviour.
*
Expand Down Expand Up @@ -138,7 +113,7 @@ private function createProfilesFromRequest(RequestAbstract $request) : array
$accounts = $request->getDataList('iaccount-idlist');

foreach ($accounts as $account) {
$account = (int) \trim($account);
$account = (int) $account;

/** @var Profile $isInDb */
$isInDb = ProfileMapper::get()->where('account', $account)->execute();
Expand Down
22 changes: 3 additions & 19 deletions tests/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use Model\CoreSettings;
use Modules\Admin\Models\AccountPermission;
use Modules\Profile\Models\ContactType;
use Modules\Profile\Models\Profile;
use Modules\Profile\Models\ProfileMapper;
use phpOMS\Account\Account;
Expand Down Expand Up @@ -100,34 +99,19 @@ protected function setUp() : void
*/
public function testApiProfileCreate() : void
{
\Modules\Admin\tests\Helper::createAccounts(1);

$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));

$request->header->account = 1;
$request->setData('iaccount-idlist', '1');
$request->setData('iaccount-idlist', '2');

$this->module->apiProfileCreate($request, $response);

self::assertGreaterThan(0, $response->get('')['response'][0]->id);
}

/**
* @covers Modules\Profile\Controller\ApiController
* @group module
*/
public function testApiProfileCreateDbEntry() : void
{
$request = new HttpRequest(new HttpUri(''));

$request->header->account = 1;

$profile = new Profile(new \Modules\Admin\Models\Account());
$profile->account->login = 'ProfileCreateDb';
$profile->account->setEmail('[email protected]');

self::assertTrue($this->module->apiProfileCreateDbEntry($profile, $request));
}

/**
* @covers Modules\Profile\Controller\ApiController
* @group module
Expand Down

0 comments on commit e0f9878

Please sign in to comment.