Download the PHP package m3m0r7/method-injector without Composer
On this page you can find all versions of the php package m3m0r7/method-injector. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download m3m0r7/method-injector
More information about m3m0r7/method-injector
Files in m3m0r7/method-injector
Package method-injector
Short Description The library enhance mocking your test.
License MIT
Informations about the package method-injector
What is MethodInjector?
MethodInjector
is an open source software project that provides strong support for generating test doubles for methods, fields, and constants in the target class.
For example, the MethodInjector
can do the following.
- Replace a function in a method with a mock function or an unnamed function and execute it.
- Rewrites the default value of the specified field
- Test by rewriting the value of a constant
- Mock the return value of a specific method itself
- Process can be inserted at the start and end of method execution
MethodInjector
parses the original class file and reconstructs the class.
Therefore, you can easily create test doubles even if the original class is not inheritable (i.e., final
is defined).
However, it is also possible to inherit the original class and create a test double by inheriting the class that the declaration expects.
Documentation
- 日本語
- English
DEMO
Quick start
It can be installed from the following.
How to use?
Easy example
To create a test double with MethodInjector
, do the following
Calling a factory
method returns an instance of the MethodInjector
. inspect
takes the first argument as the name of the class to make the test double, the name of the class
The second argument can specify the condition to create the verification and test double, and the third argument can specify whether to inherit the original class.
You can also call several inspect
methods to create a test double of several classes at once.
Because the test double is generated in the namespace dedicated to MethodInjector
, it basically does not pollute the global namespace.
Restrict the method for replacing.
You can also specify and restrict the method to be replaced by the MethodInjector
of the class Inspector
by specifying the method name.
Method names are case-insensitive, according to the PHP specification.
Replace fields in the class
You may want to rewrite the default values of the fields in your class. You can also use replaceField
to change the default value of a field.
Of course, it is possible to change the field even if it is private
or protected
.
Replace constants in the class
You can use replaceConstant
to rewrite a constant value in the same way as you can rewrite a field. Of course, this is also possible with private
, even protected
.
Output something when start to process the method
You may want to interrupt some processing at the start of the method execution. With MethodInjector
, you can specify a before
of the Condition
class.
It is possible to interrupt some processing at the start of execution. For example, it is useful when you want to measure the execution time of a single method.
Output something when finish to process the method
You can also specify the end of the method execution as well as the start. When it is finished, after
is called.
Replace classes in the method
The MethodInjector
can also replace a class that is instantiated in a method with another class using replaceInstance
.
This allows for use cases that are still under development, or where you want to replace a class with another one.
It is also possible to replace it when reading with static
or self
.
Of course, even if you are reading a static method of a class, you can use replaceStaticCall
to replace it with the following.
Mocking the method
You may want to create a test double when the method itself is in development, or when you want to test a test that returns a certain value, or when it is outside the scope of your testing interests.
With MethodInjector
it is also possible to create a test double for the method itself using replaceMethod
.
Add a replacer
The MethodInjector
provides several replacers beforehand, but you may want to add more replacers depending on the situation.
In that case, you can also add a replacers. The replacers of MethodInjector
is set as the default, but if the original replacers is added, the
Note that the replayer provided by MethodInjector
is not used by default, so it should be re-specified as an argument.
If you use addReplacer
, you can use the replacers provided by MethodInjector
as is.
The replacers acts like a reduce, applying to the AST parsed nodes in the specified order.
Change the inspector
The inspector of the MethodInjector
only provides the bare minimum required functionality and may have obstacles such as not being able to use it on projects with a history.
In that case, you may want to replace the inspector itself in order to test it. Of course, it is also possible to implement the original Inspector. In that case, you should extend Inspector
, which is provided by MethodInjector
by default.
To mock defined constants, fields and methods in the class or trait.
The MethodInjector
performs static analysis of the methods and fields defined in the parent class, as well as the traits, and decomposes them into ASTs and mocks them.
This feature allows you to focus on writing the test in front of you instead of having to think about how to mock the methods of the parent classes and traits when creating a test double.
Also, since MethodInjector
refers to a class recursively, it is possible to mock not only the parent class but also the parent class of the parent class, or if it is a trail, a trail defined in a trail.
Builder
If you are using MethodInjector
, you may feel that the amount of code to make a mock is long. Therefore, the MethodInjector
has a MethodInjector
object, a builder class called ConditionBuilder
is provided to make it easier to create objects.
The ConditionBuilder
mocks the methods and properties of the parent class and all traits by default.
It is possible to write the above. Also, the above is equivalent to the following.
The range of methods applied by ConditionBuilder
is *
by default, that is, all methods.
However, you may want to mock only some of the methods. In that case, you can use the group
to specify a method to be mutable.
In this case, all the date
functions in doSomething
are mocked.
In the above case, the date executed with doSomething1
will return 9999-99-99
and the date executed with doSomething2
will return 0000-00-00
.
License
MIT