hafriedlander / phockito Goto Github PK
View Code? Open in Web Editor NEWPHP Mocking library based on Mockito
PHP Mocking library based on Mockito
Only the first stubbing works.
when($mock->foo())->return(1);
when($mock->foo())->return(2);
when($mock->foo())->return(3);
$mock->foo(); // returns 1
It should return 3.
Hello there.
Suppose I want to mock some class, and that class needs to throw an exception which takes a constructor argument. This seems impossible with the current api.
It does also seem like Phockito is different from Mockito on this point. Mockito syntax would be:
Phockito->when($foo)->bar()->throw(new BazException("Argument here");
The only change needed is to replace
else if ($response['action'] == 'throw') { $class = $response['value']; throw new $class(); }
with
else if ($response['action'] == 'throw') { $class = $response['value']; throw $response['value']; }
What do you think? This would break old tests, and therefore should probably use some other method to register the throwable class. Maybe ->throwInstance(new BazException("foo"))
Is it supposed to be:
fockito
fuhckito (like vietnamise pho)
pohckito
something else?
Nice project by the way - certainly helps reduce context switching when moving between PHP/Java.
Will put together a test to cause issue
This may not be limited to strict mode, but it's how we're running
This (currently) fails because of the reflection trying to get default values for arguments which - if I understand it correctly - comes from the fact that SoapClass is a native class (and the Reflection* doesn't really work with that?).
Hi, I try to create a mock object using namespace
$this->screen = Phockito::mock('\com\holatdd\Screen');
but have a error message
PHP Parse error: syntax error, unexpected T_NS_SEPARATOR, expecting '{' in /usr/share/php/phockito/Phockito.php(256) : eval()'d code on line 1
PHP Fatal error: Class '_phockito\com\holatdd\Screen_Mock' not found in /usr/share/php/phockito/Phockito.php on line 287
Thanks!
Sorry my poor english
Isidro
This is a generated method for a spy:
public function myMethod( $request, $response ){
$args = func_get_args();
$backtrace = debug_backtrace();
$instance = $backtrace[0]['type'] == '::' ? ('::'.'tag_test_mocks_routing_controller') : $this->__phockito_instanceid;
// The value for $response that was passed to the method is
// now replaced by this value for $response.
$response = \Phockito::__called('tag_test_mocks_routing_controller', $instance, 'myMethod', $args);
if ($response) return \Phockito::__perform_response($response, $args);
else return parent::myMethod( $request, $response );
}
Because $backtrace, $instance, $response, and $args are all assigned in this method, if used as function parameters, then the value passed to the method is invisibly replaced by the value in this function.
This means for full functionality with phockito, functions should not take any of those as arguments. The 'bug' manifests itself as function parameters mysteriously becoming null when the function is called on a spy, even though the value passed to the function is non-null. This occurs if no stub/responder has been set for the spy.
Normally I'd submit a pull request with a fix but I'm not actually sure the best way to fix this, since obfuscating the variable names has the same problem, plus makes the code unreadable.
And using call_user_func() seemed to have issues resolving parent when I tried.
About type hinting in PHP: http://www.php.net/manual/en/language.oop5.typehinting.php
Classes with type-hinted methods cannot be mocked.
Code:
<?
Phockito::mock("Foo");
class Foo {
public function doStuff(Bar $bar) {
}
}
class Bar {
}
results in:
Fatal error: Declaration of __phockito_Foo_Mock must be compatible with that of Foo in (...)
I can't mock __toString() if the mocked class does not override this magic method.
See http://www.php.net/manual/en/language.oop5.magic.php
when running a verify this error crashes all tests.
Our project don't use Hamcrest so it must be something from with in this project that is configured wrong.
in the docs for verify the type for the first argument is Phockito_Mock
:
/**
* Verify builder. Takes a mock instance and an optional number of times to verify against. Returns a
* DSL object that catches the method to verify
*
* @static
* @param Phockito_Mock $mock - The mock instance to verify
* @param string $times - The number of times the method should be called, either a number, or a number followed by "+"
* @return Phockito_VerifyBuilder
*/
This type/class cant be found.
This problem will arise for users of php 5.4. The hamcrest library contains a forward compatibility conflict with php. Specifically the function callable
is a keyword in 5.4. This should be easily fixed by updating the hamcrest library. More info here: http://code.google.com/p/hamcrest/issues/detail?id=172
It would be neat if hamcrest/hamcrest-php
composer required instead of committed to the source code.
I'm not happy with the response in issue #5 as the behaviour in Phockito differs from Mockito.
The following test passes in Mockito
package org.richardsson;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class OverwriteTest {
public static class Foo {
public int bar() { return -1; }
}
@Test
public void shouldOverride() {
Foo fooMock = mock(Foo.class);
when(fooMock.bar()).thenReturn(0);
when(fooMock.bar()).thenReturn(1);
when(fooMock.bar()).thenReturn(2);
assertEquals(2, fooMock.bar());
assertEquals(2, fooMock.bar());
assertEquals(2, fooMock.bar());
}
}
that is, the latest return value is used. To me i makes a lot of sense and is something I often use when writing java code. I usually set up the mocks for the main flow in the the setUp() method which means that test methods that verify that flow do not need any mock code. The test methods that verify other code paths on the other hand, clearly indicate the difference in mock behaviour by overrides. With the current Phockito implementation this is only possible by using reset() which adds noise.
It also turns out that the current Phockito implementation does not behave as described in issue #5, so I suppose it is broken.
when($mock->foo())->return(1);
when($mock->foo())->return(2);
when($mock->foo())->return(3);
// Expected behaviour described in issue #5
$mock->foo(); // returns 1
$mock->foo(); // returns 2
$mock->foo(); // returns 3
// Actual behaviour
$mock->foo(); // returns 1
$mock->foo(); // returns 1
$mock->foo(); // returns 1
// Mockito way
$mock->foo(); // returns 3
$mock->foo(); // returns 3
$mock->foo(); // returns 3
The example shown in the README:
Phockito::when($stub)->Foo(argOfTypeThat('B', is(equalTo($b))))->return('Zap');
Does not work. Simply missing return statement. Fixed. Added test case.
Pull request #39. I'm a github noob ;-)
Hi,
When stubbing a method, Hamcrest matchers cannot be used for parameters which have type hints. Attempting to do so raises an exception along the lines of:
PHPUnit_Framework_Error : Argument 1 passed to __phockito_PhockitoHamcrestTest_MockMe_Mock::Baz() must be an instance of PhockitoHamcrestTest_PassMe, instance of Hamcrest_Core_IsInstanceOf given
Is there a recommended work-around for this?
Thanks,
Rowan
I see a lot of test fail silently because the methods on a mock return null automatically. This can be attributed in a weakness to code, but it's difficult to discover such a weakness in the implementation.
What i propose is adding a second optional parameter to Phockito::mock that indicates that instead of returning null for all methods in a mock, instead some exception be thrown. this would result in many tests failing loudly instead of failing silently.
thoughts?
While running some tests locally I stumbled into this error
Fatal error: Cannot redeclare assertThat() (previously declared in /..../vendor/phpunit/phpunit/PHPUnit/Framework/Assert/Functions.php:1637) in /...../vendor/hafriedlander/phockito/hamcrest-php/hamcrest/Hamcrest.php on line 32
It seems like the latest version of phpunit has the same function in the global scope
https://github.com/sebastianbergmann/phpunit/blob/master/src/Framework/Assert/Functions.php#L1671
as
https://github.com/hafriedlander/phockito/blob/master/hamcrest-php/hamcrest/Hamcrest.php#L25
Think it's ok to call the current version stable an tag it as a 1.0.0 or something so we can have a stable composer?
PhockitoSilverStripe and PhockitoSilverStripeTest. I'm not even sure what it does, but I'm pretty sure whatever it is won't work in 3.1 due to the changed way of persisting the manifest ;)
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.