Omnipay common abstract-class tests fail - phpunit

I'm just looking at Omnipay with the aim of adding support for another gateway. After installing with Composer, I tried running the Paypal (as an example gateway) and Omnipay Common tests. With both packages, I adjusted bootstrap.php so that it could locate the composer autoload file. The Paypal tests run fine (green).
However, the common tests fail with 1 failure and 38 errors. All failures/errors are in AbstractGatewayTest, AbstractRequestTest, AbstractResponseTest.
This leads me to wonder if something changed in how PHPUnit or Mockery handle mocked abstract classes. I'm using PHPUnit 3.7.34. I have most recently been using PHPUnit 4.0.* for Laravel projects, but Omnipay requires 3.7.
composer.lock indicates I'm using Mockery 0.9.0. I'm running php 5.4.* installed via macports on OS X Mavericks.
Anyone have insight into what might be tripping me up? Thanks!
EDIT: Sample failure messages:
There were 38 errors:
1) Omnipay\Common\AbstractGatewayTest::testGetShortName
BadMethodCallException: Method Mockery_0_Omnipay_Common_AbstractGateway::getDefaultParameters() does not exist on this mock object
.../vendor/omnipay/common/src/Omnipay/Common/AbstractGateway.php:53
.../vendor/omnipay/common/src/Omnipay/Common/AbstractGateway.php:40
.../vendor/mockery/mockery/library/Mockery/Container.php:426
.../vendor/mockery/mockery/library/Mockery/Container.php:210
.../vendor/mockery/mockery/library/Mockery.php:71
.../vendor/omnipay/common/tests/Omnipay/Common/AbstractGatewayTest.php:12
2) Omnipay\Common\Message\AbstractRequestTest::testInitializeWithParams
Argument 1 passed to Omnipay\Common\Message\AbstractRequest::__construct() must implement interface Guzzle\Http\ClientInterface, none given
.../vendor/omnipay/common/src/Omnipay/Common/Message/AbstractRequest.php:46
.../vendor/mockery/mockery/library/Mockery/Container.php:426
.../vendor/mockery/mockery/library/Mockery/Container.php:210
.../vendor/mockery/mockery/library/Mockery.php:71
.../vendor/omnipay/common/tests/Omnipay/Common/Message/AbstractRequestTest.php:14
where "..." is my source code area.

Following Adrian Macneil's advice and explanation, I did a separate checkout of the Omnipay common files, and the unit tests indeed run green (successfully):
git clone git#github.com:omnipay/common.git
cd common/
composer update --dev && composer dump-autoload
vendor/bin/phpunit
yields:
PHPUnit 3.7.34 by Sebastian Bergmann.
Configuration read from /Users/ewb/startrib/omnipay/common/phpunit.xml.dist
............................................................... 63 / 161 ( 39%)
............................................................... 126 / 161 ( 78%)
...................................
Time: 94 ms, Memory: 8.00Mb
OK (161 tests, 258 assertions)
For the record, I adjust bootstrap.php as follows. This allows me to run the tests when they are included by Composer as dependencies. I'm new to both composer and Laravel (omnipay has nothing to do with Laravel), so my approach appears to not be best practice. Thanks for the advice!
The following version of bootstrap.php walks up its source tree (with a sanity limit of 5 levels) until it finds the directory containing vendor/autoload.php.
<?php
error_reporting(E_ALL | E_STRICT);
// Locate and include the composer autoloader
$sanity = 5;
$dir = realpath(__DIR__);
do {
$dir = dirname($dir);
$autoload = $dir.'/vendor/autoload.php';
} while ($sanity-- && !file_exists($autoload));
$autoloader = require $autoload;
// autoload abstract TestCase classes in test directory
$autoloader->add('Omnipay', __DIR__);

Related

Codeception Simple Unit Test Not Work To Find My Namespaces

