How to extend \PHPUnit\TextUI\Command::createRunner in recent versions of PHPUnit? - phpunit

The current version is defined as this:
/**
* Create a TestRunner, override in subclasses.
*/
protected function createRunner(): TestRunner
{
return new TestRunner($this->arguments['loader']);
}
What's however not directly visible that \PHPUnit\TextUI\TestRunner, the returned type, is declared as:
final class TestRunner…
Either I got it all wrong, but looks to me in fact you can't really extend the createRunner because you'd have to return a final class which you can't really extend… 🤷‍♀️

Related

Unpack Symfony "tagged Services"

I got a class which accepts multiple Consumer implementations as constructor arguments.
I want to "fill in" all my Consumers via the Symfony DI-Container.
I tried injection tagged services.
final class SynchronousMessageDispatcher implements MessageDispatcher
{
/**
* #var Consumer[]
*/
private $consumers;
public function __construct(Consumer ...$consumers)
{
$this->consumers = $consumers;
}
}
So I tried to Tag the services in the services.yml like that:
services:
_instanceof:
EventSauce\EventSourcing\Consumer:
tags: ['eventsauce.consumer']
And then inject it like this:
eventsauce.message_dispatcher:
class: EventSauce\EventSourcing\SynchronousMessageDispatcher
arguments: [!tagged eventsauce.consumer]
Now I'm getting the following error:
Argument 1 passed to EventSauce\EventSourcing\SynchronousMessageDispatcher::__construct() must implement interface EventSauce\EventSourcing\Consumer, instance of Symfony\Component\DependencyInjection\Argument\RewindableGenerator given
I fully understand why. Is there a way to unpack services
In other words: Is it possible to modify [!tagged eventsauce.consumer] somehow. Or is the ...$consumers syntax incompatible with the Tagged service Injection in Symfony.
Don't get me wrong. I know that I can easily implement MessageDispatcher myself. Just wanted to know ;-)
My original solution:
As "Tomáš Votruba" mentioned you'd have to rewrite your own !tagged functionality. e.g. !tagged-variadic.
This is not worth the effort for me. I'd rather implement the class using an iteratable ("nifr" explained the benefits, thanks).
For further reading, there is a closed issue on symfony/symfony#23608
My new solution
I used Argument unpacking and the Delegation pattern to use the class the library provided with my tagged services.
Work :-) Hurray.
final class TaggedMessageDispatcher implements MessageDispatcher {
public function __construct(iterable $consumers)
{
$this->dispatcher = new SynchronousMessageDispatcher(... $consumers);
}
public function dispatch(Message ...$messages): void
{
$this->dispatcher->dispatch(... $messages);
}
}
You're using a wrong typehint here.
With the [!tagged <tag>] syntax a single iterable will be injected - not an undefined number of arguments as expected by the splat operator.
You're actually typehinting for multiple Consumer objects as arguments with the splat (...$arguments) operator here.
So the answer to your question is:
The splat operator is not compatible with the [!tagged ..] syntax.
You'd indeed need to write your own injection type that splits up the tagged services when using a new notation like [!tagged-call_user_func ..].
That said it doesn't really make sense to collect a list of objects, extract them to be function arguments just to let PHP put them back into a list again. I get your idea behind it in terms of code cleanliness though.
Another limitation is the fact that you can't pass multiple variadic arguments to a function. So ...
public function __construct(Alpha ...$alphas, Beta ...$betas)
... is not possible.
A possible solution/workaround allowing you to keep the typehinting for the collection would be the following:
final class SynchronousMessageDispatcher implements MessageDispatcher
{
/**
* #var Consumer[]
*/
private $consumers;
public function __construct(iterable $consumers)
{
foreach($consumers as $consumer) {
assert($consumer instanceof Consumer, \InvalidArgumentException('..'));
}
$this->consumers = $consumers;
}
}

symfony 1.4 - how to reuse validate method across modules

