Skip to content

Commit

Permalink
Improvements, Bugfixes, Tests (#2)
Browse files Browse the repository at this point in the history
* - Improvements
- Bugfixes
- Tests

* removed debug code

* Added php 8.2 to tests
  • Loading branch information
Sandritsch91 committed Oct 26, 2023
1 parent 2faa755 commit 94ca5bb
Show file tree
Hide file tree
Showing 26 changed files with 1,877 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: false
matrix:
os: [ ubuntu-latest ]
php: [ '7.3', '7.4', '8.0', '8.1' ]
php: [ '7.3', '7.4', '8.0', '8.1', '8.2']

steps:
- name: Checkout
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ composer.lock

# phpunit itself is not needed
phpunit.phar
.phpunit.result.cache
tests/log
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"ext-soap": "*",
"yiisoft/yii2": "^2.0.20",
"php-ews/php-ews": "^1.0.0",
"simialbi/yii2-simialbi-base": ">=0.13.1 <1.0 | ^1.0.0"
"simialbi/yii2-simialbi-base": ">=0.13.1 <1.0 | ^1.0.0",
"simshaun/recurr": "^5.0.0"
},
"require-dev": {
"yiisoft/yii2-coding-standards": "~2.0",
Expand Down
15 changes: 15 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,19 @@
<directory>./tests</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-html" target="./tests/log/report" lowUpperBound="35" highLowerBound="70"/>
<log type="testdox-html" target="./tests/log/testdox.html"/>
</logging>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/conditions</directory>
<directory suffix=".php">./src/models</directory>
<directory suffix=".php">./src/recurrence/transformers</directory>
<exclude>
<directory suffix=".php">/path/to/files</directory>
<file>./tests/TestCase.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>
8 changes: 4 additions & 4 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static function primaryKey(): array

/**
* {@inheritDoc}
* @throws \yii\base\InvalidConfigException
* @throws InvalidConfigException
*/
public static function find(): ActiveQuery
{
Expand All @@ -67,7 +67,7 @@ public static function find(): ActiveQuery
/**
* {@inheritDoc}
* @return \simialbi\yii2\ews\Connection
* @throws \yii\base\InvalidConfigException
* @throws InvalidConfigException
*/
public static function getDb(): Connection
{
Expand All @@ -77,7 +77,7 @@ public static function getDb(): Connection
/**
* {@inheritDoc}
* @return boolean
* @throws \yii\base\NotSupportedException|Exception|InvalidConfigException
* @throws \yii\base\NotSupportedException|Exception|InvalidConfigException|\ReflectionException
*/
public static function updateAll($attributes, $condition = null): bool
{
Expand Down Expand Up @@ -207,7 +207,7 @@ public function insert($runValidation = true, $attributes = null, array $params
*
* @param string|null $value The date time string
* @return string|null The formatted date time string
* @throws \yii\base\InvalidConfigException
* @throws InvalidConfigException
*/
public function typeCastDateTime(?string $value): ?string
{
Expand Down
73 changes: 73 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use jamesiarmes\PhpEws\ClassMap;
use jamesiarmes\PhpNtlm\SoapClient;
use Yii;
use yii\base\InvalidConfigException;

class Client extends \jamesiarmes\PhpEws\Client
{
Expand All @@ -17,6 +18,22 @@ class Client extends \jamesiarmes\PhpEws\Client
*/
protected string $location;

/**
* {@inheritDoc}
* @param string $timezone The default timezone to set (Exchange format). Defaults to `W. Europe standard time`.
* @throws InvalidConfigException
*/
public function __construct($server = null, $username = null, $password = null, $version = self::VERSION_2013, ?string $timezone = null)
{
parent::__construct($server, $username, $password, $version);

if (null === $timezone) {
$this->autoSetTimezone();
} else {
$this->setTimezone($timezone);
}
}

/**
* Sets the location property
*
Expand Down Expand Up @@ -50,4 +67,60 @@ protected function initializeSoapClient(): SoapClient

return $this->soap;
}

/**
* Try to set exchange timezone from app configuration
*
* @return void
* @throws InvalidConfigException
*/
private function autoSetTimezone(): void
{
$list = timezone_abbreviations_list();
$offset = 0;
$timezoneName = Yii::$app->formatter->asDate('now', 'VV');

foreach ($list as $items) {
foreach ($items as $item) {
if ($item['dst']) {
continue 2;
}
if ($item['timezone_id'] === $timezoneName) {
$offset = $item['offset'];
break 2;
}
}
}

$this->setTimezone(match ($offset) {
-39600 => 'UTC-11',
-36000 => 'Hawaiian Standard Time',
-28800 => 'Pacific Standard Time',
-25200 => 'Mountain Standard Time',
-21600 => 'Central America Standard Time',
-18000 => 'Eastern Standard Time',
-16200 => 'Venezuela Standard Time',
-14400 => 'SA Western Standard Time',
-10800 => 'Pacific SA Standard Time',
-7200 => 'UTC-02',
-3600 => 'Cape Verde Standard Time',
default => 'UTC',
3600 => 'W. Europe Standard Time',
7200 => 'FLE Standard Time',
10800 => 'Arab Standard Time',
12600 => 'Iran Standard Time',
14400 => 'Caucasus Standard Time',
19800 => 'India Standard Time',
20700 => 'Nepal Standard Time',
21600 => 'Bangladesh Standard Time',
23400 => 'Myanmar Standard Time',
25200 => 'SE Asia Standard Time',
28800 => 'Singapore Standard Time',
32400 => 'Tokyo Standard Time',
36000 => 'AUS Eastern Standard Time',
39600 => 'Central Pacific Standard Time',
43200 => 'UTC+12',
46800 => 'Samoa Standard Time'
});
}
}
89 changes: 58 additions & 31 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,13 @@ public function execute(): \jamesiarmes\PhpEws\Type\ItemIdType|bool
switch ($method) {
case 'CreateItem':
case 'UpdateItem':
if (isset($message->Items->Message)) {
$return = $message->Items->Message[0]->ItemId;
} else {
foreach (['Message', 'CalendarItem', 'Task', 'Item'] as $itemType) {
if (isset($message->Items->{$itemType}[0])) {
$return = $message->Items->{$itemType}[0]->ItemId;
break;
}
}
if (!$return) {
foreach ($message->Items as $item) {
/** @var \jamesiarmes\PhpEws\Type\ItemType[] $item */
$return = $item[0]->ItemId;
Expand Down Expand Up @@ -299,35 +303,13 @@ protected function queryInternal(?string $method = null): mixed
$message = ArrayHelper::getValue($response, "ResponseMessages.{$method}ResponseMessage");

if (is_array($message)) {
$message = array_shift($message);
}

if ($message->ResponseCode !== ResponseCodeType::NO_ERROR) {
throw new Exception($message->MessageText, [
'responseCode' => $message->ResponseCode,
'responseClass' => $message->ResponseClass
]);
}

$result = match ($method) {
'FindItem' => ArrayHelper::getValue($message, 'RootFolder.Items'),
'FindFolder' => ArrayHelper::getValue($message, 'RootFolder.Folders'),
'GetItem' => ArrayHelper::getValue($message, 'Items'),
default => [],
};

// TODO: Find better solution
if (!is_array($result)) {
$r = new \ReflectionClass($result);
foreach ($r->getProperties() as $property) {
if ($property->isPublic() && is_array($result->{$property->name}) && !empty($result->{$property->name})) {
$result = $result->{$property->name};
break;
}
}
}
if ($result instanceof \jamesiarmes\PhpEws\ArrayType\ArrayOfRealItemsType) {
$result = [];
while ($m = array_shift($message)) {
$tmp = $this->getResult($m, $method);
$result = array_merge($result, $tmp);
}
} else {
$result = $this->getResult($message, $method);
}
} catch (Exception $e) {
throw $e;
Expand All @@ -346,4 +328,49 @@ protected function queryInternal(?string $method = null): mixed

return $result;
}

/**
* @param mixed $message
* @param string|null $method
* @return array|mixed
* @throws Exception
* @throws \ReflectionException
* @throws \Exception
*/
protected function getResult(mixed $message, ?string $method): mixed
{
if ($message->ResponseCode !== ResponseCodeType::NO_ERROR) {
if ($message->ResponseCode === 'ErrorItemNotFound') {
return [];
}
throw new Exception($message->MessageText, [
'responseCode' => $message->ResponseCode,
'responseClass' => $message->ResponseClass
]);
}

$result = match ($method) {
'FindItem' => ArrayHelper::getValue($message, 'RootFolder.Items'),
'FindFolder' => ArrayHelper::getValue($message, 'RootFolder.Folders'),
'GetItem' => ArrayHelper::getValue($message, 'Items'),
default => [],
};

// TODO: Find better solution
if (!is_array($result)) {
$r = new \ReflectionClass($result);
foreach ($r->getProperties() as $property) {
if ($property->isPublic() && is_array($result->{$property->name}) && !empty($result->{$property->name})) {
$result = $result->{$property->name};
break;
}
}
}

if ($result instanceof \jamesiarmes\PhpEws\ArrayType\ArrayOfRealItemsType) {
$result = [];
}

return $result;
}
}
1 change: 1 addition & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class Connection extends Component
/**
* Get the [[Client]] instance.
* @return Client
* @throws InvalidConfigException
*/
public function getClient(): Client
{
Expand Down
1 change: 0 additions & 1 deletion src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

namespace simialbi\yii2\ews;


/**
* Exception represents an exception that is caused by some EWS-related operations.
*
Expand Down
Loading

0 comments on commit 94ca5bb

Please sign in to comment.