最新服务器上的版本,以后用这个
wangzhenxin
2023-11-19 bc164b8bdbfbdf1d8229a5ced6b08d7cb8db7361
commit | author | age
2207d6 1 # Codeception\Stub
W 2
3 [![Build Status](https://travis-ci.org/Codeception/Stub.svg?branch=master)](https://travis-ci.org/Codeception/Stub)
4 [![Latest Stable Version](https://poser.pugx.org/codeception/stub/v/stable)](https://packagist.org/packages/codeception/stub)
5 [![Total Downloads](https://poser.pugx.org/codeception/stub/downloads)](https://packagist.org/packages/codeception/stub)
6 [![License](https://poser.pugx.org/codeception/stub/license)](https://packagist.org/packages/codeception/stub)
7
8 Library on top of PHPUnit's mock builder providing a highly simplified syntax:
9
10 ## Reference
11
12 * [Stub](https://github.com/Codeception/Stub/blob/master/docs/Stub.md) - creating stub classes using static methods
13 * [Stub Trait](https://github.com/Codeception/Stub/blob/master/docs/StubTrait.md) - creating stubs and mocks using trait
14 * [Expected](https://github.com/Codeception/Stub/blob/master/docs/Expected.md) - defining expectations for mocks
15
16 ## Install
17
18 Enabled by default in Codeception.
19 For PHPUnit install this package:
20
21 ```
22 composer require codeception/stub --dev
23 ```
24
25 ## Stubs
26
27 Stubs can be constructed with `Codeception\Stub` static calls:
28
29 ```php
30 <?php
31 // create a stub with find method replaced
32 $userRepository = Stub::make(UserRepository::class, ['find' => new User]);
33 $userRepository->find(1); // => User
34
35 // create a dummy
36 $userRepository = Stub::makeEmpty(UserRepository::class);
37
38 // create a stub with all methods replaced except one
39 $user = Stub::makeEmptyExcept(User::class, 'validate');
40 $user->validate($data);
41
42 // create a stub by calling constructor and replacing a method
43 $user = Stub::construct(User::class, ['name' => 'davert'], ['save' => false]);
44
45 // create a stub by calling constructor with empty methods
46 $user = Stub::constructEmpty(User::class, ['name' => 'davert']);
47
48 // create a stub by calling constructor with empty methods
49 $user = Stub::constructEmptyExcept(User::class, 'getName', ['name' => 'davert']);
50 $user->getName(); // => davert
51 $user->setName('jane'); // => this method is empty
52 $user->getName(); // => davert 
53 ```
54
55 [See complete reference](https://github.com/Codeception/Stub/blob/master/docs/Stub.md)
56
57 Alternatively, stubs can be created by using [`Codeception\Test\Feature\Stub` trait](https://github.com/Codeception/Stub/blob/master/docs/StubTrait.md):
58
59 ```php
60 <?php
61 $this->make(UserRepositry::class);
62 $this->makeEmpty(UserRepositry::class);
63 $this->construct(UserRepositry::class);
64 $this->constructEmpty(UserRepositry::class);
65 // ...
66 ```
67
68 ## Mocks
69
70 Mocks should be created by including [`Codeception\Test\Feature\Stub` trait](https://github.com/Codeception/Stub/blob/master/docs/StubTrait.md) into a test case.
71 Execution expectation are set with [`Codescption\Stub\Expected`](https://github.com/Codeception/Stub/blob/master/docs/Expected.md):
72
73 ```php
74 <?php
75 // find should be never called
76 $userRepository = $this->make(UserRepository::class, [
77     'find' => Codeception\Stub\Expected::never()
78 ]);
79
80 // find should be called once and return a new user
81 $userRepository = $this->make(UserRepository::class, [
82     'find' => Codeception\Stub\Expected::once(new User)
83 ]);
84 ```
85
86
87 ## License 
88
89 MIT