|
| 1 | +# Exemples d'utilisation avec les fixtures |
| 2 | + |
| 3 | +## Utilisation des fixtures dans les tests |
| 4 | + |
| 5 | +```php |
| 6 | +<?php |
| 7 | + |
| 8 | +use Tests\Fixtures\Groups\GroupsFixture; |
| 9 | +use Tests\Fixtures\Steps\CustomSteps; |
| 10 | +use Grazulex\LaravelFlowpipe\Flowpipe; |
| 11 | + |
| 12 | +class ExampleUsageTest extends TestCase |
| 13 | +{ |
| 14 | + /** @test */ |
| 15 | + public function it_can_use_groups_fixture() |
| 16 | + { |
| 17 | + $result = GroupsFixture::basicGroupFlow(); |
| 18 | + |
| 19 | + expect($result)->toHaveKeys([ |
| 20 | + 'user_id', |
| 21 | + 'action', |
| 22 | + 'validated', |
| 23 | + 'processed', |
| 24 | + 'email_sent', |
| 25 | + 'slack_notified' |
| 26 | + ]); |
| 27 | + } |
| 28 | + |
| 29 | + /** @test */ |
| 30 | + public function it_can_use_nested_flows() |
| 31 | + { |
| 32 | + $result = GroupsFixture::nestedFlowExample(); |
| 33 | + |
| 34 | + expect($result)->toHaveKeys([ |
| 35 | + 'data', |
| 36 | + 'nested_step_1', |
| 37 | + 'nested_step_2', |
| 38 | + 'main_step' |
| 39 | + ]); |
| 40 | + } |
| 41 | + |
| 42 | + /** @test */ |
| 43 | + public function it_can_use_custom_steps() |
| 44 | + { |
| 45 | + $result = Flowpipe::make() |
| 46 | + ->send(['user_id' => 1, 'email' => 'test@example.com']) |
| 47 | + ->through([ |
| 48 | + new AuthenticationStep(['read', 'write']), |
| 49 | + new DataValidationStep(['email' => 'email']), |
| 50 | + new ApiCallStep('/api/users', 'POST'), |
| 51 | + new NotificationStep('email', ['admin@example.com']), |
| 52 | + ]) |
| 53 | + ->thenReturn(); |
| 54 | + |
| 55 | + expect($result)->toHaveKeys([ |
| 56 | + 'authenticated', |
| 57 | + 'is_valid', |
| 58 | + 'api_response', |
| 59 | + 'notification_sent' |
| 60 | + ]); |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Utilisation dans une application réelle |
| 66 | + |
| 67 | +### 1. Configuration des groupes de base |
| 68 | + |
| 69 | +```php |
| 70 | +<?php |
| 71 | + |
| 72 | +// Dans un Service Provider ou au démarrage de l'application |
| 73 | +use Grazulex\LaravelFlowpipe\Flowpipe; |
| 74 | +use Tests\Fixtures\Steps\CustomSteps; |
| 75 | + |
| 76 | +// Groupe d'authentification |
| 77 | +Flowpipe::group('auth', [ |
| 78 | + new AuthenticationStep(['read']), |
| 79 | + new LoggingStep('info', 'User authentication checked'), |
| 80 | +]); |
| 81 | + |
| 82 | +// Groupe de validation |
| 83 | +Flowpipe::group('validation', [ |
| 84 | + new DataValidationStep([ |
| 85 | + 'email' => 'email', |
| 86 | + 'name' => 'required', |
| 87 | + 'age' => 'numeric' |
| 88 | + ]), |
| 89 | + new LoggingStep('info', 'Data validation completed'), |
| 90 | +]); |
| 91 | + |
| 92 | +// Groupe de traitement |
| 93 | +Flowpipe::group('processing', [ |
| 94 | + new DatabaseStep('insert', 'users'), |
| 95 | + new CacheStep('user_data', 3600), |
| 96 | + new LoggingStep('info', 'User data processed'), |
| 97 | +]); |
| 98 | + |
| 99 | +// Groupe de notifications |
| 100 | +Flowpipe::group('notifications', [ |
| 101 | + new NotificationStep('email', ['admin@example.com']), |
| 102 | + new NotificationStep('slack', ['#general']), |
| 103 | + new LoggingStep('info', 'Notifications sent'), |
| 104 | +]); |
| 105 | +``` |
| 106 | + |
| 107 | +### 2. Utilisation dans un contrôleur |
| 108 | + |
| 109 | +```php |
| 110 | +<?php |
| 111 | + |
| 112 | +namespace App\Http\Controllers; |
| 113 | + |
| 114 | +use Illuminate\Http\Request; |
| 115 | +use Grazulex\LaravelFlowpipe\Flowpipe; |
| 116 | +use Tests\Fixtures\Steps\CustomSteps; |
| 117 | + |
| 118 | +class UserController extends Controller |
| 119 | +{ |
| 120 | + public function store(Request $request) |
| 121 | + { |
| 122 | + $result = Flowpipe::make() |
| 123 | + ->send($request->all()) |
| 124 | + ->useGroup('auth') |
| 125 | + ->useGroup('validation') |
| 126 | + ->nested([ |
| 127 | + new FileOperationStep('upload', '/tmp/avatar.jpg'), |
| 128 | + new ApiCallStep('/api/external/notify', 'POST'), |
| 129 | + ]) |
| 130 | + ->useGroup('processing') |
| 131 | + ->useGroup('notifications') |
| 132 | + ->thenReturn(); |
| 133 | + |
| 134 | + if (isset($result['error'])) { |
| 135 | + return response()->json(['error' => $result['error']], 400); |
| 136 | + } |
| 137 | + |
| 138 | + return response()->json($result); |
| 139 | + } |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +### 3. Workflow complexe avec gestion d'erreurs |
| 144 | + |
| 145 | +```php |
| 146 | +<?php |
| 147 | + |
| 148 | +$result = Flowpipe::make() |
| 149 | + ->send(['order_id' => 123]) |
| 150 | + ->through([ |
| 151 | + new ErrorHandlingStep(false, [ |
| 152 | + function($error, $payload) { |
| 153 | + Log::error('Order processing error', ['error' => $error, 'payload' => $payload]); |
| 154 | + } |
| 155 | + ]), |
| 156 | + 'auth', |
| 157 | + 'validation', |
| 158 | + ]) |
| 159 | + ->nested([ |
| 160 | + // Traitement de paiement |
| 161 | + new ApiCallStep('/api/payments/process', 'POST'), |
| 162 | + new DatabaseStep('update', 'orders', ['status' => 'paid']), |
| 163 | + ]) |
| 164 | + ->through([ |
| 165 | + // Mise à jour de l'inventaire |
| 166 | + new DatabaseStep('update', 'inventory'), |
| 167 | + new CacheStep('inventory_status', 1800), |
| 168 | + ]) |
| 169 | + ->useGroup('notifications') |
| 170 | + ->thenReturn(); |
| 171 | +``` |
| 172 | + |
| 173 | +### 4. Utilisation avec mise en cache |
| 174 | + |
| 175 | +```php |
| 176 | +<?php |
| 177 | + |
| 178 | +$result = Flowpipe::make() |
| 179 | + ->send(['product_id' => 456]) |
| 180 | + ->through([ |
| 181 | + new CacheStep('product_details', 3600), |
| 182 | + new AuthenticationStep(['read']), |
| 183 | + ]) |
| 184 | + ->nested([ |
| 185 | + new ApiCallStep('/api/products/details', 'GET'), |
| 186 | + new DataValidationStep(['price' => 'numeric']), |
| 187 | + ]) |
| 188 | + ->through([ |
| 189 | + new DatabaseStep('select', 'products'), |
| 190 | + new FileOperationStep('read', '/storage/product_images'), |
| 191 | + ]) |
| 192 | + ->thenReturn(); |
| 193 | +``` |
| 194 | + |
| 195 | +### 5. Workflow de test avec groupes avancés |
| 196 | + |
| 197 | +```php |
| 198 | +<?php |
| 199 | + |
| 200 | +use Tests\Fixtures\Groups\AdvancedGroupsFixture; |
| 201 | + |
| 202 | +// Configuration des groupes avancés |
| 203 | +AdvancedGroupsFixture::setupAdvancedGroups(); |
| 204 | + |
| 205 | +$result = Flowpipe::make() |
| 206 | + ->send(['test_data' => 'value']) |
| 207 | + ->through([ |
| 208 | + 'advanced-validation', |
| 209 | + 'advanced-processing', |
| 210 | + ]) |
| 211 | + ->nested([ |
| 212 | + new LoggingStep('debug', 'Nested processing started'), |
| 213 | + new CacheStep('test_cache', 300), |
| 214 | + new LoggingStep('debug', 'Nested processing completed'), |
| 215 | + ]) |
| 216 | + ->thenReturn(); |
| 217 | +``` |
| 218 | + |
| 219 | +## Avantages des fixtures |
| 220 | + |
| 221 | +1. **Réutilisabilité** : Les fixtures peuvent être utilisées dans plusieurs tests |
| 222 | +2. **Consistance** : Garantit que tous les tests utilisent les mêmes données |
| 223 | +3. **Maintenabilité** : Un seul endroit pour modifier la configuration des tests |
| 224 | +4. **Simplicité** : Simplifie l'écriture et la lecture des tests |
| 225 | + |
| 226 | +## Bonnes pratiques |
| 227 | + |
| 228 | +1. **Organisation** : Séparez les fixtures par domaine (Groups, Steps, etc.) |
| 229 | +2. **Nommage** : Utilisez des noms explicites pour les méthodes de fixture |
| 230 | +3. **Documentation** : Commentez les fixtures complexes |
| 231 | +4. **Réutilisation** : Créez des fixtures génériques réutilisables |
| 232 | +5. **Isolation** : Chaque fixture doit être indépendante des autres |
0 commit comments