From 347ee5c490b8737eb37baef709d07753b34a06de Mon Sep 17 00:00:00 2001 From: vgrams Date: Sun, 30 Aug 2026 17:59:01 +0600 Subject: [PATCH] test: extract file save mocking into trait helpers - add `mockFileSave()`, which builds the origin structure path and mocks `file_put_contents` with the expected fixture, replacing the six-line arrange block in all 32 saving tests - add `expectNoFileSave()` and call it in the 20 tests that assert an exception and still call `save()`, so a builder that stops throwing fails the test instead of silently overwriting the origin structure - drop `callFilePutContent()`: no test ever passed its `$flags` argument and `PHPFileBuilder::save()` calls `file_put_contents()` with two args Co-Authored-By: Claude Opus 5 refs: https://github.com/RonasIT/larabuilder/issues/80 --- tests/AppBootstrapBuilderTest.php | 23 +- tests/PHPFileBuilderTest.php | 221 ++++-------------- .../Traits/PHPFileBuilderTestMockTrait.php | 17 +- 3 files changed, 67 insertions(+), 194 deletions(-) diff --git a/tests/AppBootstrapBuilderTest.php b/tests/AppBootstrapBuilderTest.php index 6945e47..88a3b7d 100644 --- a/tests/AppBootstrapBuilderTest.php +++ b/tests/AppBootstrapBuilderTest.php @@ -16,12 +16,7 @@ class AppBootstrapBuilderTest extends TestCase public function testAddExceptionsRenderEmpty(): void { - $file = $this->generateOriginalStructurePath('bootstrap_empty.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'bootstrap_empty.php'), - ); + $file = $this->mockFileSave('bootstrap_empty.php'); new AppBootstrapBuilder($file) ->addExceptionsRender( @@ -40,12 +35,7 @@ public function testAddExceptionsRenderEmpty(): void public function testAddExceptionsRenderCustom(): void { - $file = $this->generateOriginalStructurePath('bootstrap_configured.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'bootstrap_configured.php'), - ); + $file = $this->mockFileSave('bootstrap_configured.php'); new AppBootstrapBuilder($file) ->addExceptionsRender( @@ -68,12 +58,7 @@ public function testAddExceptionsRenderCustom(): void public function testAddExceptionsRenderExist(): void { - $file = $this->generateOriginalStructurePath('bootstrap_with_render.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'bootstrap_with_render.php'), - ); + $file = $this->mockFileSave('bootstrap_with_render.php'); new AppBootstrapBuilder($file) ->addExceptionsRender( @@ -88,6 +73,7 @@ public function testAddExceptionsRenderInvalidBody() { $file = $this->generateOriginalStructurePath('bootstrap_configured.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew( expectedClassName: InvalidPHPCodeException::class, expectedMessage: 'Cannot parse provided code: \'return ($request->expectsJson()\'.', @@ -128,6 +114,7 @@ public function testInvalidBootstrapAppFileException(string $fixture, string $ty { $file = $this->generateOriginalStructurePath($fixture); + $this->expectNoFileSave(); $this->assertExceptionThrew(InvalidBootstrapAppFileException::class, "Bootstrap app file must not contain {$type} declarations"); new AppBootstrapBuilder($file) diff --git a/tests/PHPFileBuilderTest.php b/tests/PHPFileBuilderTest.php index cd19ce2..ce2f205 100644 --- a/tests/PHPFileBuilderTest.php +++ b/tests/PHPFileBuilderTest.php @@ -24,12 +24,7 @@ class PHPFileBuilderTest extends TestCase public function testSetProperty(): void { - $file = $this->generateOriginalStructurePath('class_with_properties.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_with_properties.php'), - ); + $file = $this->mockFileSave('class_with_properties.php'); new PHPFileBuilder($file) ->setProperty('intProperty', 1.23) @@ -49,12 +44,7 @@ public function testSetProperty(): void public function testSetPropertyWithoutExistingProperties(): void { - $file = $this->generateOriginalStructurePath('class.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_with_new_properties.php'), - ); + $file = $this->mockFileSave('class.php', 'class_with_new_properties.php'); new PHPFileBuilder($file) ->setProperty('newString', 'some string') @@ -66,6 +56,7 @@ public function testSetPropertyNotClassTrait(): void { $file = $this->generateOriginalStructurePath('enum.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(InvalidStructureTypeException::class, "'SetProperty' operation may only be applied to: Class, Trait."); new PHPFileBuilder($file) @@ -75,12 +66,7 @@ public function testSetPropertyNotClassTrait(): void public function testAddArrayPropertyItem(): void { - $file = $this->generateOriginalStructurePath('class_with_properties.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_with_array_properties.php'), - ); + $file = $this->mockFileSave('class_with_properties.php', 'class_with_array_properties.php'); new PHPFileBuilder($file) ->addArrayPropertyItem('fillable', 'age') @@ -98,6 +84,7 @@ public function testAddArrayPropertyItemThrowsException(): void { $file = $this->generateOriginalStructurePath('class_with_properties.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(UnexpectedPropertyTypeException::class, "Property 'boolProperty' has unexpected type. Expected 'array', actual 'bool'."); new PHPFileBuilder($file) @@ -109,6 +96,7 @@ public function testAddArrayPropertyItemNotClassTrait(): void { $file = $this->generateOriginalStructurePath('enum.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(InvalidStructureTypeException::class, "'AddArrayPropertyItem' operation may only be applied to: Class, Trait."); new PHPFileBuilder($file) @@ -127,12 +115,7 @@ public function testInvalidPhpFileThrowsException(): void public function testSetPropertyInTrait(): void { - $file = $this->generateOriginalStructurePath('trait.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'trait.php'), - ); + $file = $this->mockFileSave('trait.php'); new PHPFileBuilder($file) ->setProperty('floatProperty', 56) @@ -146,12 +129,7 @@ public function testSetPropertyInTrait(): void public function testRemoveArrayPropertyItem(): void { - $file = $this->generateOriginalStructurePath('class_with_properties.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_with_array_properties_removed.php'), - ); + $file = $this->mockFileSave('class_with_properties.php', 'class_with_array_properties_removed.php'); new PHPFileBuilder($file) ->removeArrayPropertyItem('fillable', ['name', 'age']) @@ -169,6 +147,7 @@ public function testRemoveArrayPropertyItemThrowsException(): void { $file = $this->generateOriginalStructurePath('class_with_properties.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(UnexpectedPropertyTypeException::class, "Property 'boolProperty' has unexpected type. Expected 'array', actual 'bool'."); new PHPFileBuilder($file) @@ -178,12 +157,7 @@ public function testRemoveArrayPropertyItemThrowsException(): void public function testRemoveArrayPropertyItemNoProperty(): void { - $file = $this->generateOriginalStructurePath('class.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_unchanged.php'), - ); + $file = $this->mockFileSave('class.php', 'class_unchanged.php'); new PHPFileBuilder($file) ->removeArrayPropertyItem('notProperty', ['value']) @@ -194,6 +168,7 @@ public function testRemoveArrayPropertyUnexpectedPropertyExceptionNull(): void { $file = $this->generateOriginalStructurePath('class_with_properties.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(UnexpectedPropertyTypeException::class, "Property 'nullProperty' has unexpected type. Expected 'array', actual 'null'."); new PHPFileBuilder($file) @@ -205,6 +180,7 @@ public function testRemoveArrayPropertyNotClassTrait(): void { $file = $this->generateOriginalStructurePath('enum.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(InvalidStructureTypeException::class, "'RemoveArrayPropertyItem' operation may only be applied to: Class, Trait."); new PHPFileBuilder($file) @@ -233,12 +209,7 @@ public static function provideAddImportsFiles(): array #[DataProvider('provideAddImportsFiles')] public function testAddImports(string $fixture): void { - $file = $this->generateOriginalStructurePath($fixture); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, "add_imports_to_{$fixture}"), - ); + $file = $this->mockFileSave($fixture, "add_imports_to_{$fixture}"); new PHPFileBuilder($file) ->addImports([ @@ -251,12 +222,7 @@ public function testAddImports(string $fixture): void public function testAddImportsEmptyList(): void { - $file = $this->generateOriginalStructurePath('class.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_unchanged.php'), - ); + $file = $this->mockFileSave('class.php', 'class_unchanged.php'); new PHPFileBuilder($file) ->addImports([]) @@ -265,12 +231,7 @@ public function testAddImportsEmptyList(): void public function testAddImportsAlreadyImported(): void { - $file = $this->generateOriginalStructurePath('class.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_unchanged.php'), - ); + $file = $this->mockFileSave('class.php', 'class_unchanged.php'); new PHPFileBuilder($file) ->addImports(['RonasIT\Larabuilder\Tests\Support\FirstClass']) @@ -279,12 +240,7 @@ public function testAddImportsAlreadyImported(): void public function testAddImportsToFileWithoutNamespace(): void { - $file = $this->generateOriginalStructurePath('empty_php_file.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'empty_php_file.php'), - ); + $file = $this->mockFileSave('empty_php_file.php'); new PHPFileBuilder($file) ->addImports(['RonasIT\Larabuilder\Tests\Support\FirstClass']) @@ -309,12 +265,7 @@ public static function provideAddTraitsFiles(): array #[DataProvider('provideAddTraitsFiles')] public function testAddTraits(string $fixture): void { - $file = $this->generateOriginalStructurePath($fixture); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, "add_traits_to_{$fixture}"), - ); + $file = $this->mockFileSave($fixture, "add_traits_to_{$fixture}"); new PHPFileBuilder($file) ->addTraits([ @@ -327,12 +278,7 @@ public function testAddTraits(string $fixture): void public function testAddTraitsWithoutChanges(): void { - $file = $this->generateOriginalStructurePath('class.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_unchanged.php'), - ); + $file = $this->mockFileSave('class.php', 'class_unchanged.php'); new PHPFileBuilder($file) ->addTraits([]) @@ -342,12 +288,7 @@ public function testAddTraitsWithoutChanges(): void public function testAddTraitsWithDoubleCalls(): void { - $file = $this->generateOriginalStructurePath('class.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'add_traits_to_class.php'), - ); + $file = $this->mockFileSave('class.php', 'add_traits_to_class.php'); new PHPFileBuilder($file) ->addTraits([ @@ -364,6 +305,7 @@ public function testAddTraitsNotClassTraitEnum(): void { $file = $this->generateOriginalStructurePath('interface.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(InvalidStructureTypeException::class, "'AddTraits' operation may only be applied to: Class, Trait, Enum."); new PHPFileBuilder($file) @@ -375,12 +317,7 @@ public function testAddTraitsNotClassTraitEnum(): void public function testInsertCodeToMethodToTheEndPosition(): void { - $file = $this->generateOriginalStructurePath('class_with_properties.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_with_method_code_added.php'), - ); + $file = $this->mockFileSave('class_with_properties.php', 'class_with_method_code_added.php'); new PHPFileBuilder($file) ->insertCodeToMethod('__construct', $this->getFixture('sample_code.php')) @@ -389,12 +326,7 @@ public function testInsertCodeToMethodToTheEndPosition(): void public function testInsertCodeToMethodToTheStartPosition(): void { - $file = $this->generateOriginalStructurePath('class_with_properties.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_with_method_code.php'), - ); + $file = $this->mockFileSave('class_with_properties.php', 'class_with_method_code.php'); new PHPFileBuilder($file) ->insertCodeToMethod('__construct', '$this->name = $name;', InsertPositionEnum::Start) @@ -403,12 +335,7 @@ public function testInsertCodeToMethodToTheStartPosition(): void public function testInsertCodeToTraitMethod(): void { - $file = $this->generateOriginalStructurePath('trait.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'trait_with_method_code_added.php'), - ); + $file = $this->mockFileSave('trait.php', 'trait_with_method_code_added.php'); new PHPFileBuilder($file) ->insertCodeToMethod('method1', $this->getFixture('sample_code.php'), InsertPositionEnum::Start) @@ -417,12 +344,7 @@ public function testInsertCodeToTraitMethod(): void public function testInsertCodeToEnum(): void { - $file = $this->generateOriginalStructurePath('enum.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'add_imports_to_enum_code_added.php'), - ); + $file = $this->mockFileSave('enum.php', 'add_imports_to_enum_code_added.php'); new PHPFileBuilder($file) ->insertCodeToMethod('toArray', $this->getFixture('sample_code.php'), InsertPositionEnum::Start) @@ -433,6 +355,7 @@ public function testInsertCodeToMethodNotExists(): void { $file = $this->generateOriginalStructurePath('class.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(NodeNotExistException::class, "Method 'noMethod' does not exist."); new PHPFileBuilder($file) @@ -442,12 +365,7 @@ public function testInsertCodeToMethodNotExists(): void public function testInsertCodeToMethodEmptyString(): void { - $file = $this->generateOriginalStructurePath('class_with_properties.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_with_properties_unchanged.php'), - ); + $file = $this->mockFileSave('class_with_properties.php', 'class_with_properties_unchanged.php'); new PHPFileBuilder($file) ->insertCodeToMethod('someMethod', '') @@ -458,6 +376,7 @@ public function testInsertCodeToMethodInvalidCode(): void { $file = $this->generateOriginalStructurePath('class.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(InvalidPHPCodeException::class, 'Cannot parse provided code: \'$this->name\'.'); new PHPFileBuilder($file) @@ -469,6 +388,7 @@ public function testInsertCodeToMethodNotClassTraitEnum(): void { $file = $this->generateOriginalStructurePath('interface.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(InvalidStructureTypeException::class, "'InsertCodeToMethod' operation may only be applied to: Class, Trait, Enum."); new PHPFileBuilder($file) @@ -480,6 +400,7 @@ public function testInsertCodeToMethodWhenMethodNotExist(): void { $file = $this->generateOriginalStructurePath('class_with_properties.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(NodeNotExistException::class, "Method 'noMethod' does not exist."); new PHPFileBuilder($file) @@ -524,12 +445,7 @@ public static function provideInsertDuplicateCode(): array #[DataProvider('provideInsertDuplicateCode')] public function testInsertDuplicateCodeToMethod(string $code): void { - $file = $this->generateOriginalStructurePath('class.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_unchanged.php'), - ); + $file = $this->mockFileSave('class.php', 'class_unchanged.php'); new PHPFileBuilder($file) ->insertCodeToMethod('someMethod', $code) @@ -538,12 +454,7 @@ public function testInsertDuplicateCodeToMethod(string $code): void public function testAddMethod(): void { - $file = $this->generateOriginalStructurePath('class_empty.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_with_added_method.php'), - ); + $file = $this->mockFileSave('class_empty.php', 'class_with_added_method.php'); new PHPFileBuilder($file) ->addMethod( @@ -589,6 +500,7 @@ public function testAddMethodAlreadyExists(): void { $file = $this->generateOriginalStructurePath('class.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(NodeAlreadyExistsException::class, "Method 'someMethod' already exists."); new PHPFileBuilder($file) @@ -598,12 +510,7 @@ public function testAddMethodAlreadyExists(): void public function testAddStaticMethod(): void { - $file = $this->generateOriginalStructurePath('class.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_with_added_static_method.php'), - ); + $file = $this->mockFileSave('class.php', 'class_with_added_static_method.php'); new PHPFileBuilder($file) ->addMethod( @@ -617,12 +524,7 @@ public function testAddStaticMethod(): void public function testAddProtectedMethod(): void { - $file = $this->generateOriginalStructurePath('class.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_with_added_protected_method.php'), - ); + $file = $this->mockFileSave('class.php', 'class_with_added_protected_method.php'); new PHPFileBuilder($file) ->addMethod( @@ -635,12 +537,7 @@ public function testAddProtectedMethod(): void public function testAddMethodToEnum(): void { - $file = $this->generateOriginalStructurePath('enum.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'enum_with_added_method.php'), - ); + $file = $this->mockFileSave('enum.php', 'enum_with_added_method.php'); new PHPFileBuilder($file) ->addMethod( @@ -654,12 +551,7 @@ public function testAddMethodToEnum(): void public function testAddMethodToTrait(): void { - $file = $this->generateOriginalStructurePath('trait.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'trait_with_added_method.php'), - ); + $file = $this->mockFileSave('trait.php', 'trait_with_added_method.php'); new PHPFileBuilder($file) ->addMethod( @@ -674,6 +566,7 @@ public function testAddMethodNotClassTraitEnum(): void { $file = $this->generateOriginalStructurePath('interface.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(InvalidStructureTypeException::class, "'AddMethod' operation may only be applied to: Class, Trait, Enum."); new PHPFileBuilder($file) @@ -715,12 +608,7 @@ public static function provideRemoveMethod(): array #[DataProvider('provideRemoveMethod')] public function testRemoveMethod(string $structure, string $method, string $result): void { - $file = $this->generateOriginalStructurePath($structure); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, $result), - ); + $file = $this->mockFileSave($structure, $result); new PHPFileBuilder($file) ->removeMethod($method) @@ -729,12 +617,7 @@ public function testRemoveMethod(string $structure, string $method, string $resu public function testAddReturnedArrayItem(): void { - $file = $this->generateOriginalStructurePath('class.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_with_return_array_item_added.php'), - ); + $file = $this->mockFileSave('class.php', 'class_with_return_array_item_added.php'); new PHPFileBuilder($file) ->addReturnedArrayItem('casts', 'datetime', 'created_at') @@ -751,12 +634,7 @@ public function testAddReturnedArrayItem(): void public function testAddReturnedArrayItemUpdatesExistingKey(): void { - $file = $this->generateOriginalStructurePath('class.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'class_with_return_array_item_updated.php'), - ); + $file = $this->mockFileSave('class.php', 'class_with_return_array_item_updated.php'); new PHPFileBuilder($file) ->addReturnedArrayItem('casts', 'encrypted', 'password') @@ -765,12 +643,7 @@ public function testAddReturnedArrayItemUpdatesExistingKey(): void public function testAddReturnedArrayItemInTrait(): void { - $file = $this->generateOriginalStructurePath('trait.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'trait_with_return_array_item_added.php'), - ); + $file = $this->mockFileSave('trait.php', 'trait_with_return_array_item_added.php'); new PHPFileBuilder($file) ->addReturnedArrayItem('getUserData', "['admin', 'editor']", 'roles') @@ -779,12 +652,7 @@ public function testAddReturnedArrayItemInTrait(): void public function testAddReturnedArrayItemInEnum(): void { - $file = $this->generateOriginalStructurePath('enum.php'); - - $this->mockNativeFunction( - 'RonasIT\Larabuilder\Builders', - $this->callFilePutContent($file, 'enum_with_return_array_item_added.php'), - ); + $file = $this->mockFileSave('enum.php', 'enum_with_return_array_item_added.php'); new PHPFileBuilder($file) ->addReturnedArrayItem('updatableStatuses', 'self::Second') @@ -795,6 +663,7 @@ public function testAddReturnedArrayItemThrowsOnNonArrayReturn(): void { $file = $this->generateOriginalStructurePath('class.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(UnexpectedReturnTypeException::class, "Method 'someMethod' return value has unexpected type. Expected 'array', actual 'true'."); new PHPFileBuilder($file) @@ -806,6 +675,7 @@ public function testAddReturnedArrayItemThrowsOnMultipleReturnStatements(): void { $file = $this->generateOriginalStructurePath('class.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(MultipleReturnStatementsException::class, "Method 'getRelations' contains multiple return statements."); new PHPFileBuilder($file) @@ -817,6 +687,7 @@ public function testAddReturnedArrayItemThrowsOnMethodNotFound(): void { $file = $this->generateOriginalStructurePath('class.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(NodeNotExistException::class, "Method 'noMethod' does not exist."); new PHPFileBuilder($file) @@ -828,6 +699,7 @@ public function testAddReturnedArrayItemNotClassTraitEnum(): void { $file = $this->generateOriginalStructurePath('interface.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(InvalidStructureTypeException::class, "'AddReturnedArrayItem' operation may only be applied to: Class, Trait, Enum."); new PHPFileBuilder($file) @@ -839,6 +711,7 @@ public function testAddReturnedArrayItemInvalidCode(): void { $file = $this->generateOriginalStructurePath('class.php'); + $this->expectNoFileSave(); $this->assertExceptionThrew(InvalidPHPCodeException::class, 'Cannot parse provided code: \'??invalid\'.'); new PHPFileBuilder($file) diff --git a/tests/Support/Traits/PHPFileBuilderTestMockTrait.php b/tests/Support/Traits/PHPFileBuilderTestMockTrait.php index 9e9bcd1..7b51887 100644 --- a/tests/Support/Traits/PHPFileBuilderTestMockTrait.php +++ b/tests/Support/Traits/PHPFileBuilderTestMockTrait.php @@ -8,8 +8,21 @@ trait PHPFileBuilderTestMockTrait { use MockTrait; - protected function callFilePutContent(string $fileName, string $resultFixture, int $flags = 0): array + protected function mockFileSave(string $originStructure, ?string $resultFixture = null): string { - return $this->functionCall('file_put_contents', [$fileName, $this->getFixture($resultFixture), $flags]); + $file = $this->generateOriginalStructurePath($originStructure); + + $this->mockNativeFunction( + 'RonasIT\Larabuilder\Builders', + $this->functionCall('file_put_contents', [$file, $this->getFixture($resultFixture ?? $originStructure)]), + ); + + return $file; + } + + protected function expectNoFileSave(): void + { + $this->getFunctionMock('RonasIT\Larabuilder\Builders', 'file_put_contents') + ->expects($this->never()); } }