I'm trying a basic UnitTest with Codeception. No frameworks are used.
My working root is as:
tests |
|-unit
|-Test.php
includes|
|-general
|-Custom.php
In Custom.php
<?php
namespace Custom;
class General {
public static function check(){}
}
My test case is:
<?php
use Custom\General;
use PHPUnit\Framework\TestCase;
final class Test extends TestCase
{
public function testPushAndPop(): void
{
General::check();
}
}
I also have in my composer.json:
"autoload": {
"psr-4":{
"Custom\\":"includes/general"
}
},
When I run
php vendor/bin/codecept run unit
...
1) Test: Push and pop
Test tests/unit/Test.php:testPushAndPop
[Error] Class 'DB\General' not found
#1 /var/www/html/prj/tests/unit/Test.php:9
Codeception Simple Unit Test Not Work To Find My Namespaces
This is less about Codeception but how PHP autoloading works in general and what the configuration of autoloading in Composer is in specific:
{
"autoload": {
"psr-4":{
"Custom\\":"includes/general"
}
}
}
Even the path-segment includes/general does map on the file-system path includes/general/ (composer should have told you to add / at the end of the path-segment), the error message
[Error] Class 'DB\General' not found
#1 /var/www/html/prj/tests/unit/Test.php:9
shows that the namespace of Custom\\ in the Composer configuration is different to the namespace of the class that is not found (which is DB\\).
So even the framework you have in use (you have one and that is the testing framework) may load the composer auto-loader (highly likely), its just that the class is not found.
As #Naktibalda already highlighted in a comment, it is just a plain autoloader configuration issue.
You are right, but why? The IDE does not claim any error... (your reaction)
These are two pair of shoes.
Your IDE most likely does not rely on the autoloader and just guesses the file from the file-system.
Depending how well maintained and configured your IDE is, it perhaps should and could have highlighted you that.
On the other hand, PHP can only rely on the autoloader, in your case you delegate the autoloading to composer(1), not to your IDE.
So maybe improve on that end as well, composer is more central for your projects development than the IDE can be.
So a suggestion:
Whenever changing the composer.json file, I suggest to run composer validate --strict to check your status, then follow with a composer update.
Run this automatically before running your tests. You may not want to run composer update, then run composer install before running the test-runner if you have it as a development dependency.
Example to bind this for a single test run command within your composer.json:
{
"scripts": {
"test": [
"#composer --no-plugins --version",
"#composer validate --strict",
"#composer --no-plugins -q install --no-scripts --no-progress",
"codecept run unit"
]
}
}
you then have a single call to run all the important things in your Composer based project:
$ composer test
...
Running unit-tests is a good way to verify the autoload configuration by the way. With the composer test-script you ensure it is always up-to-date when running the test-suite, too.
You don't need an IDE for this technically, which makes it more portable and stable giving you the peace of mind for your composer based project to grow.

Autowiring does not work in Symfony 5.1 demo project with PHP 7.4.4

I am attempting to bootstrap a simple Symfony 5.1 project and I cannot get dependency injection / autowiring support to work. I am quite at the end of my abilities and need help. Here is what I do:
Create empty project by running: composer create-project symfony/website-skeleton testProject
Create files testProject/src/Util/Rot13Transformer.php and testProject/src/Service/TwitterClient.php with contents copy-pasted from this tutorial page: https://symfony.com/doc/current/service_container/autowiring.html
Check the services in console by executing
php bin/console debug:container App\Util\Rot13Transformer and php bin/console debug:container App\Service\TwitterClient
Output for both is:
------
Public no
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired yes
Autoconfigured yes
------
Open file testProject/public/index.php, add a new line after other use statements:
use App\Service\TwitterClient;
Add a new line just before kernel initialization at the end of the index file with following content:
$client = new TwitterClient();
Run this file on console with
php public/index.php
I always end up with error:
Too few arguments to function
App\Service\TwitterClient::__construct(), 0 passed in
/Users/oink/Documents/Development/testProject/public/index.php on line
27 and exactly 1 expected
Why? Should not autowiring take care of Rot13Transformer argument? Why does not it work? This is a vanilla example made on the basis of skeleton and with scripts copied/pasted from Symfony web site.
My PHP version is 7.4.4
Thanks!!

class PHPUnit\Framework\ExpectationFailedException not found

