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

Enable basic PHPSTAN checks #239

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ jobs:

cs:
runs-on: ubuntu-20.04
name: CS tests
name: CS & PHPSTAN tests

steps:
- uses: actions/checkout@v2
Expand All @@ -142,3 +142,6 @@ jobs:

- name: Run CS tests
run: vendor/bin/php-cs-fixer fix --config=.php_cs -v --dry-run --using-cache=no --show-progress=dots --diff $(git diff -- '*.php' --name-only --diff-filter=ACMRTUXB "HEAD~..HEAD")

- name: Run PHPSTAN
run: composer phpstan
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
.phpunit.result.cache
/.ddev/mautic-preference
/mautic
.php_cs.cache
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
"raveren/kint": "~0.9",
"friendsofphp/php-cs-fixer": "~2.16.1",
"phpunit/phpunit": "~9.5.0",
"brianium/paratest": "^6.2"
"brianium/paratest": "^6.2",
"phpstan/phpstan": "^0.12.78"
},
"scripts": {
"test": "vendor/bin/paratest"
"test": "vendor/bin/paratest",
"phpstan": "vendor/bin/phpstan analyse",
"cs": "vendor/bin/php-cs-fixer fix --config=.php_cs"
}
}
110 changes: 59 additions & 51 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Mautic\Api;

use Mautic\Auth\ApiAuth;
use Mautic\Auth\AbstractAuth;
use Mautic\Auth\AuthInterface;
use Mautic\QueryBuilder\QueryBuilder;
use Psr\Log\LoggerAwareInterface;
Expand Down Expand Up @@ -86,7 +86,7 @@ class Api implements LoggerAwareInterface
protected $searchCommands = [];

/**
* @var ApiAuth
* @var AbstractAuth
*/
private $auth;

Expand Down
2 changes: 1 addition & 1 deletion lib/MauticApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function getContext($apiContext, AuthInterface $auth, $baseUrl = '

$apiContext = ucfirst($apiContext);

if (!isset($context[$apiContext])) {
if (!isset($contexts[$apiContext])) {
$class = 'Mautic\\Api\\'.$apiContext;

if (!class_exists($class)) {
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 1
paths:
- lib
- tests
9 changes: 6 additions & 3 deletions tests/Api/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Mautic\Tests\Api;

use Mautic\Api\Files;

class AssetsTest extends MauticApiTestCase
{
protected $skipPayloadAssertion = ['file'];
Expand Down Expand Up @@ -37,12 +39,13 @@ public function testGetListOfSpecificIds()
public function testCreateWithLocalFileGetAndDelete()
{
// Upload a testing file
$this->apiFiles = $this->getContext('files');
$this->apiFiles->setFolder('assets');
/** @var Files */
$apiFiles = $this->getContext('files');
$apiFiles->setFolder('assets');
$fileRequest = [
'file' => dirname(__DIR__).'/'.'mauticlogo.png',
];
$response = $this->apiFiles->create($fileRequest);
$response = $apiFiles->create($fileRequest);
$this->assertErrors($response);
$file = $response['file'];

Expand Down
2 changes: 1 addition & 1 deletion tests/Api/CampaignsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public function testCampaignContactEditEvent()
$date = new \DateTime($log['triggerDate'], new \DateTimeZone('UTC'));
$this->assertEquals($log['triggerDate'], $date->format('c'));
} else {
$this->assertFalse(false, 'Event ID not recognized in the log.', var_export($event, true));
$this->assertFalse(var_export($event, true), 'Event ID not recognized in the log.');
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make much sense. The previous version did not make sense either. I expect the intent was to fail the test if this assert will run. How about this:

Suggested change
$this->assertFalse(var_export($event, true), 'Event ID not recognized in the log.');
$this->fail('Event ID not recognized in the log.'.var_export($event, true));

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

Expand Down
12 changes: 11 additions & 1 deletion tests/Api/FormsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@

namespace Mautic\Tests\Api;

use Mautic\Api\Forms;

class FormsTest extends MauticApiTestCase
{
/** @var Forms */
protected $api;

public function setUp(): void
{
$this->api = $this->getContext('forms');
Expand Down Expand Up @@ -218,10 +223,15 @@ public function testFormSubmissions()
$response = $this->api->getSubmissions($formId);
$this->assertErrors($response);

foreach ($response['submissions'] as $submission) {
$submissions = $response['submissions'];
$this->assertTrue(count($submissions) > 0, 'Expected at least one form submission');

foreach ($submissions as $submission) {
$this->assertSubmission($submission, $formId);
}

$submission = end($submissions);

// Try to fetch the last submission
$response = $this->api->getSubmission($formId, $submission['id']);
$this->assertErrors($response);
Expand Down
4 changes: 2 additions & 2 deletions tests/Api/MauticApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function getAuth()
$auth = $apiAuth->newAuth($this->config, $authMethod);
if ('BasicAuth' != $authMethod) {
if (empty($this->config['refreshToken']) && !$auth->isAuthorized()) {
$this->assertTrue($authorized, 'Authorization failed. Check credentials in local.config.php.');
$this->assertTrue($auth->isAuthorized(), 'Authorization failed. Check credentials in local.config.php.');
} else {
try {
$auth->validateAccessToken();
Expand Down Expand Up @@ -179,7 +179,7 @@ protected function standardTestGetListOfSpecificIds($callback = null)
$itemIds[] = $response[$this->api->itemName()]['id'];
}

if (is_callable($callback)) {
if (is_callable($callback) && !empty($response)) {
// Allow support to make additional tests based on created associations with this item that
// may not have create/edit endpoints
$callback($response);
Expand Down
6 changes: 5 additions & 1 deletion tests/Api/UtmTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ protected function removeUtmTags($utmIds)
}
}

$this->assertSame(0, count($response[$this->api->itemName()]['utmtags']), 'Should be no more items');
if (!empty($response)) {
$this->assertSame(0, count($response[$this->api->itemName()]['utmtags']), 'Should be no more items');
} else {
throw new \Exception('Expected a reponse object');
}
}

protected function addUtmTags()
Expand Down