├── .editorconfig ├── .php_cs ├── CHANGELOG.md ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist └── src ├── SnapshotAssertions.php └── SnapshotsManager.php /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_size = 4 6 | indent_style = space 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true -------------------------------------------------------------------------------- /.php_cs: -------------------------------------------------------------------------------- 1 | setRiskyAllowed(true) 9 | ->setRules([ 10 | '@Symfony' => true, 11 | 'array_syntax' => ['syntax' => 'short'], 12 | 'combine_consecutive_unsets' => true, 13 | 'dir_constant' => true, 14 | 'ereg_to_preg' => true, 15 | 'linebreak_after_opening_tag' => true, 16 | 'mb_str_functions' => true, 17 | 'modernize_types_casting' => true, 18 | 'no_multiline_whitespace_before_semicolons' => true, 19 | 'no_php4_constructor' => true, 20 | 'no_short_echo_tag' => true, 21 | 'no_useless_else' => true, 22 | 'no_useless_return' => true, 23 | 'ordered_imports' => true, 24 | 'phpdoc_order' => true, 25 | 'psr0' => true, 26 | 'psr4' => true, 27 | 'semicolon_after_instruction' => true, 28 | 'simplified_null_return' => true, 29 | 'strict_comparison' => true, 30 | 'strict_param' => true, 31 | ]) 32 | ->setFinder(Finder::create()->in(['src', 'tests'])); 33 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # CHANGELOG 2 | 3 | ## 0.1.2 - 2016-12-09 4 | ### Fixed 5 | - Fixed snapshot being purged between tests when in `--update` mode 6 | 7 | ## 0.1.1 - 2016-12-08 8 | ### Added 9 | - Added ability to add an identifier to a snapshot 10 | 11 | ### Fixed 12 | - Fixed some edge-cases when generating snapshots 13 | 14 | ## 0.1.0 - 2016-12-08 15 | ### Added 16 | - Initial release 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Madewithlove 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | 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, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHPUnit Snapshots 2 | 3 | [![Build Status](http://img.shields.io/travis/madewithlove/phpunit-snapshots.svg?style=flat-square)](https://travis-ci.org/madewithlove/phpunit-snapshots) 4 | [![Latest Stable Version](http://img.shields.io/packagist/v/madewithlove/phpunit-snapshots.svg?style=flat-square)](https://packagist.org/packages/madewithlove/phpunit-snapshots) 5 | [![Total Downloads](http://img.shields.io/packagist/dt/madewithlove/phpunit-snapshots.svg?style=flat-square)](https://packagist.org/packages/madewithlove/phpunit-snapshots) 6 | 7 | This trait allows you to use [Jest-like](https://facebook.github.io/jest/) snapshot testing in your PHPUnit tests. 8 | 9 | It is a very basic trait and is only meant to snapshot JSON-encodable structures, not complex objects and such. 10 | 11 | ## Installation 12 | 13 | ```bash 14 | composer require madewithlove/phpunit-snapshots 15 | ``` 16 | 17 | ## Usage 18 | 19 | ### Using snapshots in tests 20 | 21 | Simply call the assertion on any encodable result (the result of a function, a variable, etc.). 22 | You can pass an identifier as second argument which will be used as title of the snapshot in the snapshot file. 23 | 24 | ```php 25 | assertEqualsSnapshot($this->someComplexOperation()); 33 | $this->assertEqualsSnapshot($this->someComplexOperation(), 'Compute something'); 34 | } 35 | } 36 | ``` 37 | 38 | This will generate a snapshot if we didn't have one for this test, else it will assert that the current results match the ones in the snapshot. 39 | 40 | ### Updating all snapshots 41 | 42 | You can update all snapshots in your tests by running the following: 43 | 44 | ```bash 45 | $ phpunit -d --update 46 | ``` 47 | 48 | ## Testing 49 | 50 | ``` bash 51 | $ composer test 52 | ``` 53 | 54 | ## License 55 | 56 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 57 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "madewithlove/phpunit-snapshots", 3 | "description": "Snaphot testing for PHPUnit", 4 | "license": "MIT", 5 | "keywords": ["phpunit", "snapshot"], 6 | "authors": [ 7 | { 8 | "name": "Madewithlove", 9 | "email": "heroes@madewithlove.be" 10 | } 11 | ], 12 | "autoload": { 13 | "psr-4": { 14 | "Madewithlove\\PhpunitSnapshots\\": "src" 15 | } 16 | }, 17 | "autoload-dev": { 18 | "psr-4": { 19 | "Madewithlove\\PhpunitSnapshots\\": "tests" 20 | } 21 | }, 22 | "require": {}, 23 | "require-dev": { 24 | "friendsofphp/php-cs-fixer": "^2.0", 25 | "phpunit/phpunit": "^5.7", 26 | "symfony/var-dumper": "^3.2" 27 | }, 28 | "scripts": { 29 | "test": "phpunit", 30 | "lint": "php-cs-fixer fix" 31 | }, 32 | "config": { 33 | "sort-packages": true 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | tests 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/SnapshotAssertions.php: -------------------------------------------------------------------------------- 1 | assertEquals($snapshot, $expected, $message); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/SnapshotsManager.php: -------------------------------------------------------------------------------- 1 | getName(); 111 | static::$assertionsInTest[$methodName] = isset(static::$assertionsInTest[$methodName]) 112 | ? static::$assertionsInTest[$methodName] 113 | : -1; 114 | 115 | $name = $methodName.'-'.++static::$assertionsInTest[$methodName]; 116 | $name = $identifier ? $name.': '.$identifier : $name; 117 | 118 | return $name; 119 | } 120 | 121 | //////////////////////////////////////////////////////////////////////////////// 122 | /////////////////////////////////// CONTEXT //////////////////////////////////// 123 | //////////////////////////////////////////////////////////////////////////////// 124 | 125 | /** 126 | * @return string 127 | */ 128 | public static function getTestSuitePath() 129 | { 130 | return (new ReflectionClass(static::$suite))->getFileName(); 131 | } 132 | 133 | /** 134 | * Check if PHPUnit is running in update mode. 135 | * 136 | * @return bool 137 | */ 138 | public static function isUpdate() 139 | { 140 | return in_array('--update', $_SERVER['argv'], true); 141 | } 142 | 143 | //////////////////////////////////////////////////////////////////////////////// 144 | //////////////////////////////// SERIALIZATION ///////////////////////////////// 145 | //////////////////////////////////////////////////////////////////////////////// 146 | 147 | /** 148 | * @param mixed $something 149 | * 150 | * @return string 151 | */ 152 | protected static function serialize($something) 153 | { 154 | return json_encode($something, JSON_PRETTY_PRINT); 155 | } 156 | 157 | /** 158 | * @param mixed $something 159 | * 160 | * @return mixed 161 | */ 162 | protected static function unserialize($something) 163 | { 164 | return json_decode($something, true); 165 | } 166 | } 167 | --------------------------------------------------------------------------------