diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 62296b6..6ec3ff3 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -149,9 +149,9 @@ jobs: SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} SLACK_MESSAGE: 'The daily API library tests against mautic/mautic have failed. Most likely a PR was merged recently which introduced a regression of some sort.' - cs: + tests: runs-on: ubuntu-20.04 - name: CS tests + name: CS & PHPSTAN tests steps: - uses: actions/checkout@v3 @@ -169,3 +169,6 @@ jobs: - name: Run CS tests run: vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php -v --dry-run --using-cache=no --show-progress=dots --diff $(git diff -- '*.php' --name-only --diff-filter=ACMRTUXB "HEAD~..HEAD") + + - name: Run PHPSTAN tests + run: composer phpstan \ No newline at end of file diff --git a/composer.json b/composer.json index 26aabce..ba85d5a 100644 --- a/composer.json +++ b/composer.json @@ -26,13 +26,15 @@ "kint-php/kint": "^4.1", "friendsofphp/php-cs-fixer": "~3.12.0", "phpunit/phpunit": "~9.5.0", - "guzzlehttp/guzzle": "^7.5" + "guzzlehttp/guzzle": "^7.5", + "phpstan/phpstan": "^1.11" }, "suggest": { "guzzlehttp/guzzle": "A popular HTTP client that implements psr/http-client-implementation." }, "scripts": { - "test": "vendor/bin/phpunit" + "test": "vendor/bin/phpunit", + "phpstan": "vendor/bin/phpstan analyse" }, "config": { "allow-plugins": { diff --git a/lib/MauticApi.php b/lib/MauticApi.php index 15c44ba..b7bd04c 100644 --- a/lib/MauticApi.php +++ b/lib/MauticApi.php @@ -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)) { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..6c4b2eb --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + level: 1 + paths: + - lib + - tests \ No newline at end of file diff --git a/tests/Api/AssetsTest.php b/tests/Api/AssetsTest.php index 5fb0041..112e004 100644 --- a/tests/Api/AssetsTest.php +++ b/tests/Api/AssetsTest.php @@ -43,12 +43,12 @@ public function testGetListOfSpecificIds() public function testCreateWithLocalFileGetAndDelete() { // Upload a testing file - $this->apiFiles = $this->getContext('files'); - $this->apiFiles->setFolder($this->mediaFolder); + $apiFiles = $this->getContext('files'); + $apiFiles->setFolder($this->mediaFolder); $fileRequest = [ 'file' => dirname(__DIR__).'/mauticlogo.png', ]; - $response = $this->apiFiles->create($fileRequest); + $response = $apiFiles->create($fileRequest); $this->assertErrors($response); $file = $response['file']; diff --git a/tests/Api/CampaignsTest.php b/tests/Api/CampaignsTest.php index 42d68ed..79729ac 100644 --- a/tests/Api/CampaignsTest.php +++ b/tests/Api/CampaignsTest.php @@ -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->fail('Event ID not recognized in the log.'.var_export($event, true)); } } diff --git a/tests/Api/FormsTest.php b/tests/Api/FormsTest.php index ec98636..814b87a 100644 --- a/tests/Api/FormsTest.php +++ b/tests/Api/FormsTest.php @@ -218,10 +218,15 @@ public function testFormSubmissions() $response = $this->api->getSubmissions($formId); $this->assertErrors($response); + $submissions = $response['submissions']; + $this->assertTrue(count($submissions) > 0, 'Expected at least one form submission'); + foreach ($response['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); diff --git a/tests/Api/UtmTagsTest.php b/tests/Api/UtmTagsTest.php index eadffe4..8eca4be 100644 --- a/tests/Api/UtmTagsTest.php +++ b/tests/Api/UtmTagsTest.php @@ -99,7 +99,7 @@ public function testAddCheckActiveDateAndDelete() $utmIds = $this->addUtmTags(); // check the date; Should be empty - $this->assertTrue(empty($response[$this->api->itemName()]['lastActive'])); + $this->assertTrue(empty($utmIds[$this->api->itemName()]['lastActive'])); // Now add the payload with a known date $this->testPayload['lastActive'] = '2017-01-17T00:30:08+00:00'; @@ -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 { + $this->fail('Expected a response object'); + } } protected function addUtmTags()