I have a quite complex validate method in module1/actions.php called from module1/executeMyAction
I would like to reuse this in module2 rather than duplicate very similar code.
It feels like I should use a component or something like this but I don't need to call validate from a template I need to call it as part of the module1/executeMyAction or module2/executeMyAction so I can then set various variables for the executeMyActionSuccess.php template to handlle.
Can anyone let me know how I should reuse this validation code, I considered moving it into the form class but that just means I can't set the template variiables and it seems like it is breaking the MVC structure a bit so I'm not happy with that.
Would really appreciate any guidance.
If you want to share some parts of code between actions you can create a custom class which will implement some methods you need. You can put the class in the lib directory of either the application or the whole project.
E.g. create a apps/frontend/lib/myUtil.class.php
class myUtil
{
public static function addNumbers($a, $b)
{
return $a + $b;
}
}
Then in your action you can just use:
$sum = myUtil::addNumbers(2, 3);
If your code depends on some other objects it's best if you don't implement static methods but create an object of the class. E.g.
class myUtil
{
protected $request;
public function __construct(sfWebRequest $request)
{
$this->request = $request;
}
public function sumFromRequest()
{
return $this->request->getParameter('a') + $this->request->getParameter('b');
}
}
then in your action
public function executeSomething(sfWebRequest $request)
{
$util = new myUtil($request);
$this->sum = $util->sumFromRequest();
}
If your code is strictly used for validation of form fields you can create a custom validator which can be then used in your form. (which will definitely be the best solution). You can read about creating custom validators in the Symfony docs.

How can I get the name of the defined class in a parent static method in Groovy

Note: I found another answer that suggests that Java would redirect the static method call to it's own class even if it's called on a child class so I guess I need to find a Groovy work-around trick or it's just not going to be doable.
Here's the problem: I created an abstract generic "Launcher" class with a "public static void main". The idea is that you extend it and in your child class you annotate methods like this:
#Command("Show an explorere shell")
public dir() {
"explorer".execute()
}
The parent of this class has a main that goes through, reflects for the #Command annotation and if the method name matches your parameter, executes it.
The problem is that I can't figure out how to tell what the actual, instantiated class is within the parent's static main method.
I'm pretty sure there is a trick somewhere--"this" won't work in statics, stack traces don't contain the actual class, just the parent class, and I can't find any meta-info in the class or MetaClass objects that helps.
Currently I've gotten it to work by hard-coding the name of the child class into the parent's main like this:
public class QuickCli {
public static void main(String[] args} {
(new HardCodedChildClassName())."${args[0]}"()
}
}
I cut quite a bit out of that, but it's the general idea. I'd like to replace
"new HardCodedChildClassName()"
with something that will work for any class that extends this class.
Given the two code snips above, the command would be executed from the command line as:
groovy HardCodedChildClassName dir
Although I'd prefer not to make all the #Command methods static I could do so if I had to, but currently I'm not even convinced I could make that work.
I'm not sure that's possible. In any case, it's likely to be an ugly hack if it is. I'd suggest this alternative: Rather than using the static main() entry point, make QuickCli a Runnable. Groovy will automatically create an instance and call run() on it when it is launched.
One minor problem here is capturing the command-line arguments. Groovy handles this by passing them to a constructor with a String[] parameter. The instantiated class needs this constructor to capture the args, but in Java, constructors are not inherited. Fortunately, Groovy has an InheritConstructors annotation that works around this.
Here's an example of how this would look:
class QuickCli implements Runnable {
def args
QuickCli(String[] args) {
this.args = args
}
void run() {
"${args[0]}"()
}
}
#groovy.transform.InheritConstructors
class HardCodedChildClassName extends QuickCli {
#Command("Show an explorere shell")
public dir() {
"explorer".execute()
}
}

Allow custom text representation of data in MXML

I have an actionscript class called Dimension, which allows a client to specify a dimension using a value and a unit such as "CM" or "Inches". I want to use an instance of this class as a property in MXML, so a user can write
<DimensionView value="2cm"/>
How do I make "2cm" an accepted string value for Dimension? I assume I need to write a parser method on my Dimension class, but I can't work out which interface I should be implementing to provide this functionality.
Can anyone help?
One option is to just type the value property as a String, write a getter and a setter for it and do the parsing there:
/**
* docs here
*/
[Bindable(event="valueChanged")]
public function get value():String
{
return _valueInt.toString();
}
/**
* #private
*/
public function set value(aVal:String):void
{
// parse the aVal String to an int (or whatever) here
_valueInt = parsed_aVal;
dispatchEvent(new Event("valueChanged"));
}
On a related note, the framework components implement the feature of allowing for the usage of percentage signs in some sizing properties, when assigned in MXML, by using an undocumented metadata field called PercentProxy. The below example is the width property getter and setter from mx.core.UIComponent:
[Bindable("widthChanged")]
[Inspectable(category="General")]
[PercentProxy("percentWidth")]
override public function get width():Number
{
// --snip snip--
}
override public function set width(value:Number):void
{
// --snip snip--
}

