The class Mock_* was not found in the chain configured namespaces App\Entity [Symfony 5.3][PHPUnit 8.5] - symfony

I am trying to write PHPUnit tests for my Symfony 5.3 project with some Repositories mocked with others real.
$ bin/console -v
Symfony 5.3.10 (env: dev, debug: true)
$ bin/phpunit -V
PHPUnit 8.5.19 by Sebastian Bergmann and contributors.
Doctrine\Persistence\Mapping\MappingException: The class 'Mock_SType_b1b7aee4' was not found in the chain configured namespaces App\Entity
/var/www/cir/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php:23
/var/www/cir/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/Driver/MappingDriverChain.php:91
/var/www/cir/vendor/doctrine/doctrine-bundle/Mapping/MappingDriver.php:45
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:156
/var/www/cir/vendor/doctrine/doctrine-bundle/Mapping/ClassMetadataFactory.php:19
/var/www/cir/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:382
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:85
/var/www/cir/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:251
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:293
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1789
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1764
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:629
/var/www/cir/src/Component/User/UserManager.php:1059
/var/www/cir/src/Component/User/UserManager.php:383
/var/www/cir/tests/Component/User/Manager/Creation/ATCPTest.php:146
I have created a KernelTestCase where:
$sTypeRepository = $this->createStub(STypeRepository::class);
So I tried to use the actual Repository:
$sTypeRepository = static::getContainer()->get('doctrine')->getRepository(SType::class);
and I get error:
TypeError: Argument 2 passed to App\Component\User\UserManager::__construct() must be an instance of App\Repository\STypeRepository, instance of Doctrine\ORM\EntityRepository given, called in /var/www/cir/tests/Component/User/Manager/Creation/ATCPTest.php on line 122
/var/www/cir/src/Component/User/UserManager.php:214
/var/www/cir/tests/Component/User/Manager/Creation/ATCPTest.php:122
Other tests run fine for other mocked classes, like here:
$this->SUserRoleRepository = $this->createStub(SUserRoleRepository::class);
So, why do I get a MappingException or TypeError, respectively, for some repositories but not others? Perhaps help me to understand the errors better? Thanks!

The issue was that my SType entity did not have an accurate #ORM\Entity(...) annotation. I had:
/**
* SType.
*
* #ORM\Table(name="s_type", indexes={#ORM\Index(name="idx_s_type_1", columns={"type_group"}), #ORM\Index(name="idx_s_type_2", columns={"code"})})
* #ORM\Entity
*/
class SType {}
Needed the repositoryClass relation:
/**
* SType.
*
* #ORM\Table(name="s_type", indexes={#ORM\Index(name="idx_s_type_1", columns={"type_group"}), #ORM\Index(name="idx_s_type_2", columns={"code"})})
* #ORM\Entity(repositoryClass="App\Repository\STypeRepository")
*/
class SType {}

Related

r8 does not create output dex file

