phpunit error with app helper in laravel 4 - unit-testing

I try to start using phpunit on my laravel project.
I call "phpunit" and got this error:
$ phpunit
PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from /home/bee/www/postaler/phpunit.xml
EPHP Fatal error: Cannot redeclare Helper\isMenuActive() (previously declared in /home/bee/www/postaler/app/helpers.php:4) in /home/bee/www/postaler/app/helpers.php on line 6
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:130
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:192
PHP 5. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/TextUI/TestRunner.php:325
PHP 6. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:705
PHP 7. PHPUnit_Framework_TestSuite->runTest() /usr/share/php/PHPUnit/Framework/TestSuite.php:745
PHP 8. PHPUnit_Framework_TestCase->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:772
PHP 9. PHPUnit_Framework_TestResult->run() /usr/share/php/PHPUnit/Framework/TestCase.php:751
PHP 10. PHPUnit_Framework_TestCase->runBare() /usr/share/php/PHPUnit/Framework/TestResult.php:649
PHP 11. Illuminate\Foundation\Testing\TestCase->setUp() /usr/share/php/PHPUnit/Framework/TestCase.php:801
PHP 12. Illuminate\Foundation\Testing\TestCase->refreshApplication() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:31
PHP 13. Illuminate\Foundation\Application->boot() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:48
PHP 14. Illuminate\Foundation\Application->bootApplication() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:552
PHP 15. Illuminate\Foundation\Application->fireAppCallbacks() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:569
PHP 16. call_user_func:{/home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:792}() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:792
PHP 17. TestCase->{closure:/home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/start.php:223-271}() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:792
PHP 18. require() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/start.php:239
my helper code
<?php namespace Helper;
//app/helpers.php
function isMenuActive($url_segment) {
if (\Request::segment(1) == $url_segment) { return " active"; }
}
I included the helper file in app/start/global.php
require app_path().'/helpers.php'
my test script
<?php
// app/tests/FromTest.php
class FromTest extends TestCase {
public function testFrom()
{
$response = $this->call('GET', 'user/profile');
$this->assertTrue(true);
}
}

app/start/global.php is executed once for each request and should not be used for class/function/constant definitions. Instead, add a files autoload rule to your composer.json.
"files": ["src/functions.php"],

Related

phpseclib wordpress integration not working

i am trying to run phpseclib with Wordpress but its not logging in. the same code runs on PHP Designer 8 which has PHP v 5.* but on word press with PHP 7.3* and PHP 7.4 its not running. I got 2 error in Eventviewer
sshd: Bad packet length 980908999. [preauth]
sshd: ssh_dispatch_run_fatal: Connection from 127.0.0.1 port 15412: message authentication code incorrect [preauth]
SFTP server is Openssh
it gives 2 warnings on the webpage as follows. Can someone guide please. thanks.
Warning: unpack(): Type C: not enough input, need 1, have 0 in C:\Program Files (x86)\xampp\htdocs\testing\wp-content\plugins\sftp\phpseclib\Net\SSH2.php on line 1345
Warning: extract() expects parameter 1 to be array, bool given in C:\Program Files (x86)\xampp\htdocs\testing\wp-content\plugins\sftp\phpseclib\Net\SSH2.php on line 1345
$sftp = new Net_SFTP('127.0.0.1',22); $sftp->getLog(); if
(!$sftp->login('myuser', 'pass')) { //if you can't log on...
$sftp->getLog(); echo $sftp->getErrors();
print_r($sftp->getSFTPErrors()); echo $sftp->getSFTPLog();
exit('sftp Login Failed'); } echo $sftp->pwd();

Symfony2 unit tests produces Fatal Error: Declaration of ContainerAwareEventManager::removeEventListener() must be compatible with