Is it possible to add behavior to a non-dynamic ActionScript 3 class without inheriting the class?

What I'd like to do is something like the following:
FooClass.prototype.method = function():String
{
return "Something";
}
var foo:FooClass = new FooClass();
foo.method();
Which is to say, I'd like to extend a generated class with a single method, not via inheritance but via the prototype.
The class is generated from a WSDL, it's not a dynamic class, and I don't want to touch the generated code because it will be overwritten anyway.
Long story short, I'd like to have the moral equivalent of C# 3:s Extension Methods for AS3.
Edit: I accepted aib's answer, because it fits what I was asking best -- although upon further reflection it doesn't really solve my problem, but that's my fault for asking the wrong question. :) Also, upmods for the good suggestions.
Yes, such a thing is possible.
In fact, your example is very close to the solution.
Try
foo["method"]();
instead of
foo.method();
#Theo: How would you explain the following working in 3.0.0.477 with the default flex-config.xml (<strict>true</strict>) and even a -compiler.strict parameter passed to mxmlc?
Foo.as:
package
{
public class Foo
{
public var foo:String;
public function Foo()
{
foo = "foo!";
}
}
}
footest.as:
package
{
import flash.display.Sprite;
public class footest extends Sprite
{
public function footest()
{
Foo.prototype.method = function():String
{
return "Something";
}
var foo:Foo = new Foo();
trace(foo["method"]());
}
}
}
Note that the OP said inheritance was unacceptable, as was modifying the generated code. (If that weren't the case, adding "dynamic" to the class definition would probably be the easiest solution.)
Depending on how many methods your class has, this may work:
Actual Class:
public class SampleClass
{
public function SampleClass()
{
}
public function method1():void {
Alert.show("Hi");
}
Quick Wrapper:
var actualClass:SampleClass = new SampleClass();
var QuickWrapper:Object = {
ref: actualClass,
method1: function():void {
this.ref.method1();
},
method2: function():void {
Alert.show("Hello!");
}
};
QuickWrapper.method1();
QuickWrapper.method2();
#aib is unfortunately incorrect. Assuming strict mode (the default compiler mode) it is not possible to modify the prototype of non-dynamic class types in ActionScript 3. I'm not even sure that it's possible in non-strict mode.
Is wrapping an option? Basically you create a class that takes one of the objects you get from the web service and just forwards all method calls to that, but also has methods of its own:
public class FooWrapper extends Foo {
private var wrappedFoo : Foo;
public function FooWrapper( foo : Foo ) {
wrappedFoo = foo;
}
override public function methodFromFoo( ) : void {
wrappedFoo.methodFromFoo();
}
override public function anotherMethodFromFoo( ) : void {
wrappedFoo.anotherMethodFromFoo();
}
public function newMethodNotOnFoo( ) : String {
return "Hello world!"
}
}
When you want to work with a Foo, but also have the extra method you need you wrap the Foo instance in a FooWrapper and work with that object instead.
It's not the most convenient solution, there's a lot of typing and if the generated code changes you have to change the FooWrapper class by hand, but unless you can modify the generated code either to include the method you want or to make the class dynamic I don't see how it can be done.
Another solution is to add a step to your build process that modifies the source of the generated classes. I assume that you already have a step that generates the code from a WSDL, so what you could do is to add a step after that that inserts the methods you need.
Monkey patching is an (inelegant) option.
For example, suppose you don't like the fact that Flex 3 SpriteAsset.as returns a default border metrics of [7,7,7,7] (unlike flex 2). To fix this, you can:
Create a copy of SpriteAsset.as and add it to your project at /mx/core/SpriteAsset.as
Edit your local copy to fix any problems you find
Run your ap
Google "flex monkey patch" for more examples and instructions.

Resources