Download the PHP package tototoshi/staticmock without Composer

On this page you can find all versions of the php package tototoshi/staticmock. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package staticmock

StaticMock

CI

A mockery-like DSL to replace static methods in tests.

Motivation

Mockery (https://github.com/padraic/mockery) provides nice interfaces to create mock objects. But as for static methods, Mockery needs an alias class and we can't create a mock object in one shot with his easy DSL.

StaticMock provides Mockery-like DSL for static methods. StaticMock depends on runkit7 extension or uopz extension and rewrites static methods temporarily at run-time.

Requirements

About runkit7 settings

Please note that the extension name differs depending on the version of runkit7. In the past it was extension=runkit.so, but nowadays it is extension=runkit7.so

Install

composer.json

Example

Imagine that you are writing tests for User class such as this.

Stubbing and Mocking

User class has a getFeed method. This method aggregates user's feeds from Google+ and Facebook. It depends on GooglePlusClient and FacebookClient to fetch feeds from their API. We sometimes want stubs for GooglePlusClient and FacebookClient to write tests for the User class. Our goal is only to ensure that User class can correctly aggregate feeds from APIs. The behavior of GooglePlusClient and FacebookClient is out of our heads now.

The problem is GooglePlusClient::getFeed and FacebookClient::getFeed are static methods. If they were instance methods, we could manage their dependencies and inject stubs of them to User class. But since they are static methods, we can't do that.

StaticMock solved the problem by replacing the methods temporarily at run-time. It provides an easy DSL for replacing methods. All you need to learn is only a few methods.

See below. In this example, GooglePlusClient::getFeed and FacebookClient::getFeed are changed to return array("From Google+") and array("From Facebook").

StaticMock also has some methods to act as mock objects.

Common pitfalls

Assigning a mock variable ($mock = StaticMock::mock('MyClass')) is required since StaticMock is implemented with constructor and destructor magic. The methods are replaced when the instance of Mock class is created by StaticMock::mock and reverted when the instance goes out of scope.

So, the following code doesn't work as you expect.

Replacing method implementation

andImplement is useful to change the behavior of the method.

See below again. We are writing a test for User::register this time but we don't want to send an email every time running the test.

Pass an anonymous function like below. The Email will not be sent and only a short line will be printed on your console.

With PHPUnit

You can easily define custom PHPUnit assertion. See below.

LICENSE

BSD 3-Clause


All versions of staticmock with dependencies

PHP Build Version
Package Version
No informations.
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package tototoshi/staticmock contains the following files

Loading the files please wait ....