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

Cake5 #107

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Cake5 #107

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
17 changes: 13 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@
"irc": "irc://irc.freenode.org/muffin"
},
"require": {
"cakephp/orm": "^4.2"
"cakephp/orm": "^5.0"
},
"require-dev": {
"cakephp/cakephp": "^4.2",
"cakephp/cakephp-codesniffer": "^4.0",
"phpunit/phpunit": "^8.5 || ^9.3"
"cakephp/cakephp": "^5.0",
"cakephp/cakephp-codesniffer": "^5.0",
"phpunit/phpunit": "^10.1"
},
"scripts": {
"cs-check": "phpcs --colors --parallel=16 -p src/ tests/",
"cs-fix": "phpcbf --colors --parallel=16 -p src/ tests/"
},
"autoload": {
"psr-4": {
Expand All @@ -58,5 +62,10 @@
"SomeVendor\\SomePlugin\\": "tests/test_app/plugins/SomeVendor/SomePlugin/src",
"TestPlugin\\": "tests/test_app/plugins/TestPlugin/src"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
parameters:
level: 6
level: 8
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
treatPhpDocTypesAsCertain: false
paths:
- src/
ignoreErrors:
Expand Down
36 changes: 13 additions & 23 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="./tests/bootstrap.php"
colors="true"
stopOnFailure="false"
>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./tests/bootstrap.php" colors="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Webservice Test Cases">
<testsuite name="Slug Test Cases">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener
class="\Cake\TestSuite\Fixture\FixtureInjector"
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
<arguments>
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
</arguments>
</listener>
</listeners>

<filter>
<whitelist>
<extensions>
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>
<php>
<ini name="memory_limit" value="-1"/>
<env name="FIXTURE_SCHEMA_METADATA" value="./tests/schema.php"/>
</php>
<source>
<include>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</include>
</source>
</phpunit>
19 changes: 7 additions & 12 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
errorLevel="2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedCode="false"
findUnusedBaselineEntry="true"
>
<projectFiles>
<directory name="src" />
Expand All @@ -14,16 +16,9 @@
</projectFiles>

<issueHandlers>

<PropertyNotSetInConstructor errorLevel="info" />
<MissingClosureReturnType errorLevel="info" />
<MissingClosureParamType errorLevel="info" />

<DocblockTypeContradiction errorLevel="info" />
<RedundantConditionGivenDocblockType errorLevel="info" />

<UnsafeInstantiation errorLevel="info" />

<UnresolvableInclude errorLevel="info" />
<RedundantConditionGivenDocblockType errorLevel="suppress"/>
<MissingClosureParamType errorLevel="suppress"/>
<MissingClosureReturnType errorLevel="suppress"/>
<PropertyNotSetInConstructor errorLevel="suppress"/>
</issueHandlers>
</psalm>
66 changes: 60 additions & 6 deletions src/Datasource/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
namespace Muffin\Webservice\Datasource;

use Cake\Core\App;
use Cake\Datasource\ConnectionInterface;
use Muffin\Webservice\Datasource\Exception\MissingConnectionException;
use Muffin\Webservice\Webservice\Driver\AbstractDriver;
use Muffin\Webservice\Webservice\Exception\MissingDriverException;
use Muffin\Webservice\Webservice\Exception\UnexpectedDriverException;
use Psr\SimpleCache\CacheInterface;

/**
* Class Connection
Expand All @@ -16,14 +18,21 @@
* @method \Muffin\Webservice\Webservice\WebserviceInterface getWebservice(string $name) Proxy method through to the Driver
* @method string configName() Proxy method through to the Driver
*/
class Connection
class Connection implements ConnectionInterface
{
/**
* Driver
*
* @var \Muffin\Webservice\Webservice\Driver\AbstractDriver
*/
protected $_driver;
protected ?AbstractDriver $_driver = null;

protected CacheInterface $cacher;

/**
* The connection name in the connection manager.
*/
protected string $configName = '';

/**
* Constructor
Expand All @@ -33,19 +42,64 @@ class Connection
*/
public function __construct(array $config)
{
if (isset($config['name'])) {
$this->configName = $config['name'];
}
$config = $this->_normalizeConfig($config);
/** @psalm-var class-string<\Muffin\Webservice\Webservice\Driver\AbstractDriver> */
$driver = $config['driver'];
unset($config['driver'], $config['service']);

$this->_driver = new $driver($config);

/** @psalm-suppress TypeDoesNotContainType */
if (!($this->_driver instanceof AbstractDriver)) {
throw new UnexpectedDriverException(['driver' => $driver]);
}
}

/**
* @param \Psr\SimpleCache\CacheInterface $cacher
* @return void
*/
public function setCacher(CacheInterface $cacher): void
{
}

/** @return \Psr\SimpleCache\CacheInterface */
public function getCacher(): CacheInterface
{
}

/**
* {@inheritDoc}
*
* @see \Cake\Datasource\ConnectionInterface::getDriver()
* @return \Muffin\Webservice\Webservice\Driver\AbstractDriver
*/
public function getDriver(string $role = self::ROLE_WRITE): object
{
return $this->_driver;
}

/**
* Get the configuration name for this connection.
*
* @return string
*/
public function configName(): string
{
return $this->configName;
}

/**
* Get the config data for this connection.
*
* @return array
*/
public function config(): array
{
return $this->_driver->getConfig();
}

/**
* Validates certain custom configuration values.
*
Expand All @@ -61,7 +115,7 @@ protected function _normalizeConfig(array $config): array
throw new MissingConnectionException(['name' => $config['name']]);
}

$config['driver'] = App::className($config['service'], 'Webservice/Driver');
$config['driver'] = App::className($config['service'], 'Webservice/Driver', 'Driver');
if (!$config['driver']) {
throw new MissingDriverException(['driver' => $config['driver']]);
}
Expand All @@ -77,7 +131,7 @@ protected function _normalizeConfig(array $config): array
* @param array $args Arguments to pass-through
* @return mixed
*/
public function __call($method, $args)
public function __call(string $method, array $args): mixed
{
return call_user_func_array([$this->_driver, $method], $args);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Datasource/Exception/MissingConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ class MissingConnectionException extends CakeException
*
* @var string
*/
protected $_messageTemplate = 'No `%s` connection configured.';
protected string $_messageTemplate = 'No `%s` connection configured.';
}
34 changes: 15 additions & 19 deletions src/Datasource/Marshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Cake\Datasource\InvalidPropertyInterface;
use Muffin\Webservice\Model\Endpoint;
use RuntimeException;
use Traversable;

/**
* Contains logic to convert array data into resources.
Expand All @@ -22,7 +23,7 @@ class Marshaller
*
* @var \Muffin\Webservice\Model\Endpoint
*/
protected $_endpoint;
protected Endpoint $_endpoint;

/**
* Constructor.
Expand Down Expand Up @@ -110,25 +111,20 @@ protected function _validate(array $data, array $options, bool $isNew): array
if (!$options['validate']) {
return [];
}

$validator = null;
if ($options['validate'] === true) {
$validator = $this->_endpoint->getValidator('default');
} elseif (is_string($options['validate'])) {
$validator = $this->_endpoint->getValidator($options['validate']);
} else {
/** @var \Cake\Validation\Validator $validator */
$validator = $options['validator'];
$options['validate'] = $this->_endpoint->getValidator('default');
}

if (!is_callable([$validator, 'errors'])) {
throw new RuntimeException(sprintf(
'"validate" must be a boolean, a string or an object with method "errors()". Got %s instead.',
gettype($options['validate'])
));
if (is_string($options['validate'])) {
$options['validate'] = $this->_endpoint->getValidator($options['validate']);
}
if (!is_object($options['validate'])) {
throw new RuntimeException(
sprintf('validate must be a boolean, a string or an object. Got %s.', gettype($options['validate']))
);
}

return $validator->validate($data, $isNew);
return $options['validate']->validate($data, $isNew);
}

/**
Expand Down Expand Up @@ -165,7 +161,7 @@ protected function _prepareDataAndOptions(array $data, array $options): array
*
* @param array $data The data to hydrate.
* @param array $options List of options
* @return \Cake\Datasource\EntityInterface[] An array of hydrated records.
* @return array<\Cake\Datasource\EntityInterface> An array of hydrated records.
* @see \Muffin\Webservice\Model\Endpoint::newEntities()
*/
public function many(array $data, array $options = []): array
Expand Down Expand Up @@ -260,13 +256,13 @@ public function merge(EntityInterface $entity, array $data, array $options = [])
* the accessible fields list in the entity will be used.
* - accessibleFields: A list of fields to allow or deny in entity accessible fields.
*
* @param array|\Traversable $entities the entities that will get the
* @param \Traversable|array $entities the entities that will get the
* data merged in
* @param array $data list of arrays to be merged into the entities
* @param array $options List of options.
* @return \Cake\Datasource\EntityInterface[]
* @return array<\Cake\Datasource\EntityInterface>
*/
public function mergeMany($entities, array $data, array $options = []): array
public function mergeMany(array|Traversable $entities, array $data, array $options = []): array
{
$primary = (array)$this->_endpoint->getPrimaryKey();

Expand Down
Loading