How to make phpunit #expectedException work with hhvm? - phpunit

I have a few PHPUnit tests in my project, and some of them use the #expectedException feature, as shown below.
/**
* #expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException
**/
public function testExceptionThrownWhenErrorObjectReceived()
{
...
}
/**
* #expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException
**/
public function testExceptionThrownWhenOAuthErrorReceived()
{
...
}
/**
* #expectedException UnexpectedValueException
**/
public function testExceptionThrownWhenAskingForResourceOwner()
{
...
}
I run them with the help of travis under PHP 5.6, 7.0, 7.1 and 7.2 without problem, but HHVM fails :
There were 3 errors:
1) Mrjoops\OAuth2\Client\Test\Provider\JiraTest::testExceptionThrownWhenErrorObjectReceived
Mrjoops\OAuth2\Client\Provider\Exception\JiraIdentityProviderException: Validation Failed
2) Mrjoops\OAuth2\Client\Test\Provider\JiraTest::testExceptionThrownWhenOAuthErrorReceived
Mrjoops\OAuth2\Client\Provider\Exception\JiraIdentityProviderException: error_collection
3) Mrjoops\OAuth2\Client\Test\Provider\JiraTest::testExceptionThrownWhenAskingForResourceOwner
UnexpectedValueException: Invalid response received from Authorization Server. Expected JSON.
I use the latest PHPUnit 5.7 version (for PHP 5.6 compatibility) and the latest HHVM 3.29.1.
Travis wrote on their website about this :
Please note that if you want to run PHPUnit on HHVM, you have to
explicitly install version 5.7 in your .travis.yml due to a
compatibility issue between HHVM and PHP7
So I suppose I'm OK.
I know there is a known issue (https://github.com/sebastianbergmann/phpunit/issues/1640) which was unresolved and closed in PHPUnit, and an inconsistency documented in HHVM (https://github.com/hhvm/user-documentation/blob/master/guides/hhvm/06-inconsistencies/03-classes-and-objects.md), but it's not clear to me if a workaround exists.
Details available here :
Test file : https://github.com/mrjoops/oauth2-jira/blob/develop/test/src/Provider/JiraTest.php
Travis build : https://travis-ci.org/mrjoops/oauth2-jira/jobs/452936060
Thank you in advance for your help.

You can try to use:
$this->expectException(UnexpectedValueException::class);
but I guess that will raise the same problem. Or you try to use a workaround:
try {
doSomething();
} catch (Exception $ex) {
$this->assertInstanceOf(UnexpectedValueException::class, $ex);
}
$this->fail('Exception did not occur');
But I would raise the question if HHVM is something you really want/have to support? Support for it was dropped by many major frameworks and applications (composer, symfony and others):
https://github.com/facebook/hhvm/issues/7198

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.

Sudden syntax error after deployment

I have a working symfony project. I have it on a private bitbucked repository and locally the website works without an issue.
Today I tried to deploy the project onto an external server linuxpl.com.
Steps taken include:
Istalling composer
Adding the mysql database
Running git clone to get the data into a proper location
Running composer install on the folder to install everything and connect to the db
Cleared the cache
Set the project root as ....domain/project_name/web
However after completing all these steps, when running the website with regular server:run I'm getting this odd error:
Parse error: syntax error, unexpected '.' in /home/spirifer/domains/surowcewobiektywie.pl/konkurs/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1571
Not sure if this is of any importance but the mentioned code partion looks like this in my local files:
// Some objects throw exceptions when they have __call, and the method we try
// to call is not supported. If ignoreStrictCheck is true, we should return null.
try {
$ret = $object->$method(...$arguments);
} catch (BadMethodCallException $e) {
if ($call && ($ignoreStrictCheck || !$env->isStrictVariables())) {
return;
}
throw $e;
}
The local version does not differ from the one on the server.
My local machine has PHP 7.0.9 and the remove server has PHP 7.0.14
How could I fix this issue?
PHP 5.6 adds Variadic functions, with "...". However, Twig v1.x only required the use of PHP 5.2.7 or above.
If you didn't explicitly update to Twig 2.0, it's very possible you have used the 'death star' version constraint in the composer file - '*'. which allows uncontrolled version updates to the latest version. If this is the case, you will need to either update your version of PHP, or at least require just a previous version of Twig/twig, "^1.32" would be the latest in the version 1 series of Twig.

Error in ResponseExeption while populating FOSElasticaBundle/Symfony2

I am trying to get FOSElasticaBundle to work.
ElasticSearch Instance is running on localhost:9200 and responding.
I followed each step in the docs https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/Resources/doc/setup.md
but at the last step, I get this error in my console:
c:\xampp\htdocs\my\folder>php app/console fos:elastica:populate
Resetting app
Fatal error: Wrong parameters for Exception([string $exception [, long $code [,
Exception $previous = NULL]]]) in C:\xampp\htdocs\my\folder\vendor\rufli
n\elastica\lib\Elastica\Exception\ResponseException.php on line 34
[Symfony\Component\Debug\Exception\FatalErrorException]
Error: Wrong parameters for Exception([string $exception [, long $code [, Exception $previous = NULL]]])
fos:elastica:populate [--index[="..."]] [--type[="..."]] [--no-reset] [--offset="..."] [--sleep="..."] [--batch-size="..."] [--ignore-errors] [--no-overwrite-format]
It seems like there are 3 parameters mandatory for "__construct"-Function, but there are only 2 of them. I've simply tried to add "NULL"-parameter to get it work, but then another function throws an error.
public function __construct(Request $request, Response $response)
{
$this->_request = $request;
$this->_response = $response;
parent::__construct($response->getError());
}
Is this a common problem? How do I solve it?
This is because the ruflin/Elastica package is not compatible with elasticsearch 2.0 yet.
https://github.com/ruflin/Elastica/issues/946
An alternative for now (until ruflin/Elastica is 2.0 upgraded), is to use the latest 1.x version.
You can download it here: https://www.elastic.co/downloads/past-releases/elasticsearch-1-7-3
ES 1.7.3 + FosElasticaBundle (which uses ruflin/Elastica) works fine with Elasticsearch 1.7.3 version.
The reason for this issue is, that with elasticsearch 2.0 the structure of the response error changed (more details here: https://github.com/ruflin/Elastica/issues/946). Instead of a string before it is now a nested array. Elastica is currently not yet fully compatible with elasticsearch 2.0. As soon as a new release of Elastica is out which is compatible with Elasticsearch 2.0 it will probably mean that also foselastica bundle will have to be updated as these changes will break backward compatibility. Be also aware, that this is not the only backward compatibility breaking change.
To follow the progress of the upgrade, follow this issue: https://github.com/ruflin/Elastica/issues/946

ReactJS.NET - Bundles - TinyIoCResolutionException: Unable to resolve type: React.IReactEnvironment

I'm attempting to minify my .JSX files with ASP.NET Minification and Optimization via System.Web.Optimization.React. I've installed the MVC4 React Package as well as the Optimization package, but whenever I try to include a bundle I get the following:
React.TinyIoC.TinyIoCResolutionException: Unable to resolve type: React.IReactEnvironment
The InnerException is always null
My bundles are setup as follows:
bundles.Add(new ScriptBundle("~/Bundle/Scripts/ReactJS").Include(
"~/Scripts/React/react-0.12.2.js",
"~/Scripts/React/react-with-addons-0.12.2.js",
"~/Scripts/React/JSXTransformer-0.12.2.js"
));
bundles.Add(new JsxBundle("~/Bundle/Scripts/ReactCalendar").Include(
"~/Scripts/React/Calendar/Main.react.jsx",
"~/Scripts/React/Calendar/Components/Calendar.react.jsx",
"~/Scripts/React/Calendar/Components/CalendarEvent.react.jsx",
"~/Scripts/React/Calendar/Components/CalendarControls.react.jsx",
"~/Scripts/React/Calendar/Components/CalendarTimeSlots.react.jsx"
));
And included in the view as:
#section scripts{
#Scripts.Render("~/Bundle/Scripts/ReactJS");
#Scripts.Render("~/Bundle/Scripts/ReactCalendar");
}
The error is always thrown on line:
#Scripts.Render("~/Bundle/Scripts/ReactCalendar");
Anyone got any ideas on how to solve / debug this one? Let me know if more info is needed.
I'm not sure if this is the same issue I was facing, but I googled the exact same error, found this SO topic as the first hit, with no definitive answer, so I thought I'd offer my solution.
I'm using .NET 4.5 in an MVC app, and React.Web.Mvc4 v3.0.0.
I managed to work around this issue with the help of this comment on Github.
Here's my entire ReactConfig.cs:
using React;
using React.TinyIoC;
using React.Web.TinyIoC;
namespace NS.Project
{
public static class ReactConfig
{
public static void Configure()
{
Initializer.Initialize(AsPerRequestSingleton);
ReactSiteConfiguration.Configuration
.SetLoadBabel(false)
.AddScriptWithoutTransform("~/React/dist/server.bundle.js");
}
private static TinyIoCContainer.RegisterOptions AsPerRequestSingleton(
TinyIoCContainer.RegisterOptions registerOptions)
{
return TinyIoCContainer.RegisterOptions.ToCustomLifetimeManager(
registerOptions,
new HttpContextLifetimeProvider(),
"per request singleton"
);
}
}
}
Then, I'm callingReactConfig.Configure explicitly from Application_Start.
"Unable to resolve type: React.IReactEnvironment" with no InnerException generally means ReactJS.NET is not initialising properly for some reason. In web apps, ReactJS.NET handles initialisation through the use of WebActivator. Make sure your project is referencing React.Web, React.Web.Mvc4 and WebActivatorEx, and all the corresponding .dll files are ending up in your app's bin directory.
Also, you do not need to (and should not) include JSXTransformer in your JavaScript bundles, as ReactJS.NET does all the JSX compilation server-side.
Something looks like changed from React.Web.MVc4 version 4.0.0. versions before didnt have that problem.
as stated here
Install the React.Web.Mvc4 package through NuGet. You will also need to install a JS engine to use (either V8 or ChakraCore are recommended). See the JSEngineSwitcher docs for more information.
To use V8, add the following packages:
JavaScriptEngineSwitcher.V8
JavaScriptEngineSwitcher.V8.Native.win-x64
ReactConfig.cs will be automatically generated for you. Update it to register a JS engine and your JSX files:
using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.V8;
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(React.Sample.Mvc4.ReactConfig), "Configure")]
namespace React.Sample.Mvc4
{
public static class ReactConfig
{
public static void Configure()
{
ReactSiteConfiguration.Configuration
.AddScript("~/Content/Sample.jsx");
JsEngineSwitcher.Current.DefaultEngineName = V8JsEngine.EngineName;
JsEngineSwitcher.Current.EngineFactories.AddV8();
}
}
}
If anyone needs this, just install this nuget and it will resolve this issue.
System.Web.Optimization.React

I am trying to unit test a Silex application but Silex\WebTestCase namespace cannot be found

I have phpunit 3.6.12 installed as well as Silex. In the root directory of my app I have tests directory which contains the trivial test file BlogFunctionTest.php
<?
use Silex\WebTestCase;
// BLOG: Front end test
class BlogFunctionalTest extends Silex\WebTestCase
{
public function testIndex()
{
$this->assertGreaterThan(0, 1);
}
}
?>
when I run phpunit from the command line I get the error
PHP Fatal error: Class 'Silex\WebTestCase' not found in {path}/BlogFunctionTest.php line 7
which refers to the line where I try to extend WebTestCase. The same happens if I replace
use Silex\WebTestCase;
with
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
Silex is installed in relative to my test file at
../vendor/silex/
Any tips are greatly appreciated, thank you!
you have to define a bootstrap inside phpunit.xml.dist which point to the bootstrap of your silex application like it's done here https://github.com/joshuamorse/Silex-Boilerplate/blob/master/phpunit.xml.dist

Resources