Giter Site home page Giter Site logo

function-mocker's People

Contributors

andreasciamanna avatar kagg-design avatar lucatume avatar nikolaystrikhar avatar wppunk avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

function-mocker's Issues

syntax error, unexpected 'public' (T_PUBLIC)

FunctionMocker::replace('\App\Model\Base\XyzUser::save', null);

the Class and Method to redefine

abstract class XyzUser implements ActiveRecordInterface {

public function save(ConnectionInterface $con = null) {
        if ($this->isDeleted()) {
            throw new PropelException("You cannot save an object that has been deleted.");
        }

        if ($this->alreadyInSave) {
            return 0;
        }

        if ($con === null) {
            $con = Propel::getServiceContainer()->getWriteConnection(EubylonUserTableMap::DATABASE_NAME);
        }

        return $con->transaction(function () use ($con) {
            $ret = $this->preSave($con);
            $isInsert = $this->isNew();
            if ($isInsert) {
                $ret = $ret && $this->preInsert($con);
            } else {
                $ret = $ret && $this->preUpdate($con);
            }
            if ($ret) {
                $affectedRows = $this->doSave($con);
                if ($isInsert) {
                    $this->postInsert($con);
                } else {
                    $this->postUpdate($con);
                }
                $this->postSave($con);
                EubylonUserTableMap::addInstanceToPool($this);
            } else {
                $affectedRows = 0;
            }

            return $affectedRows;
        });
    }
 }
syntax error, unexpected 'public' (T_PUBLIC)
 /home/ridaamirini/PhpstormProjects/v2.0/lib/vendor/lucatume/function-mocker/src/tad/FunctionMocker/MockWrapper.php:53
 /home/ridaamirini/PhpstormProjects/v2.0/lib/vendor/lucatume/function-mocker/src/tad/FunctionMocker/Replacers/InstanceForger.php:116
 /home/ridaamirini/PhpstormProjects/v2.0/lib/vendor/lucatume/function-mocker/src/tad/FunctionMocker/Replacers/InstanceForger.php:42
 /home/ridaamirini/PhpstormProjects/v2.0/lib/vendor/lucatume/function-mocker/src/tad/FunctionMocker/FunctionMocker.php:211
 /home/ridaamirini/PhpstormProjects/v2.0/lib/vendor/lucatume/function-mocker/src/tad/FunctionMocker/FunctionMocker.php:161
 /home/ridaamirini/PhpstormProjects/v2.0/lib/vendor/lucatume/function-mocker/src/tad/FunctionMocker/FunctionMocker.php:142
 /home/ridaamirini/PhpstormProjects/v2.0/tests/dev_user/RecoverPasswordTest.php:167

Composer installs dependencies even with phpunit 6, deprecated imports

First of all thank you for #3 !

Given the following block:

  "require-dev": {
    "phpunit/phpunit": "^6.1",
    "lucatume/function-mocker": "^1.0"
  }

It successfully installs function-mocker: 1.2 and phpunit 6.1 on php 7.1.4 .
But from the config in this repo it shouldn't allow a phpunit version >= 6.0 .

The reason I'm reporting this is because
some imports like PHPUnit_Framework_Assert will not work in phpunit 6+ (reproducible when calling wasCalledWithOnce on a redefinition).

Is there any chance that phpunit >=6.0 will be supported?
Maybe on a separate branch, even if features won't be added as often?

Patchwork 2.0 works with internal functions but function-mocker does not.

Patchwork can redefine for instance time() calls if a proper entry in patchworks.json is added before patchwork's init. I have the file but for some reason the mocker explicitly checks to see if it is a system function an bails out. Is there a more deeper reason why this is not supported? or it's an artifact of a previous version.

https://github.com/lucatume/function-mocker/blob/master/src/tad/FunctionMocker/Checker.php#L18

Cache directory is causing issues with some IDE's code inspection

I've noticed that in the most recent version of the library, a vendor\lucatume\function-mocker\cache\ folder is created and some cached code is saved there.

This is causing two issues:

  • Our IDE (PhpStorm/IntelliJ) sees this code and causes some code inspection issues (e.g. seeing multiple definitions of a symbol)
  • The cache does not reflect the actual code until tests are executed
  • Sometimes, the cached code is not updated, even if the original has been changed

Is it possible to make the library cache these files outside the project (e.g. in the default "temp" directory)?

A solution which requires an environment variable (so it can fall back to the current vendor\lucatume\function-mocker\cache\ if the variable is missing) would be also fine.

Is there a possibility to see what a function was called with?

Hello,

I have a situation, where I check with wasCalledWithOnce something like this:

$mock_func->wasCalledWithOnce([
    0,
    [],
    [
        'key_1' => [
            [
                'key_1_1' => [
                    [
                        'key' => null,
                        'another_key' => null,
                        'another_one' => 0
                    ]
                ]
            ]
        ],
        'key_2' => 0,
        'key_3' => 0,
        'key_4' => null,
    ],
    0
]);

It's failing at the third argument, The args values are valid, and I can't see where the issue is.

So my question is: Is there any way I can print the diff between what I give it and what the actual args, like what assertEqual is doing?

Thanks,

Infinite loop when FunctionMocker can't find the vendor directory

I've stumbled in a project where the vendor directory does not exist (actually, it does exist but with a different name).

\tad\FunctionMocker\Utils::findParentContainingFrom tries to find the first directory called "vendor" from the current directory and up.

This logic has two consequences:

  1. "vendor" is hard-coded: perhaps \tad\FunctionMocker\FunctionMocker::setUp could accept an optional argument to specify the name of the vendor directory?
  2. Once \tad\FunctionMocker\Utils::findParentContainingFrom reaches the root directory, it continues forever checking that same root directory.
    I think a simple fix could be to check that $dir !== ''/' right before calling $dir = dirname($dir); and, in this case, gracefully fail.

Not a blocking issue for me. The original developers of the project I'm working with, are most likely going to start using the "vendor" directory, as most of the rest of the world :)

I just wanted to report it.

Add replaceInOrder for faster call the replacement of the same function

I have a case where I use one function several times in a row. For example, filter_input. It is not very convenient to use anonymous functions:

public function test_case() {
	$answers = [ 'one', 'two' ];
	FunctionMocker::replace(
		'filter_input',
		function () use ($answers) {
			static $i = 0;

			return $answers[ $i ++ ];
		}
	);

	$this->assertSame(
		'one',
		filter_input( INPUT_POST, 'first_use', FILTER_SANITIZE_STRING )
	);
	$this->assertSame(
		'two',
		filter_input( INPUT_POST, 'second_use', FILTER_SANITIZE_STRING )
	);
}

It would be nice to add method replaceInOrder for a shorter record:

public function test_case() {
	FunctionMocker::replaceInOrder( 'filter_input', [ 'one', 'two' ] );

	$this->assertSame(
		'one',
		filter_input( INPUT_POST, 'first_use', FILTER_SANITIZE_STRING )
	);
	$this->assertSame(
		'two',
		filter_input( INPUT_POST, 'second_use', FILTER_SANITIZE_STRING )
	);
}

Redefine "filter_input" or "time"

I tried to redefine the Method but nothing happens.

FunctionMocker::replace('filter_input','TEST');

$this->assertEquals('Test', filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING));

Patchwork.json

{ "redefinable-internals": [ "filter_input", "time" ] }

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.