Skip to content

Commit 5b96124

Browse files
committed
Test Coverage + ModuleLoader API finalization
1 parent c015a16 commit 5b96124

File tree

12 files changed

+285
-49
lines changed

12 files changed

+285
-49
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
],
2121
"require": {
2222
"php": ">=8.0",
23-
"michaelj2324/php-rest-client": "^3.0"
23+
"michaelj2324/php-rest-client": ">=3.0.2"
2424
},
2525
"require-dev": {
2626
"phpunit/phpunit": "9.*",

examples/Bulk.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* ©[2024] SugarCRM Inc. Licensed by SugarCRM under the Apache 2.0 license.
5+
*/
6+
7+
require_once 'include.php';
8+
9+
$SugarAPI = new \Sugarcrm\REST\Client\SugarApi($server, $credentials);
10+
try {
11+
if ($SugarAPI->isAuthenticated()) {
12+
$lead1 = $SugarAPI->module("Leads");
13+
$lead2 = $SugarAPI->module("Leads");
14+
$lead1['first_name'] = "Test";
15+
$lead1['last_name'] = "Lead";
16+
17+
//Set same data on Lead 2
18+
$lead2->set($lead1->toArray());
19+
//Add 2 to second lead last name to differentiate
20+
$lead2['last_name'] .= "2";
21+
//Set
22+
$lead1->setCurrentAction(\MRussell\REST\Endpoint\ModelEndpoint::MODEL_ACTION_CREATE);
23+
$lead2->setCurrentAction(\MRussell\REST\Endpoint\ModelEndpoint::MODEL_ACTION_CREATE);
24+
$bulk = $SugarAPI->bulk();
25+
$bulk->setData([
26+
$lead1,
27+
$lead2,
28+
]);
29+
echo json_encode($bulk->getData()->toArray(), JSON_PRETTY_PRINT);
30+
$bulk->execute();
31+
echo json_encode($bulk->getResponseContent(), JSON_PRETTY_PRINT);
32+
} else {
33+
echo "Could not login.";
34+
pre($SugarAPI->getAuth()->getActionEndpoint('authenticate')->getResponse());
35+
}
36+
} catch (Exception $ex) {
37+
echo "Error Occurred: ";
38+
pre($ex->getMessage());
39+
pre($ex->getTraceAsString());
40+
}

src/Endpoint/Abstracts/AbstractSugarBeanEndpoint.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function __construct(array $urlArgs = [], array $properties = [])
134134
{
135135
parent::__construct($urlArgs, $properties);
136136
foreach (static::$_DEFAULT_SUGAR_BEAN_ACTIONS as $action => $method) {
137-
$this->actions[$action] = $method;
137+
$this->_actions[$action] = $method;
138138
}
139139
}
140140

@@ -473,7 +473,7 @@ public function filterRelated(string $linkName, bool $count = false): FilterData
473473
$args[] = 'count';
474474
}
475475

476-
$this->configureAction($this->action, $args);
476+
$this->configureAction($this->_action, $args);
477477
return $Filter;
478478
}
479479

src/Endpoint/MLPackage.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Sugarcrm\REST\Endpoint;
4+
5+
class MLPackage extends SugarBean
6+
{
7+
public const ACTION_INSTALL = 'install';
8+
9+
public const ACTION_UNINSTALL = 'uninstall';
10+
11+
public const ACTION_ENABLE = 'enable';
12+
13+
public const ACTION_DISABLE = 'disable';
14+
15+
public const ACTION_INSTALL_STATUS = 'installation-status';
16+
17+
protected static array $_DEFAULT_PROPERTIES = [
18+
self::PROPERTY_URL => 'Administration/packages/$id/$:action',
19+
self::PROPERTY_AUTH => true,
20+
];
21+
22+
protected static array $_DEFAULT_SUGAR_BEAN_ACTIONS = [
23+
self::ACTION_INSTALL => 'GET',
24+
self::ACTION_UNINSTALL => 'GET',
25+
self::ACTION_ENABLE => 'GET',
26+
self::ACTION_DISABLE => 'GET',
27+
self::ACTION_INSTALL_STATUS => 'GET',
28+
];
29+
30+
/**
31+
* Setup the query params passed during File Uploads
32+
* @codeCoverageIgnore
33+
*/
34+
protected function configureFileUploadQueryParams(): array
35+
{
36+
return [];
37+
}
38+
}

src/Endpoint/Me.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function __construct(array $urlArgs = [], array $properties = [])
6666
{
6767
parent::__construct($urlArgs, $properties);
6868
foreach (static::$_DEFAULT_SUGAR_USER_ACTIONS as $action => $method) {
69-
$this->actions[$action] = $method;
69+
$this->_actions[$action] = $method;
7070
}
7171
}
7272

@@ -115,7 +115,7 @@ protected function configureAction(string $action, array $arguments = []): void
115115
case self::USER_ACTION_DELETE_PREFERENCE:
116116
case self::USER_ACTION_CREATE_PREFERENCE:
117117
if (isset($arguments[0])) {
118-
$this->urlArgs['actionArg1'] = $arguments[0];
118+
$this->_urlArgs['actionArg1'] = $arguments[0];
119119
}
120120
}
121121
}

src/Endpoint/ModuleLoader.php

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,69 @@
22

33
namespace Sugarcrm\REST\Endpoint;
44

