Code Monkey home page Code Monkey logo

phockito's People

Contributors

chillu avatar drdamour avatar fredrikwendt avatar rowanhill avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phockito's Issues

Stubbing override

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.

Throw exception which takes constructor argument impossible

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"))

Pronunciation

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.

Can't mock SoapClient

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?).

using namespaces

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

Spies seem to have reserved words for arguments.

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.

Type Hinting

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 (...)

Phpstorm cant find Phockito_Mock

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.

Stubbing override, continued

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

Type hints & Hamcrest matchers

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

Have mocked methods throw "notmocked" exceptions instead of returning null optionally

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?

AssertThat() already exists in later versions of PHPUnit

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

SS manifest overloading won't work with 3.1

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 ;)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.