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

Stubbing DBAL QueryBuilder #45

Merged
merged 2 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use function class_exists;
use function glob;

use const GLOB_BRACE;

class Plugin implements PluginEntryPointInterface
{
/** @return void */
Expand All @@ -27,7 +29,7 @@ public function __invoke(RegistrationInterface $psalm, ?SimpleXMLElement $config
/** @return string[] */
private function getStubFiles(): array
{
return glob(__DIR__ . '/' . 'stubs/*.php');
return glob(__DIR__ . '/' . 'stubs/{,*/}*.php', GLOB_BRACE);
}

/** @return string[] */
Expand Down
91 changes: 91 additions & 0 deletions stubs/DBAL/QueryBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Doctrine\DBAL\Query;

/**
* @psalm-type _WhereExpr=Expression\CompositeExpression|string
* @psalm-type _SelectExpr=string
* @psalm-type _GroupExpr=string
*/
class QueryBuilder
{
/**
* @param _SelectExpr|_SelectExpr[]|null $select
* @param _SelectExpr ...$selects
*/
public function select($select = null, ...$selects): self
{
}

/**
* @param _SelectExpr|_SelectExpr[]|null $select
* @param _SelectExpr ...$selects
*/
public function addSelect($select = null, ...$selects): self
{
}

/**
* @param _WhereExpr $predicate
* @param _WhereExpr ...$predicates
*/
public function where($predicate, ...$predicates): self
{
}

/**
* @param _WhereExpr $predicate
* @param _WhereExpr ...$predicates
*/
public function andWhere($predicate, ...$predicates): self
{
}

/**
* @param _WhereExpr $predicate
* @param _WhereExpr ...$predicates
*/
public function orWhere($predicate, ...$predicates): self
{
}

/**
* @param _GroupExpr|_GroupExpr[] $predicate
* @param _GroupExpr ...$predicates
*/
public function groupBy($predicate, ...$predicates): self
{
}

/**
* @param _GroupExpr|_GroupExpr[] $predicate
* @param _GroupExpr ...$predicates
*/
public function addGroupBy($predicate, ...$predicates): self
{
}

/**
* @param _WhereExpr $predicate
* @param _WhereExpr ...$predicates
*/
public function having($predicate, ...$predicates): self
{
}

/**
* @param _WhereExpr $predicate
* @param _WhereExpr ...$predicates
*/
public function andHaving($predicate, ...$predicates): self
{
}

/**
* @param _WhereExpr $predicate
* @param _WhereExpr ...$predicates
*/
public function orHaving($predicate, ...$predicates): self
{
}
}
4 changes: 2 additions & 2 deletions tests/acceptance/QueryBuilder.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: QueryBuilder
In order to to use Doctrine QueryBuilder safely
In order to use Doctrine ORM QueryBuilder safely
As a Psalm user
I need Psalm to typecheck QueryBuilder

Expand Down Expand Up @@ -89,7 +89,7 @@ Feature: QueryBuilder
Given I have the following code
"""
$expr = new Expr\Andx(['a = b']);
builder()->having([$expr])->distinct();
builder()->andHaving([$expr])->distinct();
"""
When I run Psalm
Then I see no errors
Expand Down
135 changes: 135 additions & 0 deletions tests/acceptance/QueryBuilderDbal.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
Feature: QueryBuilderDbal
In order to use Doctrine DBAL QueryBuilder safely
As a Psalm user
I need Psalm to typecheck QueryBuilder

Background:
Given I have the following config
"""
<?xml version="1.0"?>
<psalm totallyTyped="true">
<projectFiles>
<directory name="."/>
</projectFiles>
<plugins>
<pluginClass class="Weirdan\DoctrinePsalmPlugin\Plugin" />
</plugins>
</psalm>
"""
And I have the following code preamble
"""
<?php
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Query\Expression\CompositeExpression;

/**
* @psalm-suppress InvalidReturnType
* @return QueryBuilder
*/
function builder() {}
"""

@QueryBuilderDbal
Scenario: Dbal QueryBuilder ::select accepts variadic arguments
Given I have the following code
"""
builder()->select('field1', 'field2')->distinct();
weirdan marked this conversation as resolved.
Show resolved Hide resolved
"""
When I run Psalm
Then I see no errors

@QueryBuilderDbal
Scenario: Dbal QueryBuilder ::select accepts array argument
Given I have the following code
"""
builder()->select(['field1', 'field1'])->distinct();
"""
When I run Psalm
Then I see no errors

@QueryBuilderDbal
Scenario: Dbal QueryBuilder ::addSelect accepts variadic arguments
Given I have the following code
"""
builder()->addSelect('field1', 'field2')->distinct();
"""
When I run Psalm
Then I see no errors

@QueryBuilderDbal
Scenario: Dbal QueryBuilder ::addSelect accepts array argument
Given I have the following code
"""
builder()->addSelect(['field1', 'field2'])->distinct();
"""
When I run Psalm
Then I see no errors

@QueryBuilderDbal
Scenario: Dbal QueryBuilder ::where, ::orWhere and ::andWhere accept variadic arguments
Given I have the following code
"""
builder()->where('field1', 'field2')
->andWhere('field1', 'field2')
->orWhere('field1', 'field2')
->distinct();
"""
When I run Psalm
Then I see no errors

@QueryBuilderDbal
Scenario: Dbal QueryBuilder ::where, ::orWhere and ::andWhere accept CompositeExpression
Given I have the following code
"""
$expr = builder()->expr();
$orx = $expr->orX();
$orx->add($expr->eq('field1', 1));
$orx->add($expr->eq('field1', 2));
builder()->where($orx)->andWhere($orx)->orWhere($orx)->distinct();
"""
When I run Psalm
Then I see no errors

@QueryBuilderDbal
Scenario: Dbal QueryBuilder ::groupBy ::addGroupBy accept variadic arguments
Given I have the following code
"""
builder()->groupBy('field1', 'field2')
->addGroupBy('field1', 'field2')
->distinct();
"""
When I run Psalm
Then I see no errors

@QueryBuilderDbal
Scenario: Dbal QueryBuilder ::groupBy ::addGroupBy accept array argument
Given I have the following code
"""
builder()->groupBy(['field1', 'field2'])
->addGroupBy(['field1', 'field2'])
->distinct();
"""
When I run Psalm
Then I see no errors

@QueryBuilderDbal
Scenario: Dbal QueryBuilder ::having, ::orHaving and ::andHaving accept variadic arguments
Given I have the following code
"""
builder()->having('field1', 'field2')
->orHaving('field1', 'field2')
->andHaving('field1', 'field2')
->distinct();
"""
When I run Psalm
Then I see no errors

@QueryBuilderDbal
Scenario: Dbal QueryBuilder ::having, ::orHaving and ::andHaving accept CompositeExpression
Given I have the following code
"""
$andx = builder()->expr()->andX('a = b');
builder()->having($andx)->orHaving($andx)->andHaving($andx)->distinct();
"""
When I run Psalm
Then I see no errors