Coder Social home page Coder Social logo

hackmock's Introduction

Build Status

HackMock

Creating mock objects for hacklang - yes, seriously.

Note

Due to the use of eval, hackmock may stop working in future hhvm versions.

What works?

  • Strict mode
  • Creating mocks of interfaces and concrete classes
  • Defining basic method expectations (parameter validation, return value definition)

What does not work?

  • Everything else, especially rare and/or untested cases involving generics, etc.
use function Usox\HackMock\{mock, prospect};

class SomethingTest extends \Usox\HackMock\HackMock {

  public function testSomething() {
    $my_fine_class = mock(SomeInterface::class);

    prospect($my_fine_class, 'someMethodName')
      ->once()
      ->andReturn('some-fine-value');

    prospect($my_fine_class, 'someOtherMethodName')
      ->andThrow(new \Exception('foobar'));
  }
}

hackmock's People

Contributors

fredemmott avatar ivyhjk avatar jjergus avatar lexidor avatar usox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hackmock's Issues

interface - default mixed value

Interfaces that contains methods with default parameters as mixed, breaks the generated code.

HHVM Reflection generate a HH\mixed value, but HHVM does not recognizes this
type as a mixed value, like HH\bool, HH\string, etc.

Test code:

use function Usox\HackMock\{mock, prospect};

interface InterfaceWithDefaultInput {
    public function mixedWithoutDefault(mixed $input): void;
}

class InterfaceWithDefaultInputTest extends \Usox\HackMock\HackMock {
    public function testWithDefault(): void {
        $stub = mock(InterfaceWithDefaultInput::class);

        prospect($stub, 'mixedWithoutDefault')->execute(vec['foo']);
    }
}

I think replacing value on-fly is a quick patch, but using Reflection (maybe custom reflection classes) or a Builder (for types) should be a better solution ๐Ÿค”.

Destructors (and ReflectionMethod::isDestructor) no longer exist

Destructors and isDestructor don't exist:

repos/hackmock/src/Mock/MockBuilder.hh:40:45,56: Could not find method isDestructor in an object of type ReflectionMethod (Typing[4053])

Partial mode must be specified:

repos/hackmock/tests/Stub/UntypedClass.hh:1:1,192: Invalid mode; try '// strict' or '// partial' (Parsing[1002])

Default boolean value

Mocking fails with default boolean values as parameter.

Example code:

<?hh // strict

class Foo {
    public function defaultBoolean(bool $param = false): void {

    }
}

The error:

Fatal error: An expression is expected here. in src/Mock.hh(113)(b5ced6f046ab20f7c27f048ec18873f0) : eval()'d code on line 5

mock untyped parameters

some classes are already written in PHP, and when their have some untyped parameters, mocking fails, for example:

<?hh // decl

class Foo {
  public function untypedParameter($param): void { 

  }
}

also works with <?php as initial tag.

The error:

Fatal error: A type specifier is expected here. in /app/vendor/usox/hackmock/src/Mock.hh(123)(df9b58dfc07696f2f66f85fd021ce9e7) : eval()'d code on line 5

I know that PHP support is ending for HHVM, but come on, this is breaking a lot of possible mocks and possible integrations with this great library.

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.