I'm trying to use r8 without much success:
$ wget https://repo1.maven.org/maven2/com/mikepenz/fastadapter/3.2.7/fastadapter-3.2.7.aar
$ wget https://maven.google.com/com/android/support/recyclerview-v7/26.0.1/recyclerview-v7-26.0.1.aar
$ unzip fastadapter-3.2.7.aar && mv classes.jar fastadapter.jar
$ unzip recyclerview-v7-26.0.1.aar && mv classes.jar recycleview.jar
$ echo "-dontwarn java.lang.Object" > proguard.cfg
$ java -jar build/libs/r8.jar --lib android-4.1.1.4.jar --lib recycleview.jar --release --output . --pg-conf proguard.cfg fastadapter.jar
$ dexdump -d classes.dex | grep "insns size" | wc -l
Unable to open 'classes.dex' : No such file or directory
0
But with d8 it works perfect (even without --lib android.jar and --lib recycleview.jar):
$ java -jar ../build/libs/d8.jar --release --output . fastadapter.jar
$ dexdump -d classes.dex | grep "insns size" | wc -l
390
EDIT
Trying to create the fastadapter proguard keep rules failed with:
$ cmdline-tools/build-tools/30.0.3/aapt2 link --proguard proguard_fastadapter.cfg -o proguard_fastadapter.cfg --manifest AndroidManifest.xml
AndroidManifest.xml:2: error: attribute android:versionCode not found.
AndroidManifest.xml:2: error: attribute android:versionName not found.
AndroidManifest.xml:7: error: attribute android:minSdkVersion not found.
AndroidManifest.xml:7: error: attribute android:targetSdkVersion not found.
error: failed processing manifest.
which is a bit odd because targetSdkVersion in fact does exist:
$ grep "targetSdkVersion" AndroidManifest.xml
android:targetSdkVersion="27" />
The problem is that the only rule you are passing to R8 is -dontwarn java.lang.Object. There is no single -keep rule, which means that the output will be empty as no entry points are kept.
What is missing is two set of keep rules:
General keep rules for all Android apps
Specific rules for the concrete app with all entry points (reflective access from the platform for e.g. view inflation), which can be generated with aapt2.
For the first set of rules Android Studio (or more precisely AGP) bundles this rules file, which is passed to all builds as a result of getDefaultProguardFile('proguard-android-optimize.txt').
# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
#
# Starting with version 2.2 of the Android plugin for Gradle, this file is distributed together with
# the plugin and unpacked at build-time. The files in $ANDROID_HOME are no longer maintained and
# will be ignored by new version of the Android plugin for Gradle.
# Optimizations: If you don't want to optimize, use the proguard-android.txt configuration file
# instead of this one, which turns off the optimization flags.
# Adding optimization introduces certain risks, since for example not all optimizations performed by
# ProGuard works on all versions of Dalvik. The following flags turn off various optimizations
# known to have issues, but the list may not be complete or up to date. (The "arithmetic"
# optimization can be used if you are only targeting Android 2.0 or later.) Make sure you test
# thoroughly if you go this route.
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
-optimizationpasses 5
-allowaccessmodification
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose
# Preserve some attributes that may be required for reflection.
-keepattributes *Annotation*,Signature,InnerClasses,EnclosingMethod
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
-keep public class com.google.android.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService
-dontnote com.google.vending.licensing.ILicensingService
-dontnote com.google.android.vending.licensing.ILicensingService
# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames,includedescriptorclasses class * {
native <methods>;
}
# Keep setters in Views so that animations can still work.
-keepclassmembers public class * extends android.view.View {
void set*(***);
*** get*();
}
# We want to keep methods in Activity that could be used in the XML attribute onClick.
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keepclassmembers class * implements android.os.Parcelable {
public static final ** CREATOR;
}
# Preserve annotated Javascript interface methods.
-keepclassmembers class * {
#android.webkit.JavascriptInterface <methods>;
}
# The support libraries contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version. We know about them, and they are safe.
-dontnote android.support.**
-dontnote androidx.**
-dontwarn android.support.**
-dontwarn androidx.**
# This class is deprecated, but remains for backward compatibility.
-dontwarn android.util.FloatMath
# Understand the #Keep support annotation.
-keep class android.support.annotation.Keep
-keep class androidx.annotation.Keep
-keep #android.support.annotation.Keep class * {*;}
-keep #androidx.annotation.Keep class * {*;}
-keepclasseswithmembers class * {
#android.support.annotation.Keep <methods>;
}
-keepclasseswithmembers class * {
#androidx.annotation.Keep <methods>;
}
-keepclasseswithmembers class * {
#android.support.annotation.Keep <fields>;
}
-keepclasseswithmembers class * {
#androidx.annotation.Keep <fields>;
}
-keepclasseswithmembers class * {
#android.support.annotation.Keep <init>(...);
}
-keepclasseswithmembers class * {
#androidx.annotation.Keep <init>(...);
}
# These classes are duplicated between android.jar and org.apache.http.legacy.jar.
-dontnote org.apache.http.**
-dontnote android.net.http.**
# These classes are duplicated between android.jar and core-lambda-stubs.jar.
-dontnote java.lang.invoke.**
For the second part use aapt2 link with the --proguard option. That will generate additional required rules like:
-keep class com.example.app.MainActivity { <init>(); }
Both sets of rules will have to be passed to R8 (it can take several --pg-conf options) together with additional application specific rules.
When using Android Studio (again more precisely AGP) this is handled automatically. For reference these rules can be inspected in app/build/intermediates/proguard-files. See Shrink, obfuscate, and optimize your app for more details.

Laravel 6 cannot run tests "PHPUnit\Runner\BaseTestRunner::getTest() must be of the type string"

I'm trying to write very simple phpunit test
php artisan make:test CarTest
class CarTest extends TestCase
{
/**
* A basic feature test example.
*
* #return void
*/
public function testExample()
{
$response = $this->get('/car');
$response->assertStatus(200);
}
}
Now I run it
phpunit ./tests/Feature/CarTest.php
I get
Fatal error: Uncaught TypeError: Argument 2 passed to PHPUnit\Runner\BaseTestRunner::getTest() must be of the type string, array given, called in C:\Users\yasse\AppData\Roaming\Composer\vendor\phpunit\phpunit\src\TextUI\Command.php on line 125 and defined in C:\xampp\htdocs\lara6\vendor\phpunit\phpunit\src\Runner\BaseTestRunner.php:60
Stack trace:
#0 C:\Users\{username}\AppData\Roaming\Composer\vendor\phpunit\phpunit\src\TextUI\Command.php(125): PHPUnit\Runner\BaseTestRunner->getTest('C:\\xampp\\htdocs...', Array)
#1 C:\Users\{username}\AppData\Roaming\Composer\vendor\phpunit\phpunit\src\TextUI\Command.php(101): PHPUnit\TextUI\Command->run(Array, true)
#2 C:\Users\{username}\AppData\Roaming\Composer\vendor\phpunit\phpunit\phpunit(61): PHPUnit\TextUI\Command::main()
#3 {main}
I installed phpunit globally and installed it locally (v8) using composer and run composer dump-autoload but issue is not resolved. How to fix this and be able to run this test?

