├── .github ├── FUNDING.yml └── workflows │ └── tests.yml ├── LICENSE.md ├── README.md ├── autoload.php ├── composer.json ├── phpstan.neon.dist └── src ├── Contracts └── Framework.php ├── Frameworks ├── ParaTest.php ├── Pest.php └── PhpUnit.php └── Runner.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: nunomaduro 4 | patreon: nunomaduro 5 | custom: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=66BYDWAT92N6L 6 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: tests 2 | 3 | on: [ 'push', 'pull_request' ] 4 | 5 | jobs: 6 | tests: 7 | runs-on: ubuntu-latest 8 | strategy: 9 | fail-fast: true 10 | matrix: 11 | php: ['7.3', '7.4', '8.0', '8.1', '8.2', '8.3'] 12 | 13 | name: PHP ${{ matrix.php }} 14 | 15 | steps: 16 | - name: Checkout 17 | uses: actions/checkout@v2 18 | 19 | - name: Setup PHP 20 | uses: shivammathur/setup-php@v2 21 | with: 22 | php-version: ${{ matrix.php }} 23 | tools: composer:v2 24 | coverage: none 25 | 26 | - name: Setup Problem Matchers 27 | run: | 28 | echo "::add-matcher::${{ runner.tool_cache }}/php.json" 29 | echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" 30 | 31 | - name: Install PHP 7 dependencies 32 | run: composer update --no-interaction --prefer-dist --no-progress 33 | if: "matrix.php < 8" 34 | 35 | - name: Install PHP 8 dependencies 36 | run: composer update --no-interaction --prefer-dist --no-progress --ignore-platform-req=php 37 | if: "matrix.php >= 8" 38 | 39 | - name: Run Unit Tests 40 | run: vendor/bin/phpunit 41 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Nuno Maduro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Mock final classes example 3 |

4 | 5 |

6 | Build Status 7 | Total Downloads 8 | Latest Version 9 | License 10 |

11 | 12 |

13 | Created by, and is maintained by Nuno Maduro, and once installed it automatically allows mocking of final methods and classes. 14 |

15 | 16 | ## 💡 Installation & Usage 17 | 18 | > **Requires [PHP 7.1+](https://php.net/releases/)** 19 | 20 | First, install the package using [Composer](https://getcomposer.org): 21 | 22 | ```bash 23 | composer require --dev nunomaduro/mock-final-classes 24 | ``` 25 | 26 | **That's it! You can now mock final methods and classes.** 🏄‍♂️ 27 | 28 | ## 🤯 How it works? 29 | 30 | 1. First, we run the file [autoload.php](https://github.com/nunomaduro/mock-final-classes/blob/master/autoload.php) using [Composer Autoload](https://github.com/nunomaduro/mock-final-classes/blob/8628de25120b6106421d7730457c45ac668ecef9/composer.json#L35). 31 | 2. Then, we determine if you are running a supported test framework 32 | - [ParaTest](https://github.com/nunomaduro/mock-final-classes/blob/master/src/Frameworks/ParaTest.php) 33 | - [Pest](https://github.com/nunomaduro/mock-final-classes/blob/master/src/Frameworks/Pest.php) 34 | - [PHPUnit](https://github.com/nunomaduro/mock-final-classes/blob/master/src/Frameworks/PhpUnit.php) 35 | 3. Then, we use the library [dg/bypass-finals](https://github.com/dg/bypass-finals) to remove final keywords from source code on-the-fly: [https://github.com/nunomaduro/mock-final-classes/src/Runner.php#L31](https://github.com/nunomaduro/mock-final-classes/blob/8628de25120b6106421d7730457c45ac668ecef9/src/Runner.php#L31). 36 | 37 | ## 👏🏻 Credits 38 | 39 | Most of the work is done by the library [dg/bypass-finals](https://github.com/dg/bypass-finals), so make sure you support the maintainer here: [Donate](https://nette.org/make-donation?to=bypass-finals). Thank you! 40 | 41 | --- 42 | 43 | Mock Final Classes is an open-sourced software licensed under the [MIT license](LICENSE.md). 44 | -------------------------------------------------------------------------------- /autoload.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | private static $frameworks = [ 20 | Frameworks\PhpUnit::class, 21 | Frameworks\Pest::class, 22 | Frameworks\ParaTest::class, 23 | ]; 24 | 25 | /** 26 | * Removes the `final` keyword if a 27 | * testing framework is running. 28 | */ 29 | public static function run(): void 30 | { 31 | foreach (self::$frameworks as $framework) { 32 | if ((new $framework())->isRunning()) { 33 | BypassFinals::enable(); 34 | break; 35 | } 36 | } 37 | } 38 | } 39 | --------------------------------------------------------------------------------