when I try to run a failed test with this command :
./vendor/bin/phpunit
I get this Fatal Error :
PHPUnit 5.7.20 by Sebastian Bergmann and contributors.
PHP Fatal error: Class 'PHPUnit\Framework\ExpectationFailedException'
not found in /var/www/zend/vendor/zendframework/zend-
test/src/PHPUnit/Controller/AbstractControllerTestCase.php on line 444
Your version of phpunit is probably too old for your version of Zend. The class PHPUnit\Framework\ExpectationFailedException have been renamed in PhpUnit 6.X from PHPUnit_Framework_ExpectationFailedException to ExpectationFailedException
Please check your PhpUnit version: phpunit --version, it should be 6.X. Update it to the last version to avoid this error.
This is "fixed" by a script in Zend\Test called phpunit-class-aliases.php but it's not configured properly IMHO since it's in the autoload-dev section (meaning it doesn't propagate out to other projects.)
So, in your project composer.json, do something like this:
"autoload-dev": {
"files": [
"vendor/zendframework/zend-test/autoload/phpunit-class-aliases.php"
]
},
Then composer install
N.B. Zend\Test has a pull request that fixes this very thing, but they're saying it's PHPUnit's fault (Shame on you PHPUnit 4 for... idunno... having the wrong class name according to Zend\Test) So, I've done it instead: composer require illchuk/phpunit-class-aliases
This is a configuration flaw in zend-test. It consumes classes from Phpunit 6 but per it's Composer requirements, Phpunit before that version are OK to require:
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0",
Most likely as your system because of the PHP version does not satisfy the requirements of Phpunit 6, the next lower version was installed.
As the code in the base test case (https://github.com/zendframework/zend-test/blob/master/src/PHPUnit/Controller/AbstractControllerTestCase.php#L444) makes use of Phpunit 6 classes, I strongly assume that when the configuration flaw is made aware to the Zend-Test project, you won't be even able to install on your system any longer.
Therefore upgrade to a recent PHP version and then run
composer update
If you're stuk with the PHP version, downgrade zend-test to a version that supports an older Phpunit version. I don't know that project well, so it's just a suggestion, I don't know if such a version exists or can't even recommend one.
I filed a report, perhaps using that one class was an oversight or there is a less hard way to resolve the dependency: https://github.com/zendframework/zend-test/issues/50

PhpUnit Artisan:call explode expects parameter 2 to be string

I'm trying to test a Laravel 5.1 controller with PhpUnit, for that im creating a class inheriting from TestCase, and using DatabaseTransactions and WithoutMiddleware traits.
Within the class im implementing the setUpBeforeClass method which contains:
Artisan::call('migrate:refresh');
When i try to run the test I get the following error:
1) JugadoresControllerTest::test_Index_trae_arreglo_de_jugadores
ErrorException: explode() expects parameter 2 to be string, array given
/home/vagrant/.composer/vendor/illuminate/support/helpers.php:390
/home/vagrant/Code/marcadores/vendor/laravel/framework/src/Illuminate/Support/Arr.php:319
/home/vagrant/Code/marcadores/vendor/laravel/framework/src/Illuminate/Support/Collection.php:428
/home/vagrant/Code/marcadores/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:1548
/home/vagrant/Code/marcadores/vendor/laravel/framework/src/Illuminate/Database/Migrations/DatabaseMigrationRepository.php:53
/home/vagrant/Code/marcadores/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:79
/home/vagrant/Code/marcadores/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:74
/home/vagrant/Code/marcadores/vendor/laravel/framework/src/Illuminate/Container/Container.php:503
/home/vagrant/Code/marcadores/vendor/laravel/framework/src/Illuminate/Console/Command.php:150
/home/vagrant/Code/marcadores/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:259
/home/vagrant/Code/marcadores/vendor/laravel/framework/src/Illuminate/Console/Command.php:136
/home/vagrant/Code/marcadores/vendor/laravel/framework/src/Illuminate/Console/Application.php:62
/home/vagrant/Code/marcadores/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:152
/home/vagrant/Code/marcadores/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:210
/home/vagrant/Code/marcadores/tests/unit/JugadoresControllerTest.php:34
/home/vagrant/Code/marcadores/tests/unit/JugadoresControllerTest.php:34
/home/vagrant/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:151
/home/vagrant/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:103
Hope anyone can helpme!
I don't know if you ever figured this out, but I ran into a similar problem when trying to run phpunit tests under laravel 5.1. Apparently I had some out-of-date vendor builds on my global composer install (as reverenced by your error: /home/vagrant/.composer/vendor/illuminate/support/helpers.php:390)
All I did was cd to my composer directory:
cd /home/vagrant/.composer and ran composer update
This updated my global composer and everything worked as expected.

How to install mmoreramerino/GearmanBundle with Symfony 2.1.x?

I am pretty new to Symfony 2 and brand new to Gearman.
I am looking for a bundle to integrate Symfony 2 with Gearman.
mmoreramerino's bundle seems to be the most popular bundle according to packagist. Unfortunately something seems to be broken, the autoloader does not find the bundle.
Fatal error: Class 'Mmoreramerino\GearmanBundle\MmoreramerinoGearmanBundle' not found in ...
I tried switching to "dev-development" as I got from the issues that it was fixed in this branch, but it did not work for me as well.
Question: How can I install this bundle using Symfony 2.1.x?
Question 2: Are there any working & documented alternatives?
Edit In case someone else comes across this question: Here is how I got it up and running!
Install gearman, libgearman, the PECL extension for PHP (use recent versions!)
check that gearman shows up in phpinfo() (both cli and webserver version)
start gearmand in terminal 1 using "gearmand --verbose INFO" (you will see workers & clients connect to gearman - or not ;-))
start in terminal 2 reverse_worker.php from the gearman php extension example directory
start in terminal 3 reverse_client.php from the gearman php extension example directory
If this is working, you are ready for Symfony: install mmoreramerino/GearmanBundle using "dev-development"
copy dev.base.yml from the bundle to app/config/gearman/dev.yml
Now add TestWorker.php to your bundle as outlined in the documentation
enable the testWorker by using the console script "php app/console gearman:job:execute MmoreramerinoGearmanBundleWorkerstestWorker~test"
now you are able to send jobs to the listening testWorker in a Symfony controller (or somewhere else in Symfony). I had to specify the server though I am using the default host/port.
$gearman = $this->get('gearman');
$gearman->setServer('127.0.0.1',4730);
$gearman->doNormalJob('MmoreramerinoGearmanBundleWorkerstestWorker~test');
To install the bundle, you need to add the following line to composer.json
"Mmoreramerino/GearmanBundle": "dev-development"
and run composer update;
Then register it in app/AppKernel.php (it seems you have already done this)
new Mmoreramerino\GearmanBundle\MmoreramerinoGearmanBundle(),

Resources