GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — - fixed namespaces · sitphp/doubles@312be7b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • composer.json
  • doc
    • 01_intro.md
    • 02_quick_start.md
    • 03_creating_doubles.md
    • 04_testing_doubles.md
    • 05_configuration.md
    • 06_about.md
  • phpunit.xml.dist
  • readme.md
  • src
    • Constraints.php
    • Double.php
    • Exceptions
      • BadMethodCallException.php
      • DoublesException.php
      • InvalidArgumentException.php
      • LogicException.php
      • RuntimeException.php
    • Lib
      • ClassManager.php
      • DoubleCall.php
      • DoubleInterface.php
      • DoubleStub.stub
      • EvalLoader.php
      • Expectation.php
      • ExpectationCollection.php
    • Stubs.php
    • Stubs
      • ReturnArgumentStub.php
      • ReturnCallbackStub.php
      • ReturnSelfStub.php
      • ReturnValueMapStub.php
      • ReturnValueStub.php
      • StubInterface.php
      • ThrowExceptionStub.php
    • TestCase.php
  • tests
    • AssertionTest.php
    • ConstraintTest.php
    • DoubleTest.php
    • StubTest.php
    • bootstrap.php
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎composer.json‎

Copy file name to clipboard
+36-24Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
{
2-
"name": "sitphp/doubles",
3-
"description": "A simple yet powerful library to make class doubles for testing",
4-
"keywords": ["php", "test", "testing", "doubles", "mock", "phpunit", "unit-testing"],
5-
"homepage": "https://github.com/sitphp/doubles",
6-
"license": "MIT",
7-
"authors": [
8-
{
9-
"name": "Alexandre GEISWILLER",
10-
"email": "[email protected]"
11-
}
12-
],
13-
"require": {
14-
"php": ">=7.2",
15-
"phpunit/phpunit": "~8.5"
16-
},
17-
"autoload": {
18-
"psr-4": {
19-
"Doubles\\": "src"
20-
}
21-
},
22-
"autoload-dev": {
23-
"psr-4": {
24-
"Tests\\": "tests"
25-
}
2+
"name": "sitphp/doubles",
3+
"description": "A simple yet powerful library to make class doubles for testing",
4+
"keywords": [
5+
"php",
6+
"test",
7+
"testing",
8+
"doubles",
9+
"mock",
10+
"phpunit",
11+
"unit-testing"
12+
],
13+
"homepage": "https://github.com/sitphp/doubles",
14+
"license": "MIT",
15+
"authors": [
16+
{
17+
"name": "Alexandre GEISWILLER",
18+
"email": "[email protected]"
2619
}
20+
],
21+
"require": {
22+
"php": ">=7.2",
23+
"phpunit/phpunit": "~8.5",
24+
"ext-dom": "*"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"SitPHP\\Doubles\\": "src"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"SitPHP\\Doubles\\Tests\\": "tests"
34+
}
35+
},
36+
"require-dev": {
37+
"ext-mbstring": "*"
38+
}
2739
}
Collapse file

‎doc/01_intro.md‎

Copy file name to clipboardExpand all lines: doc/01_intro.md
+38-21Lines changed: 38 additions & 21 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# Introduction
22

