diff --git a/docs/book/sql.md b/docs/book/sql.md index 2df272639..f0f73d641 100644 --- a/docs/book/sql.md +++ b/docs/book/sql.md @@ -251,11 +251,11 @@ $select->from('foo')->where(['x = 5', 'y = z']); If you provide an associative array with string keys, any value with a string key will be cast as follows: -PHP value | Predicate type ---------- | -------------- -`null` | `Predicate\IsNull` -`array` | `Predicate\In` -`string` | `Predicate\Operator`, where the key is the identifier. +| PHP value | Predicate type | +|-----------|--------------------------------------------------------| +| `null` | `Predicate\IsNull` | +| `array` | `Predicate\In` | +| `string` | `Predicate\Operator`, where the key is the identifier. | As an example: @@ -642,6 +642,25 @@ class Expression implements ExpressionInterface, PredicateInterface } ``` +Expression parameters can be supplied either as a single scalar, an array of values, or as an array of value/types for more granular escaping. + +```php +$select + ->from('foo') + ->columns([ + new Expression( + '(COUNT(?) + ?) AS ?', + [ + ['some_column' => ExpressionInterface::TYPE_IDENTIFIER], + [5 => ExpressionInterface::TYPE_VALUE], + ['bar' => ExpressionInterface::TYPE_IDENTIFIER], + ], + ), + ]); + +// Produces SELECT (COUNT("some_column") + '5') AS "bar" FROM "foo" +``` + ### isNull($identifier) ```php diff --git a/src/Adapter/Adapter.php b/src/Adapter/Adapter.php index 81db9a4a1..ad3c7c0e1 100644 --- a/src/Adapter/Adapter.php +++ b/src/Adapter/Adapter.php @@ -2,7 +2,7 @@ namespace Laminas\Db\Adapter; -use InvalidArgumentException; +use Exception as PhpException; use Laminas\Db\ResultSet; use function func_get_args; @@ -10,7 +10,7 @@ use function is_array; use function is_bool; use function is_string; -use function strpos; +use function str_starts_with; use function strtolower; /** @@ -43,24 +43,15 @@ class Adapter implements AdapterInterface, Profiler\ProfilerAwareInterface /** @var Platform\PlatformInterface */ protected $platform; - /** @var Profiler\ProfilerInterface */ - protected $profiler; + protected Profiler\ProfilerInterface $profiler; - /** @var ResultSet\ResultSetInterface */ - protected $queryResultSetPrototype; + protected ResultSet\ResultSetInterface $queryResultSetPrototype; /** - * @deprecated - * - * @var Driver\StatementInterface - */ - protected $lastPreparedStatement; - /** - * @param Driver\DriverInterface|array $driver * @throws Exception\InvalidArgumentException */ public function __construct( - $driver, + Driver\DriverInterface|array $driver, ?Platform\PlatformInterface $platform = null, ?ResultSet\ResultSetInterface $queryResultPrototype = null, ?Profiler\ProfilerInterface $profiler = null @@ -98,7 +89,7 @@ public function __construct( /** * @return $this Provides a fluent interface */ - public function setProfiler(Profiler\ProfilerInterface $profiler) + public function setProfiler(Profiler\ProfilerInterface $profiler): self { $this->profiler = $profiler; if ($this->driver instanceof Profiler\ProfilerAwareInterface) { @@ -107,21 +98,15 @@ public function setProfiler(Profiler\ProfilerInterface $profiler) return $this; } - /** - * @return null|Profiler\ProfilerInterface - */ - public function getProfiler() + public function getProfiler(): ?Profiler\ProfilerInterface { return $this->profiler; } /** - * getDriver() - * * @throws Exception\RuntimeException - * @return Driver\DriverInterface */ - public function getDriver() + public function getDriver(): Driver\DriverInterface|array { if ($this->driver === null) { throw new Exception\RuntimeException('Driver has not been set or configured for this adapter.'); @@ -129,24 +114,17 @@ public function getDriver() return $this->driver; } - /** - * @return Platform\PlatformInterface - */ - public function getPlatform() + public function getPlatform(): ?Platform\PlatformInterface { return $this->platform; } - /** - * @return ResultSet\ResultSetInterface - */ - public function getQueryResultSetPrototype() + public function getQueryResultSetPrototype(): ResultSet\ResultSetInterface { return $this->queryResultSetPrototype; } - /** @return string */ - public function getCurrentSchema() + public function getCurrentSchema(): string { return $this->driver->getConnection()->getCurrentSchema(); } @@ -154,16 +132,14 @@ public function getCurrentSchema() /** * query() is a convenience function * - * @param string $sql - * @param string|array|ParameterContainer $parametersOrQueryMode * @throws Exception\InvalidArgumentException - * @return Driver\StatementInterface|ResultSet\ResultSet + * @throws PhpException */ public function query( - $sql, - $parametersOrQueryMode = self::QUERY_MODE_PREPARE, + string $sql, + ParameterContainer|array|string $parametersOrQueryMode = self::QUERY_MODE_PREPARE, ?ResultSet\ResultSetInterface $resultPrototype = null - ) { + ): Driver\StatementInterface|ResultSet\ResultSet|Driver\ResultInterface { if ( is_string($parametersOrQueryMode) && in_array($parametersOrQueryMode, [self::QUERY_MODE_PREPARE, self::QUERY_MODE_EXECUTE]) @@ -210,13 +186,11 @@ public function query( /** * Create statement - * - * @param string $initialSql - * @param null|ParameterContainer|array $initialParameters - * @return Driver\StatementInterface */ - public function createStatement($initialSql = null, $initialParameters = null) - { + public function createStatement( + ?string $initialSql = null, + ParameterContainer|array|null $initialParameters = null + ): Driver\StatementInterface { $statement = $this->driver->createStatement($initialSql); if ( $initialParameters === null @@ -250,29 +224,19 @@ public function getHelpers() } /** - * @param string $name * @throws Exception\InvalidArgumentException * @return Driver\DriverInterface|Platform\PlatformInterface */ - public function __get($name) + public function __get(string $name) { - switch (strtolower($name)) { - case 'driver': - return $this->driver; - case 'platform': - return $this->platform; - default: - throw new Exception\InvalidArgumentException('Invalid magic property on adapter'); - } + return match (strtolower($name)) { + 'driver' => $this->driver, + 'platform' => $this->platform, + default => throw new Exception\InvalidArgumentException('Invalid magic property on adapter'), + }; } - /** - * @param array $parameters - * @return Driver\DriverInterface - * @throws InvalidArgumentException - * @throws Exception\InvalidArgumentException - */ - protected function createDriver($parameters) + protected function createDriver(array $parameters): Driver\DriverInterface { if (! isset($parameters['driver'])) { throw new Exception\InvalidArgumentException( @@ -315,7 +279,7 @@ protected function createDriver($parameters) break; case 'pdo': default: - if ($driverName === 'pdo' || strpos($driverName, 'pdo') === 0) { + if ($driverName === 'pdo' || str_starts_with($driverName, 'pdo')) { $driver = new Driver\Pdo\Pdo($parameters); } } @@ -327,16 +291,12 @@ protected function createDriver($parameters) return $driver; } - /** - * @param array $parameters - * @return Platform\PlatformInterface - */ - protected function createPlatform(array $parameters) + protected function createPlatform(array $parameters): Platform\PlatformInterface { if (isset($parameters['platform'])) { $platformName = $parameters['platform']; } elseif ($this->driver instanceof Driver\DriverInterface) { - $platformName = $this->driver->getDatabasePlatformName(Driver\DriverInterface::NAME_FORMAT_CAMELCASE); + $platformName = $this->driver->getDatabasePlatformName(); } else { throw new Exception\InvalidArgumentException( 'A platform could not be determined from the provided configuration' @@ -387,12 +347,7 @@ protected function createPlatform(array $parameters) } } - /** - * @param array $parameters - * @return Profiler\ProfilerInterface - * @throws Exception\InvalidArgumentException - */ - protected function createProfiler($parameters) + protected function createProfiler(array $parameters): ?Profiler\ProfilerInterface { if ($parameters['profiler'] instanceof Profiler\ProfilerInterface) { return $parameters['profiler']; @@ -406,27 +361,4 @@ protected function createProfiler($parameters) '"profiler" parameter must be an instance of ProfilerInterface or a boolean' ); } - - /** - * @deprecated - * - * @param array $parameters - * @return Driver\DriverInterface - * @throws InvalidArgumentException - * @throws Exception\InvalidArgumentException - */ - protected function createDriverFromParameters(array $parameters) - { - return $this->createDriver($parameters); - } - - /** - * @deprecated - * - * @return Platform\PlatformInterface - */ - protected function createPlatformFromDriver(Driver\DriverInterface $driver) - { - return $this->createPlatform($driver); - } } diff --git a/src/Adapter/Driver/Oci8/Statement.php b/src/Adapter/Driver/Oci8/Statement.php index 21747b622..288ab6a22 100644 --- a/src/Adapter/Driver/Oci8/Statement.php +++ b/src/Adapter/Driver/Oci8/Statement.php @@ -50,14 +50,6 @@ class Statement implements StatementInterface, Profiler\ProfilerAwareInterface /** @var resource */ protected $resource; - /** - * @internal - * @deprecated - * - * @var bool - */ - public $parametersBound; - /** * Is prepared * @@ -323,7 +315,6 @@ protected function bindParametersFromContainer() public function __clone() { $this->isPrepared = false; - $this->parametersBound = false; $this->resource = null; if ($this->parameterContainer) { $this->parameterContainer = clone $this->parameterContainer; diff --git a/src/Metadata/Metadata.php b/src/Metadata/Metadata.php deleted file mode 100644 index 8b3c02e0a..000000000 --- a/src/Metadata/Metadata.php +++ /dev/null @@ -1,150 +0,0 @@ -source = Source\Factory::createSourceFromAdapter($adapter); - } - - /** - * {@inheritdoc} - */ - public function getTables($schema = null, $includeViews = false) - { - return $this->source->getTables($schema, $includeViews); - } - - /** - * {@inheritdoc} - */ - public function getViews($schema = null) - { - return $this->source->getViews($schema); - } - - /** - * {@inheritdoc} - */ - public function getTriggers($schema = null) - { - return $this->source->getTriggers($schema); - } - - /** - * {@inheritdoc} - */ - public function getConstraints($table, $schema = null) - { - return $this->source->getConstraints($table, $schema); - } - - /** - * {@inheritdoc} - */ - public function getColumns($table, $schema = null) - { - return $this->source->getColumns($table, $schema); - } - - /** - * {@inheritdoc} - */ - public function getConstraintKeys($constraint, $table, $schema = null) - { - return $this->source->getConstraintKeys($constraint, $table, $schema); - } - - /** - * {@inheritdoc} - */ - public function getConstraint($constraintName, $table, $schema = null) - { - return $this->source->getConstraint($constraintName, $table, $schema); - } - - /** - * {@inheritdoc} - */ - public function getSchemas() - { - return $this->source->getSchemas(); - } - - /** - * {@inheritdoc} - */ - public function getTableNames($schema = null, $includeViews = false) - { - return $this->source->getTableNames($schema, $includeViews); - } - - /** - * {@inheritdoc} - */ - public function getTable($tableName, $schema = null) - { - return $this->source->getTable($tableName, $schema); - } - - /** - * {@inheritdoc} - */ - public function getViewNames($schema = null) - { - return $this->source->getViewNames($schema); - } - - /** - * {@inheritdoc} - */ - public function getView($viewName, $schema = null) - { - return $this->source->getView($viewName, $schema); - } - - /** - * {@inheritdoc} - */ - public function getTriggerNames($schema = null) - { - return $this->source->getTriggerNames($schema); - } - - /** - * {@inheritdoc} - */ - public function getTrigger($triggerName, $schema = null) - { - return $this->source->getTrigger($triggerName, $schema); - } - - /** - * {@inheritdoc} - */ - public function getColumnNames($table, $schema = null) - { - return $this->source->getColumnNames($table, $schema); - } - - /** - * {@inheritdoc} - */ - public function getColumn($columnName, $table, $schema = null) - { - return $this->source->getColumn($columnName, $table, $schema); - } -} diff --git a/src/Sql/Expression.php b/src/Sql/Expression.php index da40f5dd4..9a167366a 100644 --- a/src/Sql/Expression.php +++ b/src/Sql/Expression.php @@ -6,7 +6,6 @@ use function count; use function is_array; use function is_scalar; -use function is_string; use function preg_match_all; use function str_ireplace; use function str_replace; @@ -18,73 +17,45 @@ class Expression extends AbstractExpression */ public const PLACEHOLDER = '?'; - /** @var string */ - protected $expression = ''; + protected string $expression = ''; - /** @var array */ - protected $parameters = []; - - /** @var array */ - protected $types = []; + protected float|array|int|string|bool $parameters = []; /** - * @param string $expression - * @param string|array $parameters - * @param array $types @deprecated will be dropped in version 3.0.0 + * @todo Update documentation to show how parameters can be specifically typed */ - public function __construct($expression = '', $parameters = null, array $types = []) + public function __construct(string $expression = '', float|array|int|string|bool|null $parameters = null) { if ($expression !== '') { $this->setExpression($expression); } - if ($types) { // should be deprecated and removed version 3.0.0 - if (is_array($parameters)) { - foreach ($parameters as $i => $parameter) { - $parameters[$i] = [ - $parameter => $types[$i] ?? self::TYPE_VALUE, - ]; - } - } elseif (is_scalar($parameters)) { - $parameters = [ - $parameters => $types[0], - ]; - } - } - if ($parameters !== null) { $this->setParameters($parameters); } } /** - * @param string $expression - * @return $this Provides a fluent interface * @throws Exception\InvalidArgumentException */ - public function setExpression($expression) + public function setExpression(string $expression): self { - if (! is_string($expression) || $expression === '') { - throw new Exception\InvalidArgumentException('Supplied expression must be a string.'); + if ($expression === '') { + throw new Exception\InvalidArgumentException('Supplied expression must not be an empty string.'); } $this->expression = $expression; return $this; } - /** - * @return string - */ - public function getExpression() + public function getExpression(): string { return $this->expression; } /** - * @param scalar|array $parameters - * @return $this Provides a fluent interface * @throws Exception\InvalidArgumentException */ - public function setParameters($parameters) + public function setParameters(float|array|int|string|bool $parameters): self { if (! is_scalar($parameters) && ! is_array($parameters)) { throw new Exception\InvalidArgumentException('Expression parameters must be a scalar or array.'); @@ -93,41 +64,16 @@ public function setParameters($parameters) return $this; } - /** - * @return array - */ - public function getParameters() + public function getParameters(): float|array|int|string|bool { return $this->parameters; } - /** - * @deprecated - * - * @param array $types - * @return $this Provides a fluent interface - */ - public function setTypes(array $types) - { - $this->types = $types; - return $this; - } - - /** - * @deprecated - * - * @return array - */ - public function getTypes() - { - return $this->types; - } - /** * @return array * @throws Exception\RuntimeException */ - public function getExpressionData() + public function getExpressionData(): array { $parameters = is_scalar($this->parameters) ? [$this->parameters] : $this->parameters; $parametersCount = count($parameters); @@ -145,7 +91,7 @@ public function getExpressionData() // test number of replacements without considering same variable begin used many times first, which is // faster, if the test fails then resort to regex which are slow and used rarely if ($count !== $parametersCount) { - preg_match_all('/\:\w*/', $expression, $matches); + preg_match_all('/:\w*/', $expression, $matches); if ($parametersCount !== count(array_unique($matches[0]))) { throw new Exception\RuntimeException( 'The number of replacements in the expression does not match the number of parameters' @@ -154,7 +100,7 @@ public function getExpressionData() } foreach ($parameters as $parameter) { - [$values[], $types[]] = $this->normalizeArgument($parameter, self::TYPE_VALUE); + [$values[], $types[]] = $this->normalizeArgument($parameter); } return [ [ diff --git a/src/Sql/Platform/IbmDb2/SelectDecorator.php b/src/Sql/Platform/IbmDb2/SelectDecorator.php index f0060ff2d..4783a2178 100644 --- a/src/Sql/Platform/IbmDb2/SelectDecorator.php +++ b/src/Sql/Platform/IbmDb2/SelectDecorator.php @@ -44,11 +44,11 @@ public function setIsSelectContainDistinct($isSelectContainDistinct) } /** - * @param Select $select + * @param Select $subject */ - public function setSubject($select) + public function setSubject($subject) { - $this->subject = $select; + $this->subject = $subject; } /** diff --git a/src/Sql/Platform/Mysql/SelectDecorator.php b/src/Sql/Platform/Mysql/SelectDecorator.php index bb1ee8f87..5340dd43e 100644 --- a/src/Sql/Platform/Mysql/SelectDecorator.php +++ b/src/Sql/Platform/Mysql/SelectDecorator.php @@ -14,11 +14,11 @@ class SelectDecorator extends Select implements PlatformDecoratorInterface protected $subject; /** - * @param Select $select + * @param Select $subject */ - public function setSubject($select) + public function setSubject($subject) { - $this->subject = $select; + $this->subject = $subject; } protected function localizeVariables() @@ -34,7 +34,7 @@ protected function processLimit( PlatformInterface $platform, ?DriverInterface $driver = null, ?ParameterContainer $parameterContainer = null - ) { + ): ?array { if ($this->limit === null && $this->offset !== null) { return ['']; } @@ -54,9 +54,9 @@ protected function processOffset( PlatformInterface $platform, ?DriverInterface $driver = null, ?ParameterContainer $parameterContainer = null - ) { + ): ?array { if ($this->offset === null) { - return; + return null; } if ($parameterContainer) { $paramPrefix = $this->processInfo['paramPrefix']; diff --git a/src/Sql/Platform/Oracle/SelectDecorator.php b/src/Sql/Platform/Oracle/SelectDecorator.php index 2c6141221..ce6fd6282 100644 --- a/src/Sql/Platform/Oracle/SelectDecorator.php +++ b/src/Sql/Platform/Oracle/SelectDecorator.php @@ -16,30 +16,28 @@ class SelectDecorator extends Select implements PlatformDecoratorInterface { - /** @var Select */ - protected $subject; + protected Select $subject; /** - * @param Select $select + * @param Select $subject */ - public function setSubject($select) + public function setSubject($subject): void { - $this->subject = $select; + $this->subject = $subject; } /** - * @see \Laminas\Db\Sql\Select::renderTable + * @see Select::renderTable * * @param string $table * @param null|string $alias - * @return string */ - protected function renderTable($table, $alias = null) + protected function renderTable($table, $alias = null): string { return $table . ($alias ? ' ' . $alias : ''); } - protected function localizeVariables() + protected function localizeVariables(): void { parent::localizeVariables(); unset($this->specifications[self::LIMIT]); @@ -48,18 +46,13 @@ protected function localizeVariables() $this->specifications['LIMITOFFSET'] = null; } - /** - * @param array $sqls - * @param array $parameters - * @return null - */ protected function processLimitOffset( PlatformInterface $platform, ?DriverInterface $driver = null, ?ParameterContainer $parameterContainer = null, - &$sqls = [], - &$parameters = [] - ) { + array &$sqls = [], + array &$parameters = [] + ): void { if ($this->limit === null && $this->offset === null) { return; } @@ -93,13 +86,10 @@ protected function processLimitOffset( ], $selectParameters)); if ($parameterContainer) { - $number = $this->processInfo['subselectCount'] ? $this->processInfo['subselectCount'] : ''; + $number = $this->processInfo['subselectCount'] ?: ''; if ($this->limit === null) { - array_push( - $sqls, - ') b ) WHERE b_rownum > (:offset' . $number . ')' - ); + $sqls[] = ') b ) WHERE b_rownum > (:offset' . $number . ')'; $parameterContainer->offsetSet( 'offset' . $number, $this->offset, @@ -107,21 +97,20 @@ protected function processLimitOffset( ); } else { // create bottom part of query, with offset and limit using row_number - array_push( - $sqls, - ') b WHERE rownum <= (:offset' + $sqls[] = ') b WHERE rownum <= (:offset' . $number . '+:limit' . $number . ')) WHERE b_rownum >= (:offset' . $number - . ' + 1)' - ); + . ' + 1)'; + $parameterContainer->offsetSet( 'offset' . $number, $this->offset, $parameterContainer::TYPE_INTEGER ); + $parameterContainer->offsetSet( 'limit' . $number, $this->limit, diff --git a/src/Sql/Platform/SqlServer/SelectDecorator.php b/src/Sql/Platform/SqlServer/SelectDecorator.php index d491e1098..c0bcb00c6 100644 --- a/src/Sql/Platform/SqlServer/SelectDecorator.php +++ b/src/Sql/Platform/SqlServer/SelectDecorator.php @@ -21,11 +21,11 @@ class SelectDecorator extends Select implements PlatformDecoratorInterface protected $subject; /** - * @param Select $select + * @param Select $subject */ - public function setSubject($select) + public function setSubject($subject) { - $this->subject = $select; + $this->subject = $subject; } protected function localizeVariables() @@ -41,15 +41,14 @@ protected function localizeVariables() /** * @param string[] $sqls * @param array $parameters - * @return void */ protected function processLimitOffset( PlatformInterface $platform, ?DriverInterface $driver, ?ParameterContainer $parameterContainer, - &$sqls, - &$parameters - ) { + array &$sqls, + array &$parameters + ): void { if ($this->limit === null && $this->offset === null) { return; } diff --git a/src/Sql/Platform/Sqlite/SelectDecorator.php b/src/Sql/Platform/Sqlite/SelectDecorator.php index 9b742e6e1..e01acaeb9 100644 --- a/src/Sql/Platform/Sqlite/SelectDecorator.php +++ b/src/Sql/Platform/Sqlite/SelectDecorator.php @@ -10,18 +10,17 @@ class SelectDecorator extends Select implements PlatformDecoratorInterface { - /** @var Select */ - protected $subject; + protected Select $subject; /** * Set Subject * - * @param Select $select + * @param Select $subject * @return $this Provides a fluent interface */ - public function setSubject($select) + public function setSubject($subject): self { - $this->subject = $select; + $this->subject = $subject; return $this; } @@ -29,7 +28,7 @@ public function setSubject($select) /** * {@inheritDoc} */ - protected function localizeVariables() + protected function localizeVariables(): void { parent::localizeVariables(); $this->specifications[self::COMBINE] = '%1$s %2$s'; @@ -42,21 +41,20 @@ protected function processStatementStart( PlatformInterface $platform, ?DriverInterface $driver = null, ?ParameterContainer $parameterContainer = null - ) { + ): string { return ''; } - /** @return string[] */ protected function processLimit( PlatformInterface $platform, ?DriverInterface $driver = null, ?ParameterContainer $parameterContainer = null - ) { + ): ?array { if ($this->limit === null && $this->offset !== null) { return ['']; } if ($this->limit === null) { - return; + return null; } if ($parameterContainer) { $paramPrefix = $this->processInfo['paramPrefix']; @@ -71,9 +69,9 @@ protected function processOffset( PlatformInterface $platform, ?DriverInterface $driver = null, ?ParameterContainer $parameterContainer = null - ) { + ): ?array { if ($this->offset === null) { - return; + return null; } if ($parameterContainer) { $paramPrefix = $this->processInfo['paramPrefix']; @@ -91,7 +89,7 @@ protected function processStatementEnd( PlatformInterface $platform, ?DriverInterface $driver = null, ?ParameterContainer $parameterContainer = null - ) { + ): string { return ''; } } diff --git a/src/Sql/Select.php b/src/Sql/Select.php index c588a926b..828652103 100644 --- a/src/Sql/Select.php +++ b/src/Sql/Select.php @@ -70,16 +70,6 @@ class Select extends AbstractPreparableSql public const COMBINE_INTERSECT = 'intersect'; /**#@-*/ - /** - * @deprecated use JOIN_LEFT_OUTER instead - */ - public const JOIN_OUTER_LEFT = 'outer left'; - - /** - * @deprecated use JOIN_LEFT_OUTER instead - */ - public const JOIN_OUTER_RIGHT = 'outer right'; - /** @var array Specifications */ protected $specifications = [ 'statementStart' => '%1$s', @@ -120,11 +110,9 @@ class Select extends AbstractPreparableSql self::COMBINE => '%1$s ( %2$s )', ]; - /** @var bool */ - protected $tableReadOnly = false; + protected bool $tableReadOnly = false; - /** @var bool */ - protected $prefixColumnsWithTable = true; + protected bool $prefixColumnsWithTable = true; /** @var string|array|TableIdentifier */ protected $table; @@ -133,9 +121,9 @@ class Select extends AbstractPreparableSql protected $quantifier; /** @var array */ - protected $columns = [self::SQL_STAR]; + protected array $columns = [self::SQL_STAR]; - /** @var null|Join */ + /** @var Join[] */ protected $joins; /** @var Where */ @@ -269,12 +257,12 @@ public function join($name, $on, $columns = self::SQL_STAR, $type = self::JOIN_I /** * Create where clause * - * @param Where|Closure|string|array|PredicateInterface $predicate - * @param string $combination One of the OP_* constants from Predicate\PredicateSet - * @return $this Provides a fluent interface + * @param Closure|string|array|PredicateInterface $predicate + * @param string $combination One of the OP_* constants from Predicate\PredicateSet * @throws Exception\InvalidArgumentException + * @return $this Provides a fluent interface */ - public function where($predicate, $combination = Predicate\PredicateSet::OP_AND) + public function where($predicate, string $combination = Predicate\PredicateSet::OP_AND): self { if ($predicate instanceof Where) { $this->where = $predicate; @@ -622,9 +610,9 @@ protected function processWhere( PlatformInterface $platform, ?DriverInterface $driver = null, ?ParameterContainer $parameterContainer = null - ) { + ): ?array { if ($this->where->count() === 0) { - return; + return null; } return [ $this->processExpression($this->where, $platform, $driver, $parameterContainer, 'where'), @@ -635,9 +623,9 @@ protected function processGroup( PlatformInterface $platform, ?DriverInterface $driver = null, ?ParameterContainer $parameterContainer = null - ) { + ): ?array { if ($this->group === null) { - return; + return null; } // process table columns $groups = []; @@ -660,9 +648,9 @@ protected function processHaving( PlatformInterface $platform, ?DriverInterface $driver = null, ?ParameterContainer $parameterContainer = null - ) { + ): ?array { if ($this->having->count() === 0) { - return; + return null; } return [ $this->processExpression($this->having, $platform, $driver, $parameterContainer, 'having'), @@ -673,9 +661,9 @@ protected function processOrder( PlatformInterface $platform, ?DriverInterface $driver = null, ?ParameterContainer $parameterContainer = null - ) { + ): ?array { if (empty($this->order)) { - return; + return null; } $orders = []; foreach ($this->order as $k => $v) { @@ -706,9 +694,9 @@ protected function processLimit( PlatformInterface $platform, ?DriverInterface $driver = null, ?ParameterContainer $parameterContainer = null - ) { + ): ?array { if ($this->limit === null) { - return; + return null; } if ($parameterContainer) { $paramPrefix = $this->processInfo['paramPrefix']; @@ -722,9 +710,9 @@ protected function processOffset( PlatformInterface $platform, ?DriverInterface $driver = null, ?ParameterContainer $parameterContainer = null - ) { + ): ?array { if ($this->offset === null) { - return; + return null; } if ($parameterContainer) { $paramPrefix = $this->processInfo['paramPrefix']; @@ -739,9 +727,9 @@ protected function processCombine( PlatformInterface $platform, ?DriverInterface $driver = null, ?ParameterContainer $parameterContainer = null - ) { + ): ?array { if ($this->combine === []) { - return; + return null; } $type = $this->combine['type']; @@ -764,16 +752,12 @@ protected function processCombine( */ public function __get($name) { - switch (strtolower($name)) { - case 'where': - return $this->where; - case 'having': - return $this->having; - case 'joins': - return $this->joins; - default: - throw new Exception\InvalidArgumentException('Not a valid magic property for this object'); - } + return match (strtolower($name)) { + 'where' => $this->where, + 'having' => $this->having, + 'joins' => $this->joins, + default => throw new Exception\InvalidArgumentException('Not a valid magic property for this object'), + }; } /** @@ -792,7 +776,7 @@ public function __clone() /** * @param string|TableIdentifier|Select $table - * @return string + * @return array */ protected function resolveTable( $table, diff --git a/src/Sql/Sql.php b/src/Sql/Sql.php index 5f5f71f2b..96b2cf693 100644 --- a/src/Sql/Sql.php +++ b/src/Sql/Sql.php @@ -4,87 +4,59 @@ use Laminas\Db\Adapter\AdapterInterface; use Laminas\Db\Adapter\Driver\StatementInterface; -use Laminas\Db\Adapter\Platform\PlatformInterface; +use Laminas\Db\Adapter\StatementContainerInterface; -use function is_array; -use function is_string; use function sprintf; class Sql { - /** @var AdapterInterface */ - protected $adapter; + protected AdapterInterface $adapter; - /** @var string|array|TableIdentifier */ - protected $table; + protected TableIdentifier|string|array|null $table; - /** @var Platform\Platform */ - protected $sqlPlatform; + protected Platform\Platform $sqlPlatform; - /** - * @param null|string|array|TableIdentifier $table - * @param null|Platform\AbstractPlatform $sqlPlatform @deprecated since version 3.0 - */ public function __construct( AdapterInterface $adapter, - $table = null, - ?Platform\AbstractPlatform $sqlPlatform = null + array|string|TableIdentifier|null $table = null ) { - $this->adapter = $adapter; - if ($table) { - $this->setTable($table); - } - $this->sqlPlatform = $sqlPlatform ?: new Platform\Platform($adapter); + $this->adapter = $adapter; + $this->table = $table; + $this->sqlPlatform = new Platform\Platform($adapter); } - /** - * @return null|AdapterInterface - */ - public function getAdapter() + public function getAdapter(): ?AdapterInterface { return $this->adapter; } - /** @return bool */ - public function hasTable() + public function hasTable(): bool { return $this->table !== null; } /** - * @param string|array|TableIdentifier $table - * @return $this Provides a fluent interface * @throws Exception\InvalidArgumentException + * @return $this Provides a fluent interface */ - public function setTable($table) + public function setTable(array|string|TableIdentifier $table): self { - if (is_string($table) || is_array($table) || $table instanceof TableIdentifier) { - $this->table = $table; - } else { - throw new Exception\InvalidArgumentException( - 'Table must be a string, array or instance of TableIdentifier.' - ); - } + $this->table = $table; + return $this; } - /** @return string|array|TableIdentifier */ - public function getTable() + public function getTable(): array|string|TableIdentifier { return $this->table; } - /** @return Platform\Platform */ - public function getSqlPlatform() + public function getSqlPlatform(): ?Platform\Platform { return $this->sqlPlatform; } - /** - * @param null|string|TableIdentifier $table - * @return Select - */ - public function select($table = null) + public function select(string|TableIdentifier|null $table = null): Select { if ($this->table !== null && $table !== null) { throw new Exception\InvalidArgumentException(sprintf( @@ -92,14 +64,11 @@ public function select($table = null) $this->table )); } + return new Select($table ?: $this->table); } - /** - * @param null|string|TableIdentifier $table - * @return Insert - */ - public function insert($table = null) + public function insert(string|null|TableIdentifier $table = null): Insert { if ($this->table !== null && $table !== null) { throw new Exception\InvalidArgumentException(sprintf( @@ -107,14 +76,11 @@ public function insert($table = null) $this->table )); } + return new Insert($table ?: $this->table); } - /** - * @param null|string|TableIdentifier $table - * @return Update - */ - public function update($table = null) + public function update(null|string|TableIdentifier $table = null): Update { if ($this->table !== null && $table !== null) { throw new Exception\InvalidArgumentException(sprintf( @@ -122,14 +88,11 @@ public function update($table = null) $this->table )); } + return new Update($table ?: $this->table); } - /** - * @param null|string|TableIdentifier $table - * @return Delete - */ - public function delete($table = null) + public function delete(null|string|TableIdentifier $table = null): Delete { if ($this->table !== null && $table !== null) { throw new Exception\InvalidArgumentException(sprintf( @@ -137,17 +100,15 @@ public function delete($table = null) $this->table )); } + return new Delete($table ?: $this->table); } - /** - * @return StatementInterface - */ public function prepareStatementForSqlObject( PreparableSqlInterface $sqlObject, ?StatementInterface $statement = null, ?AdapterInterface $adapter = null - ) { + ): ?StatementContainerInterface { $adapter = $adapter ?: $this->adapter; $statement = $statement ?: $adapter->getDriver()->createStatement(); @@ -155,23 +116,9 @@ public function prepareStatementForSqlObject( } /** - * Get sql string using platform or sql object - * - * @deprecated Deprecated in 2.4. Use buildSqlString() instead - * - * @return string - */ - public function getSqlStringForSqlObject(SqlInterface $sqlObject, ?PlatformInterface $platform = null) - { - $platform = $platform ?: $this->adapter->getPlatform(); - return $this->sqlPlatform->setSubject($sqlObject)->getSqlString($platform); - } - - /** - * @return string * @throws Exception\InvalidArgumentException */ - public function buildSqlString(SqlInterface $sqlObject, ?AdapterInterface $adapter = null) + public function buildSqlString(SqlInterface $sqlObject, ?AdapterInterface $adapter = null): string { return $this ->sqlPlatform diff --git a/src/Sql/TableIdentifier.php b/src/Sql/TableIdentifier.php index ff69c53e0..8d9890dab 100644 --- a/src/Sql/TableIdentifier.php +++ b/src/Sql/TableIdentifier.php @@ -2,106 +2,48 @@ namespace Laminas\Db\Sql; -use function gettype; -use function is_callable; -use function is_object; -use function is_string; -use function sprintf; - class TableIdentifier { - /** @var string */ - protected $table; + protected string $table; - /** @var null|string */ - protected $schema; + protected ?string $schema = null; - /** - * @param string $table - * @param null|string $schema - */ - public function __construct($table, $schema = null) + public function __construct(string $table, ?string $schema = null) { - if (! (is_string($table) || is_callable([$table, '__toString']))) { - throw new Exception\InvalidArgumentException(sprintf( - '$table must be a valid table name, parameter of type %s given', - is_object($table) ? $table::class : gettype($table) - )); - } - - $this->table = (string) $table; - - if ('' === $this->table) { - throw new Exception\InvalidArgumentException('$table must be a valid table name, empty string given'); + if ('' === $table) { + throw new Exception\InvalidArgumentException( + '$table must be a valid table name, empty string given' + ); } + $this->table = $table; - if (null === $schema) { - $this->schema = null; - } else { - if (! (is_string($schema) || is_callable([$schema, '__toString']))) { - throw new Exception\InvalidArgumentException(sprintf( - '$schema must be a valid schema name, parameter of type %s given', - is_object($schema) ? $schema::class : gettype($schema) - )); - } - - $this->schema = (string) $schema; - - if ('' === $this->schema) { + if ($schema !== null) { + if ('' === $schema) { throw new Exception\InvalidArgumentException( '$schema must be a valid schema name or null, empty string given' ); } + $this->schema = $schema; } } - /** - * @deprecated please use the constructor and build a new {@see TableIdentifier} instead - * - * @param string $table - */ - public function setTable($table) - { - $this->table = $table; - } - - /** - * @return string - */ - public function getTable() + public function getTable(): string { return $this->table; } - /** - * @return bool - */ - public function hasSchema() + public function hasSchema(): bool { return $this->schema !== null; } - /** - * @deprecated please use the constructor and build a new {@see TableIdentifier} instead - * - * @param null|string $schema - * @return void - */ - public function setSchema($schema) - { - $this->schema = $schema; - } - - /** - * @return null|string - */ - public function getSchema() + public function getSchema(): ?string { return $this->schema; } /** @return array{0: string, 1: null|string} */ - public function getTableAndSchema() + public function getTableAndSchema(): array { return [$this->table, $this->schema]; } diff --git a/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTest.php b/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTest.php index 91056569a..0d6a8fecf 100644 --- a/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTest.php +++ b/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTest.php @@ -10,6 +10,5 @@ class AdapterTest extends AbstractAdapterTest use AdapterTrait; /** @var Adapter */ - protected $adapter; public const DB_SERVER_PORT = 3306; } diff --git a/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTrait.php b/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTrait.php index 9513063cd..f3e87de95 100644 --- a/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTrait.php +++ b/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTrait.php @@ -9,6 +9,8 @@ trait AdapterTrait { /** @var $adapter */ + protected ?Adapter $adapter; + protected function setUp(): void { if (! getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_MYSQL')) { diff --git a/test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php b/test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php index 6af423eca..98f24691f 100644 --- a/test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php +++ b/test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php @@ -13,8 +13,6 @@ class QueryTest extends TestCase { use AdapterTrait; - /** @var Adapter */ - protected $adapter; /** * @psalm-return arrayexpectNotToPerformAssertions(); + $sql = new Sql($this->adapter); $insert = $sql->update('test'); @@ -87,16 +87,16 @@ public function testNamedParameters() //positional parameters $stmt->execute([ - 1, 'foo', 'bar', + 1, ]); //"mapped" named parameters $stmt->execute([ - 'c_0' => 1, - 'c_1' => 'foo', - 'where1' => 'bar', + 'c_0' => 'foo', + 'c_1' => 'bar', + 'where1' => 1, ]); //real named parameters diff --git a/test/integration/Adapter/Driver/Pdo/Mysql/TableGatewayTest.php b/test/integration/Adapter/Driver/Pdo/Mysql/TableGatewayTest.php index d79e88432..795e78152 100644 --- a/test/integration/Adapter/Driver/Pdo/Mysql/TableGatewayTest.php +++ b/test/integration/Adapter/Driver/Pdo/Mysql/TableGatewayTest.php @@ -2,7 +2,6 @@ namespace LaminasIntegrationTest\Db\Adapter\Driver\Pdo\Mysql; -use Laminas\Db\Adapter\Adapter; use Laminas\Db\Sql\TableIdentifier; use Laminas\Db\TableGateway\Feature\MetadataFeature; use Laminas\Db\TableGateway\TableGateway; @@ -14,8 +13,6 @@ class TableGatewayTest extends TestCase { use AdapterTrait; - /** @var Adapter */ - protected $adapter; /** * @covers \Laminas\Db\TableGateway\TableGateway::__construct */ @@ -68,7 +65,6 @@ public function testInsert() /** * @see https://github.com/zendframework/zend-db/issues/35 * @see https://github.com/zendframework/zend-db/pull/178 - * * @return mixed */ public function testInsertWithExtendedCharsetFieldName() @@ -80,6 +76,7 @@ public function testInsertWithExtendedCharsetFieldName() 'field_' => 'test_value2', ]); $this->assertEquals(1, $affectedRows); + return $tableGateway->getLastInsertValue(); } diff --git a/test/unit/Adapter/Driver/Pdo/StatementIntegrationTest.php b/test/unit/Adapter/Driver/Pdo/StatementIntegrationTest.php index f6df21132..02aabcae1 100644 --- a/test/unit/Adapter/Driver/Pdo/StatementIntegrationTest.php +++ b/test/unit/Adapter/Driver/Pdo/StatementIntegrationTest.php @@ -45,6 +45,7 @@ protected function tearDown(): void public function testStatementExecuteWillConvertPhpBoolToPdoBoolWhenBinding() { + $this->expectNotToPerformAssertions(); $this->pdoStatementMock->expects($this->any())->method('bindParam')->with( $this->equalTo(':foo'), $this->equalTo(false), @@ -55,6 +56,7 @@ public function testStatementExecuteWillConvertPhpBoolToPdoBoolWhenBinding() public function testStatementExecuteWillUsePdoStrByDefaultWhenBinding() { + $this->expectNotToPerformAssertions(); $this->pdoStatementMock->expects($this->any())->method('bindParam')->with( $this->equalTo(':foo'), $this->equalTo('bar'), @@ -65,6 +67,7 @@ public function testStatementExecuteWillUsePdoStrByDefaultWhenBinding() public function testStatementExecuteWillUsePdoStrForStringIntegerWhenBinding() { + $this->expectNotToPerformAssertions(); $this->pdoStatementMock->expects($this->any())->method('bindParam')->with( $this->equalTo(':foo'), $this->equalTo('123'), @@ -75,6 +78,7 @@ public function testStatementExecuteWillUsePdoStrForStringIntegerWhenBinding() public function testStatementExecuteWillUsePdoIntForIntWhenBinding() { + $this->expectNotToPerformAssertions(); $this->pdoStatementMock->expects($this->any())->method('bindParam')->with( $this->equalTo(':foo'), $this->equalTo(123), diff --git a/test/unit/Adapter/Platform/SqlServerTest.php b/test/unit/Adapter/Platform/SqlServerTest.php index e34927b59..e4db83f56 100644 --- a/test/unit/Adapter/Platform/SqlServerTest.php +++ b/test/unit/Adapter/Platform/SqlServerTest.php @@ -171,6 +171,7 @@ public function testQuoteIdentifierInFragment() */ public function testSetDriver() { + $this->expectNotToPerformAssertions(); $driver = new Pdo(['pdodriver' => 'sqlsrv']); $this->platform->setDriver($driver); } diff --git a/test/unit/ResultSet/ResultSetIntegrationTest.php b/test/unit/ResultSet/ResultSetIntegrationTest.php index 0fe24c66a..46e88edb1 100644 --- a/test/unit/ResultSet/ResultSetIntegrationTest.php +++ b/test/unit/ResultSet/ResultSetIntegrationTest.php @@ -121,6 +121,7 @@ public function testCanProvideIteratorAggregateAsDataSource() public function testInvalidDataSourceRaisesException($dataSource) { if (is_array($dataSource)) { + $this->expectNotToPerformAssertions(); // this is valid return; } diff --git a/test/unit/Sql/AbstractSqlTest.php b/test/unit/Sql/AbstractSqlTest.php index 96c973b0f..bfb253e70 100644 --- a/test/unit/Sql/AbstractSqlTest.php +++ b/test/unit/Sql/AbstractSqlTest.php @@ -51,7 +51,7 @@ protected function setUp(): void */ public function testProcessExpressionWithoutParameterContainer() { - $expression = new Expression('? > ? AND y < ?', ['x', 5, 10], [Expression::TYPE_IDENTIFIER]); + $expression = new Expression('? > ? AND y < ?', [['x' => ExpressionInterface::TYPE_IDENTIFIER], 5, 10]); $sqlAndParams = $this->invokeProcessExpressionMethod($expression); self::assertEquals("\"x\" > '5' AND y < '10'", $sqlAndParams); @@ -63,7 +63,7 @@ public function testProcessExpressionWithoutParameterContainer() public function testProcessExpressionWithParameterContainerAndParameterizationTypeNamed() { $parameterContainer = new ParameterContainer(); - $expression = new Expression('? > ? AND y < ?', ['x', 5, 10], [Expression::TYPE_IDENTIFIER]); + $expression = new Expression('? > ? AND y < ?', [['x' => ExpressionInterface::TYPE_IDENTIFIER], 5, 10]); $sqlAndParams = $this->invokeProcessExpressionMethod($expression, $parameterContainer); $parameters = $parameterContainer->getNamedArray(); diff --git a/test/unit/Sql/ExpressionTest.php b/test/unit/Sql/ExpressionTest.php index 7c3e23968..3eeda3108 100644 --- a/test/unit/Sql/ExpressionTest.php +++ b/test/unit/Sql/ExpressionTest.php @@ -5,6 +5,7 @@ use Laminas\Db\Sql\Exception\InvalidArgumentException; use Laminas\Db\Sql\Expression; use PHPUnit\Framework\TestCase; +use TypeError; /** * This is a unit testing test case. @@ -32,9 +33,13 @@ public function testSetExpression() public function testSetExpressionException() { $expression = new Expression(); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Supplied expression must be a string.'); + $this->expectException(TypeError::class); $expression->setExpression(null); + + $expression = new Expression(); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Supplied expression must not be an empty string.'); + $expression->setExpression(''); } /** @@ -64,8 +69,7 @@ public function testSetParametersException() { $expression = new Expression('', 'foo'); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Expression parameters must be a scalar or array.'); + $this->expectException(TypeError::class); $expression->setParameters(null); } @@ -78,54 +82,11 @@ public function testGetParameters(Expression $expression) self::assertEquals('foo', $expression->getParameters()); } - /** - * @covers \Laminas\Db\Sql\Expression::setTypes - */ - public function testSetTypes(): Expression - { - $expression = new Expression(); - $return = $expression->setTypes([ - Expression::TYPE_IDENTIFIER, - Expression::TYPE_VALUE, - Expression::TYPE_LITERAL, - ]); - self::assertSame($expression, $return); - return $expression; - } - - /** - * @covers \Laminas\Db\Sql\Expression::getTypes - * @depends testSetTypes - */ - public function testGetTypes(Expression $expression) - { - self::assertEquals( - [Expression::TYPE_IDENTIFIER, Expression::TYPE_VALUE, Expression::TYPE_LITERAL], - $expression->getTypes() - ); - } - /** * @covers \Laminas\Db\Sql\Expression::getExpressionData */ public function testGetExpressionData() { - $expression = new Expression( - 'X SAME AS ? AND Y = ? BUT LITERALLY ?', - ['foo', 5, 'FUNC(FF%X)'], - [Expression::TYPE_IDENTIFIER, Expression::TYPE_VALUE, Expression::TYPE_LITERAL] - ); - - self::assertEquals( - [ - [ - 'X SAME AS %s AND Y = %s BUT LITERALLY %s', - ['foo', 5, 'FUNC(FF%X)'], - [Expression::TYPE_IDENTIFIER, Expression::TYPE_VALUE, Expression::TYPE_LITERAL], - ], - ], - $expression->getExpressionData() - ); $expression = new Expression( 'X SAME AS ? AND Y = ? BUT LITERALLY ?', [ @@ -200,8 +161,7 @@ public function testConstructorWithFalsyValidParameters($falsyParameter) public function testConstructorWithInvalidParameter() { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Expression parameters must be a scalar or array.'); + $this->expectException(TypeError::class); new Expression('?', (object) []); } diff --git a/test/unit/Sql/SelectTest.php b/test/unit/Sql/SelectTest.php index 3dc6a3d49..35cec4028 100644 --- a/test/unit/Sql/SelectTest.php +++ b/test/unit/Sql/SelectTest.php @@ -717,6 +717,8 @@ function ($name) use ($useNamedParameters) { if ($expectedParameters) { self::assertEquals($expectedParameters, $parameterContainer->getNamedArray()); + } else { + $this->expectNotToPerformAssertions(); } } @@ -797,6 +799,7 @@ public function testCloning() public function testProcessMethods(Select $select, $unused, $unused2, $unused3, array $internalTests) { if (! $internalTests) { + $this->expectNotToPerformAssertions(); return; } @@ -914,8 +917,11 @@ public function providerData(): array [ new Expression( '(COUNT(?) + ?) AS ?', - ['some_column', 5, 'bar'], - [Expression::TYPE_IDENTIFIER, Expression::TYPE_VALUE, Expression::TYPE_IDENTIFIER] + [ + ['some_column' => ExpressionInterface::TYPE_IDENTIFIER], + [5 => ExpressionInterface::TYPE_VALUE], + ['bar' => ExpressionInterface::TYPE_IDENTIFIER], + ], ), ] ); @@ -1017,7 +1023,7 @@ public function providerData(): array ]; $select19 = new Select(); - $select19->from('foo')->group(new Expression('DAY(?)', ['col1'], [Expression::TYPE_IDENTIFIER])); + $select19->from('foo')->group(new Expression('DAY(?)', [['col1' => ExpressionInterface::TYPE_IDENTIFIER]])); $sqlPrep19 = // same $sqlStr19 = 'SELECT "foo".* FROM "foo" GROUP BY DAY("col1")'; $internalTests19 = [ @@ -1172,7 +1178,7 @@ public function providerData(): array // @author Demian Katz $select34 = new Select(); $select34->from('table')->order([ - new Expression('isnull(?) DESC', ['name'], [Expression::TYPE_IDENTIFIER]), + new Expression('isnull(?) DESC', [['name' => ExpressionInterface::TYPE_IDENTIFIER]]), 'name', ]); $sqlPrep34 = 'SELECT "table".* FROM "table" ORDER BY isnull("name") DESC, "name" ASC'; diff --git a/test/unit/Sql/SqlFunctionalTest.php b/test/unit/Sql/SqlFunctionalTest.php index 9753af28b..7245cf808 100644 --- a/test/unit/Sql/SqlFunctionalTest.php +++ b/test/unit/Sql/SqlFunctionalTest.php @@ -621,7 +621,7 @@ public function test($sqlObject, $platform, $expected) $expectedString = is_string($expected) ? $expected : ($expected['string'] ?? null); if ($expectedString) { - $actual = $sql->getSqlStringForSqlObject($sqlObject); + $actual = $sql->buildSqlString($sqlObject); self::assertEquals($expectedString, $actual, "getSqlString()"); } if (is_array($expected) && isset($expected['prepare'])) { diff --git a/test/unit/Sql/SqlTest.php b/test/unit/Sql/SqlTest.php index 3f3d86cd0..0dddbcae0 100644 --- a/test/unit/Sql/SqlTest.php +++ b/test/unit/Sql/SqlTest.php @@ -16,6 +16,7 @@ use LaminasTest\Db\TestAsset; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use TypeError; class SqlTest extends TestCase { @@ -64,8 +65,7 @@ public function test__construct() $sql->setTable('foo'); self::assertSame('foo', $sql->getTable()); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Table must be a string, array or instance of TableIdentifier.'); + $this->expectException(TypeError::class); $sql->setTable(null); } diff --git a/test/unit/Sql/TableIdentifierTest.php b/test/unit/Sql/TableIdentifierTest.php index d20548d53..889842cd0 100644 --- a/test/unit/Sql/TableIdentifierTest.php +++ b/test/unit/Sql/TableIdentifierTest.php @@ -6,6 +6,7 @@ use Laminas\Db\Sql\TableIdentifier; use PHPUnit\Framework\TestCase; use stdClass; +use TypeError; use function array_merge; @@ -43,7 +44,7 @@ public function testGetTableFromObjectStringCast() $table->expects($this->once())->method('__toString')->will($this->returnValue('castResult')); - $tableIdentifier = new TableIdentifier($table); + $tableIdentifier = new TableIdentifier((string) $table); self::assertSame('castResult', $tableIdentifier->getTable()); self::assertSame('castResult', $tableIdentifier->getTable()); @@ -55,7 +56,7 @@ public function testGetSchemaFromObjectStringCast() $schema->expects($this->once())->method('__toString')->will($this->returnValue('castResult')); - $tableIdentifier = new TableIdentifier('foo', $schema); + $tableIdentifier = new TableIdentifier('foo', (string) $schema); self::assertSame('castResult', $tableIdentifier->getSchema()); self::assertSame('castResult', $tableIdentifier->getSchema()); @@ -67,7 +68,7 @@ public function testGetSchemaFromObjectStringCast() */ public function testRejectsInvalidTable($invalidTable) { - $this->expectException(InvalidArgumentException::class); + $this->expectException($invalidTable === '' ? InvalidArgumentException::class : TypeError::class); new TableIdentifier($invalidTable); } @@ -78,7 +79,7 @@ public function testRejectsInvalidTable($invalidTable) */ public function testRejectsInvalidSchema($invalidSchema) { - $this->expectException(InvalidArgumentException::class); + $this->expectException($invalidSchema === '' ? InvalidArgumentException::class : TypeError::class); new TableIdentifier('foo', $invalidSchema); }