Exclusion by annotation not working for sbt test/testOnly

Consider the following org.scalatest.TagAnnotation subclass:
public class TestSizeTags {
/** Tests with crazy long runtimes **/
#org.scalatest.TagAnnotation
#Inherited
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.METHOD, ElementType.TYPE})
public static #interface HugeTestClass {}
}
Let us annotate/tag a class with it:
#HugeTestClass
class ItemsJobTest extends FunSuite with BeforeAndAfterEach with DataFrameSuiteBase {
Now we want a quick "smoke test suite" on the codebase; therefore, let us (attempt) to exclude the testcases annotated by HugeTestClass :
Command line:
sbt test * -- -l HugeTestClass
OR maybe:
sbt 'testOnly * -- -l HugeTestClass'
Let us also attempt it within sbt itself:
sbt> testOnly * -- -l HugeTestClass
In all cases above we (unfortunately) still see:
[info] ItemsJobTest:
^C[info] - Run Items Pipeline *** FAILED *** (2 seconds, 796 milliseconds)
So the test actually did run.. contrary to the intention.
So what is the correct syntax / approach to apply a Tag Filter(/Exclusion) via sbt to scalatest classes?
You missed to put the testOnly part in double quote and also give full package to the Tag Annotation to ignore,
sbt "test-only * -- -l full.package.to.HugeTestClass"
example,
Tag annotation
package tags;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#org.scalatest.TagAnnotation
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.METHOD, ElementType.TYPE})
public #interface ExcludeMePleaseTag {}
Test to exclude
#tags.ExcludeMePleaseTag
class ExcludeMeSpecs extends FlatSpec with Matchers {
"I" should " not run" in {
888 shouldBe 1
}
}
to exclude the tests
sbt "test-only * -- -l tags.ExcludeMePleaseTag"
This github issue was helpful - https://github.com/harrah/xsbt/issues/357#issuecomment-44867814
But it does not work with static Tag annotation,
public class WrapperClass {
#org.scalatest.TagAnnotation
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.METHOD, ElementType.TYPE})
public static #interface ExcludeMePleaseTag {
}
}
sbt "test-only * -- -l tags.WrapperClass.ExcludeMePleaseTag"

Duplicate index on sqlite, not on mysql

I have a entity definition that works on dev and production envs (mysql), but not on test (sqlite):
/**
* Invoice
*
* #ORM\Table(name="invoice", indexes={
* #ORM\Index(name="ref_idx", columns={"ref"}),
* #ORM\Index(name="created_at_idx", columns={"created_at"}),
* #ORM\Index(name="paid_idx", columns={"paid"}),
* #ORM\Index(name="is_valid_idx", columns={"is_valid"}),
* #ORM\Index(name="canceled_idx", columns={"canceled"})
* })
* #ORM\Entity(repositoryClass="AppBundle\Repository\InvoiceRepository")
*/
class Invoice
// [...]
When I run doctrine:schema:create or doctrine:schema:update --force on test env, I have the following error:
[Doctrine\DBAL\DBALException]
An exception occurred while executing 'CREATE INDEX created_at_idx ON invoice (created_at)':
SQLSTATE[HY000]: General error: 1 index created_at_idx already exists
[PDOException]
SQLSTATE[HY000]: General error: 1 index created_at_idx already exists
Is someone already had this kind of issue? How to solve/ignore it?
Thanks.
The solution is here: https://stackoverflow.com/a/24634713/1731473
In a nutshell, your index must have a unique name across your database.
So you can't have:
invoice -> created_at_idx
user -> created_at_idx
But:
invoive -> invoice_created_at_idx
user -> user_created_at_idx

symfony 2.1: phpunit -c app tries to run symfony core tests

In my Symfony 2.1 app, I'm unable to run the recommended phpunit -c app/. I get the following error message (I added linebreaks):
PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from /symfony2-dual-auth/app/phpunit.xml.dist
F
PHP Fatal error: main(): Failed opening required
'/symfony2-dual-auth/vendor/symfony/symfony/vendor/autoload.php'
(include_path='.:/usr/share/php:/usr/share/pear')
in /symfony2-dual-auth/vendor/symfony/symfony/autoload.php.dist on line 3
Complete source code here: https://github.com/meonkeys/symfony2-dual-auth . Except the test, since it doesn't pass. Here's src/Oh/FOSFacebookUserBundle/Tests/SimpleFunctionalTest.php...
<?php
namespace Oh\FOSFacebookUserBundle\Tests;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\WebTestCase;
class SimpleFunctionTest extends WebTestCase {
public function testIndex() {
$client = static::createClient();
$crawler = $client->request('GET', '/login');
$this->assertEquals(1, $crawler->filter('html:contains("Remember me")')->count());
}
}
I was extending the wong class. Here's the fix:
-use Symfony\Bundle\FrameworkBundle\Tests\Functional\WebTestCase;
+use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

Resources