When calling phpunit on a very simple standard test, it produces the following output and error:
Command (in symfony2 root):
phpunit -c build/ src
Output:
PHPUnit 4.1.6-6-g43914fa by Sebastian Bergmann.
Configuration read from /srv/xxxxxxxxx/build/phpunit.xml
PHP Fatal error: Declaration of Symfony\Bridge\Doctrine\ContainerAwareEventManager::removeEventListener() must be compatible with Doctrine\Common\EventManager::removeEventListener($events, $listener = NULL) in /srv/xxxxxxxxx/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/ContainerAwareEventManager.php on line 141
PHP Stack trace:
PHP 1. {main}() /usr/local/composer/vendor/phpunit/phpunit/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/local/composer/vendor/phpunit/phpunit/phpunit:57
PHP 3. PHPUnit_TextUI_Command->run() /usr/local/composer/vendor/phpunit/phpunit/src/TextUI/Command.php:132
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/local/composer/vendor/phpunit/phpunit/src/TextUI/Command.php:179
PHP 5. PHPUnit_Framework_TestSuite->run() /usr/local/composer/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:426
PHP 6. PHPUnit_Framework_TestSuite->run() /usr/local/composer/vendor/phpunit/phpunit/src/Framework/TestSuite.php:699
PHP 7. PHPUnit_Framework_TestCase->run() /usr/local/composer/vendor/phpunit/phpunit/src/Framework/TestSuite.php:699
PHP 8. PHPUnit_Framework_TestResult->run() /usr/local/composer/vendor/phpunit/phpunit/src/Framework/TestCase.php:760
PHP 9. PHPUnit_Framework_TestCase->runBare() /usr/local/composer/vendor/phpunit/phpunit/src/Framework/TestResult.php:686
PHP 10. PHPUnit_Framework_TestCase->runTest() /usr/local/composer/vendor/phpunit/phpunit/src/Framework/TestCase.php:826
PHP 11. ReflectionMethod->invokeArgs() /usr/local/composer/vendor/phpunit/phpunit/src/Framework/TestCase.php:962
PHP 12. Syw\Admin\CoreBundle\Tests\Controller\DebugControllerTest->testIndex() /usr/local/composer/vendor/phpunit/phpunit/src/Framework/TestCase.php:962
PHP 13. Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient() /srv/xxxxxxxxx/src/Syw/Admin/CoreBundle/Tests/Controller/DebugControllerTest.php:13
PHP 14. Symfony\Component\HttpKernel\Kernel->boot() /srv/xxxxxxxxx/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php:47
PHP 15. Symfony\Component\HttpKernel\Kernel->initializeContainer() /srv/xxxxxxxxx/app/bootstrap.php.cache:2300
PHP 16. Symfony\Component\DependencyInjection\ContainerBuilder->compile() /srv/xxxxxxxxx/app/bootstrap.php.cache:2521
PHP 17. Symfony\Component\DependencyInjection\Compiler\Compiler->compile() /srv/xxxxxxxxx/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/ContainerBuilder.php:619
PHP 18. JMS\AopBundle\DependencyInjection\Compiler\PointcutMatchingPass->process() /srv/xxxxxxxxx/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/Compiler.php:117
PHP 19. JMS\AopBundle\DependencyInjection\Compiler\PointcutMatchingPass->processInlineDefinitions() /srv/xxxxxxxxx/vendor/jms/aop-bundle/JMS/AopBundle/DependencyInjection/Compiler/PointcutMatchingPass.php:66
PHP 20. JMS\AopBundle\DependencyInjection\Compiler\PointcutMatchingPass->processDefinition() /srv/xxxxxxxxx/vendor/jms/aop-bundle/JMS/AopBundle/DependencyInjection/Compiler/PointcutMatchingPass.php:85
PHP 21. class_exists() /srv/xxxxxxxxx/vendor/jms/aop-bundle/JMS/AopBundle/DependencyInjection/Compiler/PointcutMatchingPass.php:110
PHP 22. spl_autoload_call() /srv/xxxxxxxxx/vendor/jms/aop-bundle/JMS/AopBundle/DependencyInjection/Compiler/PointcutMatchingPass.php:110
PHP 23. Composer\Autoload\ClassLoader->loadClass() /srv/xxxxxxxxx/vendor/jms/aop-bundle/JMS/AopBundle/DependencyInjection/Compiler/PointcutMatchingPass.php:0
PHP 24. Composer\Autoload\includeFile() /usr/local/composer/vendor/composer/ClassLoader.php:301
This is the phpunit.xml in [symfony-root]/build:
<?xml version="1.0" encoding="UTF-8"?>
<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "../app/bootstrap.php.cache" >
<php>
<server name="KERNEL_DIR" value="app/" />
</php>
<testsuites>
<testsuite name="Onlinexxxxxxxxx Test Suite">
<directory>src/*/*Bundle/Tests</directory>
<directory>src/*/*/*Bundle/Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
<exclude>
<directory>src/*/*Bundle/Resources</directory>
<directory>src/*/*Bundle/Tests</directory>
<directory>src/*/*/*Bundle/Resources</directory>
<directory>src/*/*/*Bundle/Tests</directory>
<directory>src/*/Bundle/*Bundle/Resources</directory>
<directory>src/*/Bundle/*Bundle/Tests</directory>
</exclude>
</whitelist>
<blacklist>
<directory>src/*/*Bundle/Resources</directory>
<directory>src/*/*Bundle/Tests</directory>
<directory>src/*/*/*Bundle/Resources</directory>
<directory>src/*/*/*Bundle/Tests</directory>
<directory>src/*/Bundle/*Bundle/Resources</directory>
<directory>src/*/Bundle/*Bundle/Tests</directory>
</blacklist>
</filter>
<logging>
<log type="coverage-html" target="../build/coverage" title="GMS" charset="UTF-8" yui="true" highlight="true"
lowUpperBound="35" highLowerBound="70"/>
<log type="coverage-clover" target="../build/logs/clover.xml"/>
<log type="junit" target="../build/logs/junit.xml" logIncompleteSkipped="false"/>
</logging>
</phpunit>
And this is the very simple Test class:
<?php
namespace Syw\Admin\CoreBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class DebugControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$crawler = $client->request('GET', '/debug');
}
public function testDetail()
{
$client = static::createClient();
$crawler = $client->request('GET', '/detail');
}
}
Any help with this would be highly aprecciated.
By the way, the
Symfony\Bridge\Doctrine\ContainerAwareEventManager::removeEventListener()
and the
Doctrine\Common\EventManager::removeEventListener($events, $listener = NULL)
are of course still the original ones from the bundles/Symfony2.
Generally nothing in symfony2 or the corebundles were changed, just only our own bundle.
By the way (#2): We are using Symfony 2.4 (but I've just also tried to upgrade to Symfony 2.6 which didn't solve this problem)
Edit:
Like mentioned in the comments below, I've also tried the following things:
Upgrade Symfony from 2.4 to 2.6 -> no success, same error
Downgrade some doctrine bundles -> no success, same error
The composer.json actually looks this way (for the requirements):
"require": {
"ali/datatable": "dev-master",
"doctrine/data-fixtures": "dev-master",
"doctrine/doctrine-bundle": "~1.2",
"doctrine/doctrine-fixtures-bundle": "dev-master",
"doctrine/doctrine-migrations-bundle": "2.1.*#dev",
"doctrine/migrations": "1.0.*#dev",
"doctrine/orm": "~2.2,>=2.2.3",
"dropbox/dropbox-sdk": "1.1.*",
"friendsofsymfony/rest-bundle": "#dev",
"gedmo/doctrine-extensions": "dev-master",
"google-api-php-client": "0.6.7",
"hybridauth/hybridauth": "3.0.0.*#dev",
"incenteev/composer-parameter-handler": "~2.0",
"jms/i18n-routing-bundle": "dev-master",
"jms/security-extra-bundle": "dev-master",
"jms/serializer-bundle": "#dev",
"knplabs/knp-paginator-bundle": "dev-master",
"languagedetect": "1.0",
"languagetool": "2.8",
"lexik/maintenance-bundle": "dev-master",
"misd/guzzle-bundle": "~1.0",
"nelmio/api-doc-bundle": "#dev",
"nelmio/cors-bundle": "~1.0",
"php": ">=5.3.3",
"sensio/distribution-bundle": "2.3.*",
"sensio/framework-extra-bundle": "~3.0",
"sensio/generator-bundle": "~2.3",
"stfalcon/tinymce-bundle": "dev-master",
"stof/doctrine-extensions-bundle": "~1.1#dev",
"symfony/assetic-bundle": "2.3.*",
"symfony/monolog-bundle": "2.3.*",
"symfony/swiftmailer-bundle": "2.3.*",
"symfony/symfony": "2.4.*",
"twig/extensions": "1.0.*"
},
Okay... after many, many hours of testing and trying with downgrades, upgrades and all these things, I've now found out, that the problem simply is phpunit itself.
I had "PHPUnit 4.1.6-6-g43914fa" installed, as you can see in my output above.
I've now downgraded to 3.7.13 ("phpunit/phpunit": "~3.6") and this is working without any problems.

Disable DB write in testing controller - injecting doctrine.orm.default_entity_manager throws "Fatal error" when activating FOSUser and HWIOAuth

I want to disable the DB writing when functional testing a controller in phpunit.
In a project in Symfony2, I use phpunit to test a POST call to an API endpoint that saves to database. This worked and I now have a regression problem when activating FOSUser + HWIOAuth.
To avoid the phpunit writing dummy data to the database, I use this trick here: Testing Controllers in Symfony2 with Doctrine to mock the entity manager, then inject the mocked service into the container of the testing system.
This way when you run the test, the DB endpoint is mocked and it is only tested that a flush() is called thanks to the $entityManagerMock->expects($this->once())->method('flush'); that you can see in that question and in the code below.
This used to work.
This test gave green bar, POST calls were tested and no data was saved to the database.
public function testPostCreatesIssue()
{
$client = static::createClient();
$entityManagerMock = $this->getMockBuilder( 'Doctrine\ORM\EntityManager' )
->setMethods( array( 'persist', 'flush' ) )
->disableOriginalConstructor()
->getMock();
$entityManagerMock->expects( $this->once() )
->method( 'flush' );
$client->getContainer()->set( 'doctrine.orm.default_entity_manager', $entityManagerMock );
$postData = array
(
'title' => 'Issues controller test, post creates issue',
'body' => 'The test tests that a post call' . PHP_EOL . 'creates a new issue' . PHP_EOL . 'in the database.' . PHP_EOL . 'UTF8: áéíóú àèìòù ñç',
'pageUrl' => 'file://Xmontero/AntiqueCrayon/MainBundle/Tests/Controller/IssuesControllerTest.php'
);
$crawler = $client->request( 'POST', '/issues/', $postData );
$response = $client->getResponse();
$this->assertEquals( Response::HTTP_CREATED, $response->getStatusCode() );
$this->assertEquals( 'application/json', $response->headers->get( 'content-type' ) );
}
At that moment, I only had the default bundles + my own bundles in the AppKernel.php
The problem
When I activate those bundles in the AppKernel.php
new FOS\UserBundle\FOSUserBundle(),
new HWI\Bundle\OAuthBundle\HWIOAuthBundle(),
and after configuration, everything works properly testing manually in my browser, including the ajax call that does the POST tested above. That controller has nothing to do with the FOSUser and HWIOAuth as that controller worked before I activated those bundles in the kernel.
But despite the manual test did work, the automated test started to fail giving Call to a member function getRepository() on a non-object not in a line of my code but in the very internals of the FOS and the Doctrine - here's the full output:
xavi#bromo:/files/custom_www/antique-crayon/preproduction$ phpunit -c app --filter Post src/Xmontero/AntiqueCrayon/MainBundle/Tests/Controller/IssuesControllerTest.php
PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from /files/custom_www/antique-crayon/preproduction/app/phpunit.xml.dist
PHP Fatal error: Call to a member function getRepository() on a non-object in /files/custom_www/antique-crayon/preproduction/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php on line 759
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:130
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:192
PHP 5. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/TextUI/TestRunner.php:325
PHP 6. PHPUnit_Framework_TestSuite->runTest() /usr/share/php/PHPUnit/Framework/TestSuite.php:745
PHP 7. PHPUnit_Framework_TestCase->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:772
PHP 8. PHPUnit_Framework_TestResult->run() /usr/share/php/PHPUnit/Framework/TestCase.php:751
PHP 9. PHPUnit_Framework_TestCase->runBare() /usr/share/php/PHPUnit/Framework/TestResult.php:649
PHP 10. PHPUnit_Framework_TestCase->runTest() /usr/share/php/PHPUnit/Framework/TestCase.php:804
PHP 11. ReflectionMethod->invokeArgs() /usr/share/php/PHPUnit/Framework/TestCase.php:942
PHP 12. Xmontero\AntiqueCrayon\MainBundle\Tests\Controller\IssuesControllerTest->testPostCreatesIssue() /files/custom_www/antique-crayon/preproduction/src/Xmontero/AntiqueCrayon/MainBundle/Tests/Controller/IssuesControllerTest.php:0
PHP 13. Symfony\Component\BrowserKit\Client->request() /files/custom_www/antique-crayon/preproduction/src/Xmontero/AntiqueCrayon/MainBundle/Tests/Controller/IssuesControllerTest.php:41
PHP 14. Symfony\Bundle\FrameworkBundle\Client->doRequest() /files/custom_www/antique-crayon/preproduction/vendor/symfony/symfony/src/Symfony/Component/BrowserKit/Client.php:327
PHP 15. Symfony\Component\HttpKernel\Client->doRequest() /files/custom_www/antique-crayon/preproduction/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php:111
PHP 16. Symfony\Component\HttpKernel\Kernel->handle() /files/custom_www/antique-crayon/preproduction/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Client.php:81
PHP 17. Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2330
PHP 18. Symfony\Component\HttpKernel\HttpKernel->handle() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:3080
PHP 19. Symfony\Component\HttpKernel\HttpKernel->handleRaw() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2931
PHP 20. Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2958
PHP 21. Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->preProcess() /files/custom_www/antique-crayon/preproduction/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php:107
PHP 22. Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->getListeners() /files/custom_www/antique-crayon/preproduction/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php:215
PHP 23. Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->lazyLoad() /files/custom_www/antique-crayon/preproduction/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php:128
PHP 24. Symfony\Component\DependencyInjection\Container->get() /files/custom_www/antique-crayon/preproduction/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php:188
PHP 25. appTestDebugProjectContainer->getProfilerListenerService() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2056
PHP 26. Symfony\Component\DependencyInjection\Container->get() /files/custom_www/antique-crayon/preproduction/app/cache/test/appTestDebugProjectContainer.php:2251
PHP 27. appTestDebugProjectContainer->getProfilerService() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2056
PHP 28. Symfony\Component\DependencyInjection\Container->get() /files/custom_www/antique-crayon/preproduction/app/cache/test/appTestDebugProjectContainer.php:2234
PHP 29. appTestDebugProjectContainer->getSecurity_ContextService() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2056
PHP 30. Symfony\Component\DependencyInjection\Container->get() /files/custom_www/antique-crayon/preproduction/app/cache/test/appTestDebugProjectContainer.php:2374
PHP 31. appTestDebugProjectContainer->getSecurity_Authentication_ManagerService() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2056
PHP 32. Symfony\Component\DependencyInjection\Container->get() /files/custom_www/antique-crayon/preproduction/app/cache/test/appTestDebugProjectContainer.php:3941
PHP 33. appTestDebugProjectContainer->getFosUser_UserProvider_UsernameEmailService() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2056
PHP 34. Symfony\Component\DependencyInjection\Container->get() /files/custom_www/antique-crayon/preproduction/app/cache/test/appTestDebugProjectContainer.php:3885
PHP 35. appTestDebugProjectContainer->getFosUser_UserManagerService() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2056
PHP 36. FOS\UserBundle\Doctrine\UserManager->__construct() /files/custom_www/antique-crayon/preproduction/app/cache/test/appTestDebugProjectContainer.php:1657
PHP 37. Doctrine\ORM\EntityManager->getRepository() /files/custom_www/antique-crayon/preproduction/vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Doctrine/UserManager.php:40
xavi#bromo:/files/custom_www/antique-crayon/preproduction$
Narrowing down the problem
The problem is the service doctrine.orm.default_entity_manager for which I inject a mock during the test phase, this is why the manual test in the browser does not fail: No mocks, there.
If I edit my test to do not use any mock, just write to the real DB, it then works:
To test without mock I just comment out the ->set() in the container. And to avoid an Method was expected to be called 1 times, actually called 0 times. I also comment out the ->expects():
//$entityManagerMock->expects( $this->once() )
// ->method( 'flush' );
//
//$client->getContainer()->set( 'doctrine.orm.default_entity_manager', $entityManagerMock );
then the tests greenbars:
xavi#bromo:/files/custom_www/antique-crayon/preproduction$ phpunit -c app --filter Post src/Xmontero/AntiqueCrayon/MainBundle/Tests/Controller/IssuesControllerTest.php
PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from /files/custom_www/antique-crayon/preproduction/app/phpunit.xml.dist
.
Time: 0 seconds, Memory: 32.50Mb
OK (1 test, 2 assertions)
xavi#bromo:/files/custom_www/antique-crayon/preproduction$
Conclusions
Injecting a mock in doctrine.orm.default_entity_manager pre-activating FOSUser and HWIOAuth, works fine
When activating these bundles in the AppKernel the trick does not work.
I then only can test allowing to write real data to a database, which is not desired when testing.
Question
How can I disable the writing to the DataBase when calling a controller from a test in phpunit, when FOSUserBundle and HWIOAuthBundle are activated in the kernel?
Thanks!
Xavi.
I'm not proud about it but I haven't done so many test with phpUnit to answer your question with a 100% of security on the answer, but here is my opinion.
I think that the solution is to mock the entityManager, as you are doing yet, but you are moking only the persist() and flush() method.
->setMethods( array( 'persist', 'flush' ) )
->disableOriginalConstructor()
Without the code you are trying to test I can't know it for sure, but it seems that in any part of your controller you're using de FOSUserBundle or the HWIOAuthBundle and one or both of them is using at least the getRepository() method and you need to mock at least all the methods that can be used via FOSUserBundle and HWIOAuthBundle.
If you don't indicate the methods with the setMethods() all of them are mocked, and you can still have controll with the number of times a method is called or not as in the code example below.
$mockObjectManager = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->getMock();
$mockObjectManager->expects($this->any())
->method('persist');
$mockObjectManager->expects($this->atLeastOnce())
->method('flush');
If the FOSUserBundle is trying to get the repository, as it seems, you will have to mock the return of that call, if you don't do that it will probably end with an error at the next call, an example:
$this->em->expects($this->any())
->method('getRepository')
->with('FOSUserBundle:XXXXX')
->will($this->returnValue($this->getMockedClassWithFind('\Doctrine\ORM\EntityRepository')));
I think the best solution is to start resolving this first error, you will see another error because the object the FosUserBundle is expecting will be null, and mock that one, and maybe expects another object or maybe you're done.
Hope is helpful.

Mage_Selenium_Driver::doCommand() should be compatible with that of PHPUnit_Extensions_SeleniumTestCase_Driver

I am trying to configure Magento test automation framework on my system.
When I run phpunit in command line, I am getting following error. Same error I am getting while running test in the netbeans.
Strict Standards: Declaration of Mage_Selenium_Driver::doCommand() should
be compatible with that of PHPUnit_Extensions_SeleniumTestCase_Driver::doCommand()
in C:\MTAF\taf\lib\Mage\Selenium\Driver.php on line 38
..
Fatal error: Call to undefined method PHPUnit_Framework_TestSuite::isPublicTestMethod() in C:\MTAF\taf\lib\Mage\Selenium\TestCase.php on line 2502
Can some one please suggest some solution for the same.
I changed the code, this is the change I performed:
--- a/framework/Mage/Selenium/TestCase.php
+++ b/framework/Mage/Selenium/TestCase.php
## -409,7 +409,7 ## class Mage_Selenium_TestCase extends PHPUnit_Extensions_SeleniumTestCase
$testMethods = array();
$class = new ReflectionClass(self::$_testClass);
foreach ($class->getMethods() as $method) {
- if (PHPUnit_Framework_TestSuite::isPublicTestMethod($method)) {
+ if ($method->isPublic()) {
$testMethods[] = $method->getName();
}
}
I was getting this error when running phpunit >= 4.0. Downgrading to 3.7.x solved it for me.

Class 'Mockery' not found

I use laravel (4.1) framework and i read "Laravel-testing-decoded", it's a ebook by Jeffrey Wey.
I want to test my modal User and my method setPasswordAttribute($password)
My unit-testing :
<?php
class UserTest extends TestCase {
public function testHashesPasswordWhenSet(){
Hash::shouldReceive('make')->once()->andReturn('hashed');
$user = new User;
$user->password = 'food';
$this->assertEquals('hashed', $user->password);
}
}
But when i launch CLI : phpunit it return me a error : Fatal error: Class 'Mockery' not found
In complete error :
Fatal error: Class 'Mockery' not found in /Applications/MAMP/htdocs/ptf/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 84
Call Stack:
0.0021 236384 1. {main}() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/composer/bin/phpunit:0
0.0294 1425104 2. PHPUnit_TextUI_Command::main() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/composer/bin/phpunit:63
0.0294 1425336 3. PHPUnit_TextUI_Command->run() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:129
0.0692 3626416 4. PHPUnit_TextUI_TestRunner->doRun() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:176
0.0741 3944720 5. PHPUnit_Framework_TestSuite->run() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/TextUI/TestRunner.php:349
0.0741 3946368 6. PHPUnit_Framework_TestSuite->run() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:705
0.0742 3946968 7. PHPUnit_Framework_TestSuite->runTest() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:745
0.0742 3947000 8. PHPUnit_Framework_TestCase->run() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:775
0.0743 3948232 9. PHPUnit_Framework_TestResult->run() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:783
0.0754 4005504 10. PHPUnit_Framework_TestCase->runBare() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php:648
0.2926 15417592 11. PHPUnit_Framework_TestCase->runTest() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:838
0.2926 15418872 12. ReflectionMethod->invokeArgs() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:983
0.2926 15418904 13. UserTest->testHashesPasswordWhenSet() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:983
0.2928 15426728 14. Illuminate\Support\Facades\Facade::shouldReceive() /Applications/MAMP/htdocs/ptf/app/tests/models/UserTest.php:7
0.2928 15426944 15. Illuminate\Support\Facades\Facade::createFreshMockInstance() /Applications/MAMP/htdocs/ptf/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:50
0.2928 15427040 16. Illuminate\Support\Facades\Facade::createMockByName() /Applications/MAMP/htdocs/ptf/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:64
I don't understand, why i have this error.
Do you have Mockery installed?
If not, update your composer.json:
"require-dev": {
"mockery/mockery": "dev-master#dev"
}
Then run:
composer update