33
## Requirements
4-
The "sitphp/doubles" library requires at least PhpUnit 6.5 and at least PHP 7.0. It should be installed from composer which will make sure your configuration matches requirements.
5-
> {.note .info} Note : You can get composer here : [https://getcomposer.org](https://getcomposer.org).
6-
4+
5+
The "sitphp/doubles" library requires at least PhpUnit 6.5 and at least PHP 7.0. It should be installed from composer
6+
which will make sure your configuration matches requirements.
7+
> {.note .info} Note : You can get composer here : [https://getcomposer.org](https://getcomposer.org).
8+
79
## Install
8-
Once you have composer installed, add the line `"sitphp/doubles": "~2.1"` in the `"require-dev"` section of your composer.json file :
10+
11+
Once you have composer installed, add the line `"sitphp/doubles": "~2.1"` in the `"require-dev"` section of your
12+
composer.json file :
913

1014
```json
1115
{
@@ -20,15 +24,19 @@ Then run the following composer command :
2024
```bash
2125
composer update
2226
```
23-
27+
2428
This will install the latest version of the "sitphp/doubles' library with the required PhpUnit package.
2529

2630
## What is a double
27-
A double is just a class that implements the same methods as the original class (like a copy) except that these methods can be tested and manipulated. The are two types of doubles that you should know about : doubles of type "dummy" and doubles of type "mock" :
31+
32+
A double is just a class that implements the same methods as the original class (like a copy) except that these methods
33+
can be tested and manipulated. The are two types of doubles that you should know about : doubles of type "dummy" and
34+
doubles of type "mock" :
2835

2936
### What is a "dummy" double ?
30-
A double is called a "dummy" when all the methods of the original class are overwritten to return `null`.
31-
For example, let's say your original class looks like this :
37+
38+
A double is called a "dummy" when all the methods of the original class are overwritten to return `null`. For example,
39+
let's say your original class looks like this :
3240

3341
```php
3442
class Name {
@@ -59,9 +67,10 @@ class NameDouble extends Name {
5967

6068
As you can see, every method of the original class is overwritten in the double to return `null`.
6169

62-
### What is a "mock" double ?
63-
A double is called a "mock" when all the methods of the original class are overwritten to behave the same as in the original class.
64-
Here is what a "mock" of our previously created "Name" class would look like :
70+
### What is a "mock" double ?
71+
72+
A double is called a "mock" when all the methods of the original class are overwritten to behave the same as in the
73+
original class. Here is what a "mock" of our previously created "Name" class would look like :
6574

6675
```php
6776
class NameDouble extends Name {
@@ -75,15 +84,21 @@ class NameDouble extends Name {
7584
}
7685
}
7786
```
78-
79-
As you can see, the double will behave exactly the same as the original class. But unlike the original class, it can be tested and manipulated so can use it instead of our original class for our test.
87+
88+
As you can see, the double will behave exactly the same as the original class. But unlike the original class, it can be
89+
tested and manipulated so can use it instead of our original class for our test.
8090

8191
## How doubles can help you with testing ?
82-
Let's say you have two classes : `ClassToTest` and `Foo`. And say that you are using class `Foo` inside class `ClassToTest`. We could then say that class `ClassToTest` is dependent on class `Foo`.
8392

84-
If you wanted to test the methods of class `ClassToTest`, you might not want to be disturbed by class `Foo` which you could test separatly. So you would create a class `Double` which would have all the methods class `Foo` behaving the way you want for your test. You would then use this `Double` class instead of the `Foo` class inside your `ClassToTest` class. You could then test class `ClassToTest` without worrying about if class `Foo` is doing what it is suppose to do or not. You would do something like this :
85-
86-
93+
Let's say you have two classes : `ClassToTest` and `Foo`. And say that you are using class `Foo` inside
94+
class `ClassToTest`. We could then say that class `ClassToTest` is dependent on class `Foo`.
95+
96+
If you wanted to test the methods of class `ClassToTest`, you might not want to be disturbed by class `Foo` which you
97+
could test separatly. So you would create a class `Double` which would have all the methods class `Foo` behaving the way
98+
you want for your test. You would then use this `Double` class instead of the `Foo` class inside your `ClassToTest`
99+
class. You could then test class `ClassToTest` without worrying about if class `Foo` is doing what it is suppose to do
100+
or not. You would do something like this :
101+
87102
```php
88103
/* Double of class "Foo" */
89104
$double = Double::dummy(Foo::class)->getInstance();
@@ -92,11 +107,13 @@ $double = Double::dummy(Foo::class)->getInstance();
92107
$class_to_test = new ClassToTest($double);
93108
```
94109

95-
The "sitphp/doubles" library can generate doubles that look like the original classes but can be manipulated and tested (sort of a copy of a class). These doubles can then be used instead of the original classes for your test. This library can create doubles of any kind of class, interface or trait.
96-
Here are the 3 things that doubles can do for testing :
110+
The "sitphp/doubles" library can generate doubles that look like the original classes but can be manipulated and
111+
tested (sort of a copy of a class). These doubles can then be used instead of the original classes for your test. This
112+
library can create doubles of any kind of class, interface or trait. Here are the 3 things that doubles can do for
113+
testing :
97114

98115
- **The method calls of a double can be counted** : you can test how many times a double method has been called.
99-
- **The method arguments of a double can be tested** : you can test which arguments were passed to a double method.
116+
- **The method arguments of a double can be tested** : you can test which arguments were passed to a double method.
100117
- **The method of a double can be manipulated** : you can overwrite a double method to change its behaviour.
101118

102-
>{.note .info} Note : Doubles are often called "mocks". But in this library, the "mock" word is used to name a special kind of double.
119+
> {.note .info} Note : Doubles are often called "mocks". But in this library, the "mock" word is used to name a special kind of double.
Collapse file

‎doc/02_quick_start.md‎

Copy file name to clipboardExpand all lines: doc/02_quick_start.md
+16-6Lines changed: 16 additions & 6 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Getting started
22

33
## Creating a compatible test case
4-
In order to run PhpUnit test cases with this library, your test class should extend the `Doubles\TestCase` class. This class extends the `PHPUnit\Framework\TestCase` class so you can still use the PhpUnit methods normally. Here is what a test class should look like :
4+
5+
In order to run PhpUnit test cases with this library, your test class should extend the `Doubles\TestCase` class. This
6+
class extends the `PHPUnit\Framework\TestCase` class so you can still use the PhpUnit methods normally. Here is what a
7+
test class should look like :
58

69
```php
710
use \Doubles\TestCase;
@@ -15,14 +18,17 @@ class MyTestClass extends TestCase {
1518

1619
}
1720
```
18-
19-
This will make sure the `Doubles\Double::close()` method is executed after each test. The next step is to create class double of the class we want to test.
2021

22+
This will make sure the `Doubles\Double::close()` method is executed after each test. The next step is to create class
23+
double of the class we want to test.
2124

2225
## Creating a double
2326

2427
### Creating a "dummy" double
25-
A double is called a "dummy" when all the methods of the original class are overwritten to return `null`. If you are not sure what doubles of type "dummy" are, you can get more details on the [introduction page](/doc/intro). To get a "dummy" double, use the `dummy` method class :
28+
29+
A double is called a "dummy" when all the methods of the original class are overwritten to return `null`. If you are not
30+
sure what doubles of type "dummy" are, you can get more details on the [introduction page](/doc/intro). To get a "dummy"
31+
double, use the `dummy` method class :
2632

2733
```php
2834

@@ -40,7 +46,10 @@ class MyTestClass extends TestCase {
4046
```
4147

4248
### Creating a "mock" double
43-
A double is called a "mock" when all the methods of the original class are overwritten to behave the same as in the original class. If you are not sure what doubles of type "mock" are, you can get more details on the [introduction page](/doc/intro). To get a "mock" double instance, use the `mock` method :
49+
50+
A double is called a "mock" when all the methods of the original class are overwritten to behave the same as in the
51+
original class. If you are not sure what doubles of type "mock" are, you can get more details on
52+
the [introduction page](/doc/intro). To get a "mock" double instance, use the `mock` method :
4453

4554
```php
4655
use \Doubles\Double;
@@ -58,7 +67,8 @@ class MyTestClass extends TestCase {
5867

5968
## Testing a double
6069

61-
First use the `Double::_method` method to define which method you are going to test or change. Then use the following methods depending on what you want to do :
70+
First use the `Double::_method` method to define which method you are going to test or change. Then use the following
71+
methods depending on what you want to do :
6272

6373
- To test how many times a double method is called : use the `count` method.
6474
- To test the values of the arguments passed to a double method : use the `args` method.
Collapse file

‎doc/03_creating_doubles.md‎

Copy file name to clipboardExpand all lines: doc/03_creating_doubles.md
+49-24Lines changed: 49 additions & 24 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# Creating a double
22

33
## Creating a double instance
4-
The "sitphp/doubles" library can help you to make doubles of any kind of class, interface or trait easily. It doesn't matter if it has final, abstract or static calls.
4+
5+
The "sitphp/doubles" library can help you to make doubles of any kind of class, interface or trait easily. It doesn't
6+
matter if it has final, abstract or static calls.
57

68
### Double instance of type "dummy"
7-
A doubles of type "dummy" will overwrite the original class methods to return `null`. If you are not sure what doubles of type "dummy" are, you can get more details on the [introduction page](/doc/intro). To create a "dummy" double of a given class use the `dummy` method. :
9+
10+
A doubles of type "dummy" will overwrite the original class methods to return `null`. If you are not sure what doubles
11+
of type "dummy" are, you can get more details on the [introduction page](/doc/intro). To create a "dummy" double of a
12+
given class use the `dummy` method. :
813

914
```php
1015
// Get a double instance of type "dummy" for "MyClass" class, interface or trait
@@ -13,11 +18,15 @@ $my_double = Double::dummy(MyClass::class)->getInstance();
1318
// This will return null
1419
$my_double->myMethod();
1520
```
16-
21+
1722
> {.note .info} Note : The constructor will not be executed so you don't need to pass any of the constructor arguments to the `getInstance` method.
18-
23+
1924
### Double instance of type "mock"
20-
A doubles of type "mock" will overwrite the original class methods to behave exactly the same as in the original class. If you are not sure what doubles of type "mock" are, you can get more details on the [introduction page](/doc/intro). You should use a "mock" double when you want to leave the behaviour of the original class unchanged. To get a "mock" double of a given class use the `mock` method and pass the constructor arguments to the `getInstance` method :
25+
26+
A doubles of type "mock" will overwrite the original class methods to behave exactly the same as in the original class.
27+
If you are not sure what doubles of type "mock" are, you can get more details on the [introduction page](/doc/intro).
28+
You should use a "mock" double when you want to leave the behaviour of the original class unchanged. To get a "mock"
29+
double of a given class use the `mock` method and pass the constructor arguments to the `getInstance` method :
2130

2231
```php
2332
// Get a double instance of type "mock" for "MyClass" class, interface or trait
@@ -27,15 +36,20 @@ $my_double = Double::mock(MyClass::class)->getInstance($arg1, $arg2);
2736
$my_double->myMethod();
2837
```
2938

30-
31-
### Double instance of type "alias"
32-
Doubles of type "alias" can be used to create doubles of non existent classes or to override existent classes. Overriding an existent class might be useful when you cannot inject the double in the class you want to test.You should avoid using "alias" doubles as much as possible because they have drawbacks. :
39+
### Double instance of type "alias"
3340

34-
- An "alias" double will not implement any method from the original class. You will have to define yourself which methods you want to build your "alias" double with.
41+
Doubles of type "alias" can be used to create doubles of non existent classes or to override existent classes.
42+
Overriding an existent class might be useful when you cannot inject the double in the class you want to test.You should
43+
avoid using "alias" doubles as much as possible because they have drawbacks. :
44+
45+
- An "alias" double will not implement any method from the original class. You will have to define yourself which
46+
methods you want to build your "alias" double with.
3547
- All "alias" double implemented methods will return `null` by default an cannot behave like the original class methods.
36-
- If you are using "alias" doubles to replace an existent class, this class should not be loaded before you create your "alias" double. Otherwise you will receive an error .
37-
38-
To get an "alias" double use the `alias` method. Then add the methods you want to implement with the `addMethod` method :
48+
- If you are using "alias" doubles to replace an existent class, this class should not be loaded before you create
49+
your "alias" double. Otherwise you will receive an error .
50+
51+
To get an "alias" double use the `alias` method. Then add the methods you want to implement with the `addMethod`
52+
method :
3953

4054
```php
4155
// Get an double instance of type "alias" for non existent class "MyNonExistentClass" with methods "myMethod" and "myOtherMethod"
@@ -54,19 +68,25 @@ $my_class_to_test->methodUsingMyClass();
5468
```
5569

5670
## Get a double class name
57-
Sometimes, you may need to instantiate the double yourself or you may not need a double instance at all (if you are working with a static class for example). You can get the class name of the generated double only with the `getClass` method.
58-
71+
72+
Sometimes, you may need to instantiate the double yourself or you may not need a double instance at all (if you are
73+
working with a static class for example). You can get the class name of the generated double only with the `getClass`
74+
method.
75+
5976
```php
6077
// Get class name of double of type "dummy" for class "MyClass"
6178
$my_dummy_double_class = Double::dummy(MyClass::class)->getClass();
6279

6380
$my_dummy_double_class::foo();
6481
```
65-
66-
82+
6783
## Implementing undefined methods
68-
When you create a double, it will automatically implements all the methods of the original class (unless you're creating an [alias](#double-instance-of-type-alias) double). If you want your double to implement some methods that are not present in the original class, use the `addMethod` method. If you want to implement a static method, prefix it with the "static:" keyword :
69-
84+
85+
When you create a double, it will automatically implements all the methods of the original class (unless you're creating
86+
an [alias](#double-instance-of-type-alias) double). If you want your double to implement some methods that are not
87+
present in the original class, use the `addMethod` method. If you want to implement a static method, prefix it with
88+
the "static:" keyword :
89+
7090
```php
7191
// Get a double instance with method "myMethod" and static method "myOtherMethod"
7292
$my_double = Double::dummy(MyClass::class)
@@ -75,17 +95,20 @@ $my_double = Double::dummy(MyClass::class)
7595
```
7696

7797
## Passing constructor arguments
98+
7899
You can pass constructor arguments in the getInstance method :
79-
100+
80101
```php
81102
// Get double instance and run original constructor with arguments "first_argument" and "second_argument"
82103
$my_dummy_double = Double::dummy(MyClass::class)->getInstance($arg1, $arg2);
83104
```
84-
105+
85106
> {.note .info} Note : When you pass constructor arguments, the original `__construct` method will be called with the given arguments regardless if your double is of type "dummy" or "mock". In the same way, if you don't pass constructor arguments, the original `__construct` method will not be called regardless if your double is of type "dummy" or "mock".
86107
87108
## Implementing interfaces and trait
88-
Use `addInterface` and/or `addTrait` to declare the interfaces and/or traits you want to implement in your double class :
109+
110+
Use `addInterface` and/or `addTrait` to declare the interfaces and/or traits you want to implement in your double
111+
class :
89112

90113
```php
91114
// Implement one interface
@@ -98,11 +121,13 @@ $my_dummy_double = Double::mock_instance(MyClass::class)
98121
->addInterface([MyInterface::class, MyOtherInterface::class])
99122
->getInstance();
100123
```
101-
124+
102125
> {.note .info} Note : Every double class will automatically implement the `Doubles\DoubleInterface` interface in case you need to identify a double instance.
103126
104-
## Naming the double class
105-
Double class names are automaticaly generated but you can also set the name of the double class yourself with the `setName` method :
127+
## Naming the double class
128+
129+
Double class names are automaticaly generated but you can also set the name of the double class yourself with
130+
the `setName` method :
106131

107132
```php
108133
// Get a double instance of class "MyClass" with class name "MyDoubleClassName".

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.