5-
class ModuleLoader extends SugarBean
6-
{
7-
public const ACTION_INSTALL = 'install';
8-
9-
public const ACTION_UNINSTALL = 'uninstall';
5+
use MRussell\REST\Endpoint\CollectionEndpoint;
106

11-
public const ACTION_ENABLE = 'enable';
7+
class ModuleLoader extends CollectionEndpoint
8+
{
9+
public const URL_ARG_FILTER = 'filter';
1210

13-
public const ACTION_DISABLE = 'disable';
11+
public const FILTER_STAGED = 'staged';
1412

15-
public const ACTION_INSTALL_STATUS = 'installation-status';
13+
public const FILTER_INSTALLED = 'installed';
1614

1715
protected static array $_DEFAULT_PROPERTIES = [
18-
self::PROPERTY_URL => 'Administration/packages/$id/$:action',
19-
self::PROPERTY_AUTH => true,
16+
self::PROPERTY_URL => 'Administration/packages/$:filter',
17+
self::PROPERTY_RESPONSE_PROP => 'packages',
18+
self::PROPERTY_HTTP_METHOD => 'GET',
19+
self::PROPERTY_AUTH => true
2020
];
2121

22-
protected static array $_DEFAULT_SUGAR_BEAN_ACTIONS = [
23-
self::ACTION_INSTALL => 'GET',
24-
self::ACTION_UNINSTALL => 'GET',
25-
self::ACTION_ENABLE => 'GET',
26-
self::ACTION_DISABLE => 'GET',
27-
self::ACTION_INSTALL_STATUS => 'GET',
28-
];
22+
protected string $_modelInterface = MLPackage::class;
23+
24+
protected string $_filter = '';
25+
26+
public function setUrlArgs(array $args): static
27+
{
28+
if (!empty($args[0])) {
29+
$this->_filter = $args[0];
30+
unset($args[0]);
31+
}
32+
if (!empty($args[self::URL_ARG_FILTER])) {
33+
$this->_filter = $args[self::URL_ARG_FILTER];
34+
unset($args[self::URL_ARG_FILTER]);
35+
}
36+
return parent::setUrlArgs($args);
37+
}
38+
39+
protected function configureURL(array $urlArgs): string
40+
{
41+
if (!empty($this->_filter)) {
42+
$urlArgs[self::URL_ARG_FILTER] = $this->_filter;
43+
}
44+
return parent::configureURL($urlArgs);
45+
}
46+
47+
public function execute(array $options = []): static
48+
{
49+
parent::execute($options);
50+
$this->_filter = '';
51+
return $this;
52+
}
53+
54+
public function staged(): static
55+
{
56+
$this->_filter = self::FILTER_STAGED;
57+
return $this->execute();
58+
}
59+
60+
public function installed(): static
61+
{
62+
$this->_filter = self::FILTER_INSTALLED;
63+
return $this->execute();
64+
}
2965

30-
/**
31-
* Setup the query params passed during File Uploads
32-
*/
33-
protected function configureFileUploadQueryParams(): array
66+
public function newPackage(): MLPackage
3467
{
35-
return [];
68+
return $this->generateEndpoint(MLPackage::class);
3669
}
3770
}

src/Endpoint/Note.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Note extends SugarBean
2626

2727
public const NOTES_ATTACHMENTS_FIELD = 'attachments';
2828

29-
protected array $actions = [
29+
protected array $_actions = [
3030
self::NOTE_ACTION_MULTI_ATTACH => 'POST',
3131
];
3232

src/Endpoint/Packages.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Endpoint/Provider/SugarEndpointProvider.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Sugarcrm\REST\Auth\SugarOAuthController;
1212
use Sugarcrm\REST\Endpoint\AuditLog;
1313
use Sugarcrm\REST\Endpoint\Email;
14+
use Sugarcrm\REST\Endpoint\MLPackage;
15+
use Sugarcrm\REST\Endpoint\ModuleLoader;
1416
use Sugarcrm\REST\Endpoint\SugarBean;
1517
use Sugarcrm\REST\Endpoint\ModuleFilter;
1618
use Sugarcrm\REST\Endpoint\Search;
@@ -143,5 +145,15 @@ class SugarEndpointProvider extends VersionedEndpointProvider
143145
self::ENDPOINT_CLASS => Email::class,
144146
self::ENDPOINT_PROPERTIES => [],
145147
],
148+
[
149+
self::ENDPOINT_NAME => 'moduleLoader',
150+
self::ENDPOINT_CLASS => ModuleLoader::class,
151+
self::ENDPOINT_PROPERTIES => [],
152+
],
153+
[
154+
self::ENDPOINT_NAME => 'mlp',
155+
self::ENDPOINT_CLASS => MLPackage::class,
156+
self::ENDPOINT_PROPERTIES => [],
157+
]
146158
];
147159
}

tests/Endpoint/MeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testConstruct(): void
4141
{
4242
$Me = new Me();
4343
$Reflection = new \ReflectionClass($Me::class);
44-
$actions = $Reflection->getProperty('actions');
44+
$actions = $Reflection->getProperty('_actions');
4545
$actions->setAccessible(true);
4646
$this->assertNotEmpty(
4747
$actions->getValue($Me),
@@ -58,7 +58,7 @@ public function testConfigureUrl(): void
5858
$configureUrl = $Reflection->getMethod('configureURL');
5959
$configureUrl->setAccessible(true);
6060

61-
$action = $Reflection->getProperty('action');
61+
$action = $Reflection->getProperty('_action');
6262
$action->setAccessible(true);
6363

6464
$this->assertEquals('me', $configureUrl->invoke($Me, []));

0 commit comments

Comments
 (0)