Giter Club home page Giter Club logo

mocker's Introduction

Reflection Mocker

Latest Stable Version License Downloads

This package is initially made to for an issue on the MailEclipse package, but improvements are welcome. It currently is probably stupid simple, but deals with the one job of reading a file and mocking it.

Generate a mocked instance of the un-typed params in a __construct() method

This searches the file retrieved from the reflection class and looks for all object like arrow calls; ie:

...
public function __construct($objectArg, string $arg) 
{
    $this->value    = $objectArg->value; // this will be picked up
    $this->name     = $arg;
}
...

Installation

You can install the package via composer:

composer require reecem/mocker

Requirements

  • Laravel ^5.6 (min)

Usage

use ReeceM\ReflectionMockery;

/**
 * The class __construct Method is automatically read and args created
 */
$mock = new ReflectionMockery('\App\User');
// or
$mock = new ReflectionMockery(new \ReflectionClass('\App\User'));

// some time later

/**
 * Use call a variable from the class that don't exist
 */
{{ $mock->get('somethingNotInUser') }}
{{ $mock->somethingNotInUser }}

// both would return 

"mock->somethingNotInUser"
// if something was set in user
'mock->somethingNotInUser => ["value that set"]'

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

todo

  • Add functionality to account for $this->internal = $param; searching so it works later on in the code
  • Add a translation file for mocked values when testing to give a translated result for previews

Support

Consider supporting some code if it is useful to you ๐Ÿ˜„

  • MailEclipse "just a small donation from Laravel mail editor owner, thanks for your contributions"

Buy Me a Coffee at ko-fi.com

License

The MIT License (MIT). Please see License File for more information.

Sponsors

mocker's People

Contributors

blakehooks avatar qoraiche avatar reecem avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mocker's Issues

Move away from illuminate

I would like to move completely away from the dependency on the Illuminate packages.

Mainly to make it not restricted in the versions, thinner and also a little more portable.

I could make use and reference the license back to the Illuminate/Laravel for function patterns that I use. Or roll my own.

Nice things is that the package then can be used in any type of PHP project.

Marginally questioning the benefit though. Note to self.

Add Laravel 7 Support

Is your feature request related to a problem? Please describe.
Package currently does not support Laravel 7, blocking the ability to upgrade for any projects that use this library directly or any packages that require this library.

Describe the solution you'd like
Add support for illuminate/filesystem to ^7.0

Describe alternatives you've considered
Only possible alternative is to remove any package that requires this library

Pollution across the objects and array values from VarStrore::class

Describe the bug
There is the definite chance that a value set on a different object will be returned when it is retrieved from a different one that doesn't come from the same call

To Reproduce

// assign a value to the mocker object
$mocked->value1->sub_value = 2;

echo $mocked->value1->sub_value . PHP_EOL;
// returns int(2)

// now assign that same sub_value to another object or array
$mocked->assoc_arr['sub_value'] = 'Must be different value';

echo $mocked->assoc_arr['sub_value'];
// returns 2

// but

echo $mocked->sub_value; 
// returns 2

echo $mocked->value1->sub_value; 
// returns 2

Expected behavior
This should not do that and has a problem to do with the flat structure of the VarStore and the way in which the values are resolved.

The VarStore should be structured as the way in which the values should be called i.e.:

// calling 
$mocked->sub_value; // would return 'null' or 'mocked->sub_value';
or
$mocked->value1->sub_value; // gives 2
and 
$mocked->assoc_arr['sub_value']; // will give the string

Object cannot be used as type array

Describe the bug
The object class Mocked cannot have calls to arrays done on it.
This is raised on the dependent package on Qoraiche/laravel-mail-editor#30

To Reproduce
Steps to reproduce the behavior:
Call and array on an object that is mocked. It can be done using multi-dimensions and also just single.
e.g. `$this->data['group']['name']

Expected behavior
Should return a value for the array, even if it has multiple calls to itself

Screenshots
screenshots on this issue here
Qoraiche/laravel-mail-editor#30

affects

  • reecem\mocker v1.1.0

Objects with values stored don't return the whole array

When a object is assigned to a value e.g.:

$mocked->value['sub_value'] = 'stripes'

echo $mocked->value;
// returns 
// mocked->value

What would be nicer is that when the value is called with __toString() which triggers that reaction of printing a formatted

[
    "sub_value" => 'stripes'
]

or if the values is just called by the magic __get it should return the value as a php array type, instead of assuming that it may be calling another dynamically generated value.

Readable Output

Hi,
First, I want to say thank you for creating this Mock class,

It would be great if you make the mocked arguments output more user readable and complex, so we can use it within MailEclipse previews.

return 'mocked ' . $this->basename;

This line returns a simple string mocked varname.

Needed Result:

{{ varname }}

Including $vars when defined:

{{ varname->complex->var }}

Thanks,
qoraiche.

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.