Skip to content

Commit

Permalink
Feature/readable output (#2)
Browse files Browse the repository at this point in the history
* feat: add a readable output to the calls done

* feat: mock recalls vars set and also prints them

* refac: edits

* fix: changed the regex and allowd ->all() = __args

* feat: add tests for the mockery of mocks

* feat: add ci/codecov

* foobar

* fix: ci and coverage reports

* feat: badges :)

* feat: improve tests

* feat: dump the contents of the value set

* fix: tests to account the colection wrapping

* fix: too much arrayness

* fix: grammar and readme
  • Loading branch information
ReeceM authored May 4, 2019
1 parent 73a2854 commit 17eb8b6
Show file tree
Hide file tree
Showing 13 changed files with 466 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
composer.lock
vendor
.phpunit.result.cache
32 changes: 32 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
dist: xenial
language: php

env:
global:
- SETUP=stable

matrix:
fast_finish: true
include:
# - php: 7.2
- php: 7.2
env: SETUP=lowest
# - php: 7.3
- php: 7.3
env: SETUP=lowest

cache:
directories:
- $HOME/.composer/cache

before_install:
- travis_retry composer self-update

install:
- if [[ $SETUP = 'stable' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-stable --no-suggest; fi
- if [[ $SETUP = 'lowest' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable --no-suggest; fi

script: vendor/bin/phpunit --coverage-clover=coverage.xml --coverage-text

after_success:
- bash <(curl -s https://codecov.io/bash)
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Reflection Mocker
<p align="center">
<a href="https://codecov.io/gh/ReeceM/mocker">
<img src="https://codecov.io/gh/ReeceM/mocker/branch/master/graph/badge.svg" />
</a>
<a href="https://travis-ci.com/ReeceM/mocker">
<img src="https://travis-ci.com/ReeceM/mocker.svg?branch=master" />
</a>
<a href="https://packagist.org/packages/reecem/mocker"><img src="https://poser.pugx.org/reecem/mocker/v/stable" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/reecem/mocker"><img src="https://poser.pugx.org/reecem/mocker/license" alt="License"></a>
<a href="https://packagist.org/packages/reecem/mocker"><img src="https://poser.pugx.org/reecem/mocker/downloads" alt="Downloads"></a>
</p>

This package is initially made to fi an issue on the MailEclipse package, but improvements are welcome.
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
Expand Down Expand Up @@ -53,6 +59,13 @@ $mock = new ReflectionMockery(new \ReflectionClass('\App\User'));
* 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"]'

```

Expand Down
22 changes: 22 additions & 0 deletions _docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Example

```php
<?php

namespace App;

require_once __DIR__ . '/vendor/autoload.php';

use ReeceM\Mocker\Mocked;
use ReeceM\Mocker\Utils\VarStore;

$mocked = new Mocked('user', VarStore::singleton());
$mocked->name->class = 'Chips';

echo $mocked->name->class . PHP_EOL;

echo $mocked->name->class->data . PHP_EOL;

die(1);

```
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"illuminate/support": "^5.6",
"illuminate/filesystem": "^5.6"
},
"require-dev": {
"phpunit/phpunit": "^7.5",
"orchestra/testbench": "^3.5"
},
"keywords": [
"laravel", "mockery", "helper", "testing"
],
Expand All @@ -18,9 +22,15 @@
],
"homepage": "https://github.com/reecem/mocker",
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"ReeceM\\Mocker\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
}
}
32 changes: 32 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>

<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<php>
<server name="APP_ENV" value="testing"/>
</php>
<logging>
<log type="coverage-text" target=""/>
<!-- <log type="coverage-clover" target="coverage.xml"/> -->
</logging>
</phpunit>
143 changes: 122 additions & 21 deletions src/Mocked.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,145 @@
namespace ReeceM\Mocker;

use Illuminate\Support\Arr;
use ReeceM\Mocker\Utils\VarStore;

/**
* the default mocked class that has dynamic variable names and setting of them too
* this is what makes tha fake objects that result from reading un-typed params
*/
class Mocked {
private $basename;
private $vars = [];

public function __construct($_basename)

/**
* the base array from the new call
*/
private $base;

/**
* the previous calls to the method
*/
private $previous;

/**
* vars set
* @var array $vars
*/
private $vars;

/**
* Singleton storage
* @var \ReeceM\Mocker\Utils\VarStore $store
*/
private $store;

/**
* the combined class list to dump for to string
* @var array $trace
*/
private $trace = [];

private static $GET_METHOD = "__get";
private static $SET_METHOD = "__set";

/*
* The mocked constructor
* @param string|array $base the name of the arg/object (buttery biscuit base)
* @param \ReeceM\Mocker\Utils\VarStore $store singleton variable storage
* @param mixed $previous the base of the calling class
*/
public function __construct($base, VarStore $store, $previous = [])
{
$this->basename = $_basename;
$this->previous = $previous;
$this->store = $store;
$this->base = $base;

if(is_string($base)) {
$this->base = [['args' => [$base], 'function' => static::$GET_METHOD]];
}

$this->structureMockeryCalls();
}

public function __get($name)
/**
* takes the debug trace and structures the single level call
* @todo ensure to implement a call limit on this...
*/
private function structureMockeryCalls()
{
return Arr::get($this->vars, $name, $this);
$toSet = null;
try {
$args = Arr::get($this->base[0], 'args', []); // only one if its a get command
$function = Arr::get($this->base[0], 'function', '__get');
// $type = Arr::get($this->base[0], 'type', ''); '->' / '::'
if($function == self::$GET_METHOD)
{
// merge the preceding calls with this one
array_push($this->previous, $args[0]);
$this->trace = $this->previous;
} else {
array_push($this->previous, $args[0]);
$this->trace = $this->previous;
$toSet = $args[1];
}
return $this->setMockeryVariables($args[0], $toSet);

// return __(implode('.', [
// 'mocked',
// $this->basename,
// $name
// ]));
} catch (\Exception $th) {
throw $th;
}
}

public function __set($name, $value)
private function setMockeryVariables($key, $value = null)
{
$this->vars[$name] = $value;
// return $name . ' ' . $value;
$memorable = $this->store->memoized;

$memorable[$key] = $value;

$this->vars = array_merge($memorable, $this->store->memoized);

$this->store->memoized = $this->vars;
}

public function __toString()
public function __get($name)
{
return 'mocked ' . $this->basename;
/**
* @todo make this return a translation instance for the basename
* allows user define-able test vars
* @todo maybe return the value of the variable if it has been set and has a value
*/
return __('mocked.' . $this->basename . '_to_string');
return new Mocked(debug_backtrace(false, 1), $this->store, $this->trace);
}

/**
* Set a method to the calls
*/
public function __call($name, $arguments)
{
// return new Mocked()
}

/**
* set the value of something inside the class
*/
public function __set($name, $value)
{
return new Mocked(debug_backtrace(false, 1), $this->store, $this->trace);
}

/**
* @todo implement __callStatic
*/

/**
* Return a string of the called object
* would be at the end of the whole thing
* @param void
* @return string
*/
public function __toString()
{
$calledValue = $this->store->memoized[array_reverse($this->trace)[0]] ?? null;

if($calledValue != null) {
return implode("->", $this->trace) . ' => ' . collect($calledValue);
}

return implode("->", $this->trace);
}
}
}
Loading

0 comments on commit 17eb8b6

Please sign in to comment.