cawolf / phpstorm-quick-mock Goto Github PK
View Code? Open in Web Editor NEWPHPStorm plugin allowing you to quickly create mock objects from within a test class
License: MIT License
PHPStorm plugin allowing you to quickly create mock objects from within a test class
License: MIT License
Describe the bug
In PHP8, constructor property promotion was introduced. On top of that we can now define visibility and immutability of a property.
Example:
public function __construct(
private readonly EventLogInterface $eventLogService,
private readonly FileUploadRepository $fileUploadRepository
) {
}
Quick mock will create these annotations:
/** @var private|ObjectProphecy */
private $eventLogService;
/** @var private|ObjectProphecy */
private $fileUploadRepository;
regression of #18, introduced in #26
2 sources of the problem here:
ExistingMocks
is currently always returning true
, if not both properties (like $data
and $dataQuickMocked
) are present (in 95% of all cases) - logic needs to be rewritten to notMocked(existingField) && notMocked(existingSuffixedField)
val parameterMapByName = ArrayList<Pair<String, Parameter>>(parametersWithoutMocks.size)
ignores allParameters
, they should be combined in a real Map
Is your feature request related to a problem? Please describe.
When iterating over different version of the constructor (e.g. when doing TDD), the plugin will regenerate the mocks and members of the test.
Describe the solution you'd like
The plugin should identify previously generated mocks and members and not regenerate them.
Describe alternatives you've considered
If the plugin could reliably identify previously generated mocks as such, it could alternatively delete all previously generated mocks and regenerate them.
There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.
Error type: Cannot find preset's package (github>whitesource/merge-confidence:beta)
The option for mock-generation only shows up in the context menu when the cursor is located between the parentheses. It would be nice if this option would be available when the cursor is located on the classname of the subject. Also i would reccomend to rename the context menu text to something like "Quick-Moc:k: add constructor prophecies"
First: thanks for adding the feature "Adding missing constructor properties".
I have a little adjustment:
since we have a PHPMD rule that limits the lines to 140 characters, I'd like the plugin to handle this.
First approach: count the characters and add line breaks if it exceeds 140 characters.
Second approach: if the constructor has >= 4 Parameters, do line breaks.
Third Approach: do line breaks by default.
Example:
$this->subject = new Authenticator(
$this->clientRegistry->reveal(),
$this->router->reveal(),
$this->proxyHandler->reveal(),
$this->logger->reveal()
);
Is your feature request related to a problem? Please describe.
No
Describe the solution you'd like
I would love it if this would also work for any Methods and not just Constructors.
As an example where this might be helpful:
ArgumentResolvers in Symfony get passed both a Request and ArgumentMetadata Object which would be great targets for a Prophecy.
Describe alternatives you've considered
I considered opening a PR for this but I have no idea how these Plugins are written and have only limited experience with Kotlin / Java. I'd be open to collaborate on it though.
To Reproduce
Have a class with dependancies, which are defined using alias in constructor and not real class name.
Use QuickMock to create prohesies in its test class.
Observed result
Aliases are used as class names in prophesize()
but were not defined
Expected behavior
Similar aliases are defined in test class use
The example from documentation:
$this->subject = new Subject($this->logger, $this->router);
should work like this:
$this->subject = new Subject($this->logger->reveal(), $this->router->reveal);
note ->reveal()
Additional helper would be great, which:
prophesize
accordingly their classes from doc blockUse case:
When a test needs several more mocks which are not a part of subject constructor calls, developer could just add private properties and generate all prophesies by this magic feature
It would be nice if the annotations of the generated mocks would contain that its an Objectprophecy. For Example:
/** @var ProducerInterface|ObjectProphecy */
private $producer;
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
.github/workflows/release.yml
actions/checkout v3
actions/setup-java v3
gradle/gradle-build-action v2
thebritican/fetch-latest-release v2.0.0
actions/upload-release-asset v1.0.2
ubuntu 18.04
.github/workflows/test.yml
actions/checkout v3
actions/setup-java v3
gradle/gradle-build-action v2
ubuntu 18.04
gradle.properties
settings.gradle
build.gradle
org.jetbrains.intellij 1.10.0
org.jetbrains.gradle.plugin.idea-ext 1.1.7
org.jetbrains.kotlin.jvm 1.7.22
junit:junit 4.13.2
gradle/wrapper/gradle-wrapper.properties
gradle 7.6
The type hint should either be /** @var mixed */
or omitted completely.
Describe the bug
Plugin creates error if no constructor is present
To Reproduce
Expected behavior
Describe the bug
When the tested class has parameters in the constructor which are named like properties of the PHPUnit TestCase
class, these would be overwritten after using quickmock
To Reproduce
Steps to reproduce the behavior:
data
Expected behavior
Maybe detection if TestCase
already reservers a name and rename the generated property
Currently when a subject constructor requires e.g. string parameters they are passe simply as empty strings in the call.
The could be created as test class fields and passed respectively.
Benefit: these values can be used is assertions later on.
Is your feature request related to a problem? Please describe.
When creating new mock members of the test, a doc block is always generated. In case I am using other plugins like Haehnchen/idea-php-phpunit-plugin , I do not need these. Therefore I want to be able to disable doc block generation.
Describe the solution you'd like
A configuration page to disable generation of doc blocks.
Is your feature request related to a problem? Please describe.
PHP7.4 introduced class variable type-hinting. It would be great if the plugin could identify the selected php version for this project and if >= 7.4, could make a slight change to the mock declaration.
Describe the solution you'd like
Current behaviour:
/** @var DistributionRepository|ObjectProphecy */
private $distributionRepository;
Desired behaviour:
use Prophecy\Prophecy\ObjectProphecy;
/** @var DistributionRepository|ObjectProphecy */
private ObjectProphecy $distributionRepository;
If testing subject has dependancies of a basic type like array, string or integer, they are also added as prophesies. They could be added simply with an empty value of the corresponding type ([], '' or 0)
For better readability i would suggest to put a newline between the generated prophecies and the subject class
In some cases, the mock generation fails and creates code fragments like
$this->config = $this->prophesize(array::
In the background, an exception is thrown:
ERROR: The method designed for fully qualified names only. Got "array|string[]".
java.lang.Throwable: The method designed for fully qualified names only. Got "array|string[]".
at com.intellij.openapi.diagnostic.Logger.error(Logger.java:123)
at com.jetbrains.php.util.PhpContractUtil.assertFqn(PhpContractUtil.java:19)
at com.jetbrains.php.codeInsight.PhpCodeInsightUtil.isSameNamespace(PhpCodeInsightUtil.java:662)
at com.jetbrains.php.codeInsight.PhpCodeInsightUtil.alreadyImported(PhpCodeInsightUtil.java:841)
at de.cawolf.quickmock.intention.service.AddMissingUseStatements.invoke(AddMissingUseStatements.kt:13)
at de.cawolf.quickmock.intention.QuickMockCreator.invoke(QuickMockCreator.kt:71)
Describe the bug
The context menu for adding constructor arguments is not showing up if your subject extends a class which holds the constructor without having a constructor on its own.
Expected behavior
Even though Abstraction could and should be avoided most of the time, sometimes you are stuck with legacy code where refactoring is currently not an option. So I beg you to implement this change :)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.