From 8bdb329828e0fd92aa53d77a206bc23226d60a0c Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Tue, 19 Aug 2025 01:13:13 -0700 Subject: [PATCH] gitignore, product photos fixture, copy/delete in base controller test setUp/tearDown --- .gitignore | 2 +- src/Controller/ProductPhotosController.php | 3 +- tests/Fixture/ProductPhotosFixture.php | 2 +- .../Controller/BaseControllerTest.php | 38 ++++++++++++++++++ .../ProductPhotosControllerTest.php | 9 +---- .../2c386086-f4c5-4093-bea5-ee9c29479f58.png | Bin 0 -> 943 bytes 6 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 tests/test_app/webroot/images/2c386086-f4c5-4093-bea5-ee9c29479f58.png diff --git a/.gitignore b/.gitignore index ad5b07f..8f57e7f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ /vendor/ /.idea/ tmp -tests/test_app/webroot/images/products \ No newline at end of file +tests/test_app/webroot/images/products/ diff --git a/src/Controller/ProductPhotosController.php b/src/Controller/ProductPhotosController.php index c6cb2f7..1d929f9 100644 --- a/src/Controller/ProductPhotosController.php +++ b/src/Controller/ProductPhotosController.php @@ -101,10 +101,11 @@ class ProductPhotosController extends AppController $this->Flash->error(__('The product photo could not be saved. Please, try again.')); } $products = $productPhotosTable->Products->find('list', limit: 200)->all(); + $this->set(compact('productPhoto', 'products')); } -d /** + /** * Edit method * * @param string|null $id Product Photo id. diff --git a/tests/Fixture/ProductPhotosFixture.php b/tests/Fixture/ProductPhotosFixture.php index 7c8de7f..66564bd 100644 --- a/tests/Fixture/ProductPhotosFixture.php +++ b/tests/Fixture/ProductPhotosFixture.php @@ -23,7 +23,7 @@ class ProductPhotosFixture extends TestFixture 'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', 'product_sku_id' => null, 'photo_dir' => '/products/cfc98a9a-29b2-44c8-b587-8156adc05317/', - 'photo_filename' => '2c386086-f4c5-4093-bea5-ee9c29479f58.jpg', + 'photo_filename' => '2c386086-f4c5-4093-bea5-ee9c29479f58.png', 'primary_photo' => 1, 'photo_position' => 100, 'enabled' => 1, diff --git a/tests/TestCase/Controller/BaseControllerTest.php b/tests/TestCase/Controller/BaseControllerTest.php index eae19fc..9cc2b21 100644 --- a/tests/TestCase/Controller/BaseControllerTest.php +++ b/tests/TestCase/Controller/BaseControllerTest.php @@ -2,8 +2,12 @@ namespace CakeProducts\Test\TestCase\Controller; +use Cake\Core\Configure; use Cake\TestSuite\IntegrationTestTrait; use Cake\TestSuite\TestCase; +use FilesystemIterator; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; class BaseControllerTest extends TestCase { @@ -22,4 +26,38 @@ class BaseControllerTest extends TestCase { $this->assertEquals(1, 1); } + + /** + * setUp method + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + $toCopy = PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'webroot' . DS . 'images' . DS . '2c386086-f4c5-4093-bea5-ee9c29479f58.png'; + $productsFolder = PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'webroot' . DS . + 'images' . DS . 'products' . DS . 'cfc98a9a-29b2-44c8-b587-8156adc05317'; + $newName = $productsFolder . DS . '2c386086-f4c5-4093-bea5-ee9c29479f58.png'; + if (file_exists($toCopy)) { + if (!file_exists($productsFolder)) { + mkdir($productsFolder); + } + copy($toCopy, $newName); + } + } + + protected function tearDown(): void + { + parent::tearDown(); // TODO: Change the autogenerated stub + + $path = Configure::readOrFail('CakeProducts.photos.directory'); + if (file_exists($path)) { + $di = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS); + $ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST); + foreach ( $ri as $file ) { + $file->isDir() ? rmdir($file->getRealPath()) : unlink($file->getRealPath()); + } + } + } } diff --git a/tests/TestCase/Controller/ProductPhotosControllerTest.php b/tests/TestCase/Controller/ProductPhotosControllerTest.php index 1849bed..ff39884 100644 --- a/tests/TestCase/Controller/ProductPhotosControllerTest.php +++ b/tests/TestCase/Controller/ProductPhotosControllerTest.php @@ -58,14 +58,7 @@ class ProductPhotosControllerTest extends BaseControllerTest protected function tearDown(): void { unset($this->ProductPhotos); - $path = Configure::readOrFail('CakeProducts.photos.directory'); - if (file_exists($path)) { - $di = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS); - $ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST); - foreach ( $ri as $file ) { - $file->isDir() ? rmdir($file->getRealPath()) : unlink($file->getRealPath()); - } - } + parent::tearDown(); } diff --git a/tests/test_app/webroot/images/2c386086-f4c5-4093-bea5-ee9c29479f58.png b/tests/test_app/webroot/images/2c386086-f4c5-4093-bea5-ee9c29479f58.png new file mode 100644 index 0000000000000000000000000000000000000000..394fa42d5131cdc7b0b1246af93b92179f3c887e GIT binary patch literal 943 zcmV;g15o^lP)9q)cr7> zIGsQFGn3| zCzs2iP$-yfVPOGVTU&6sT(-5fwHb2tVsLP9#{Vr9Ct?R7q(rf?v2A5#W$OI=e1YUJ zQ1YRnA&iWSQ1XYAm__>aYb6XIhMiYVD+-z8_pYi6+CsH{*^m;vOjqvbr=H&DFkeqxHQBh$Scsoy0Glw(T zsaSG*ok62V;~yXYNgP*DUw;o98^+0@vGFb{HC+As}XJ=;xg=B7N_;-mKbHH{|lXs_o+aPcs5~J?s%^P2Odb)Uz z$GvY6^!N9(C2-h?28B$qx7%_yHnt2eU%nQ0qThbl6a_+b)EirjBgQ`g1_07Fr&6R? RzIgxu002ovPDHLkV1mdlwUYn< literal 0 HcmV?d00001