1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ /*
6+ * This file is part of the Sigwin Yassg project.
7+ *
8+ * (c) sigwin.hr
9+ *
10+ * This source file is subject to the MIT license that is bundled
11+ * with this source code in the file LICENSE.
12+ */
13+
14+ namespace Sigwin \YASSG \Test ;
15+
16+ use PHPUnit \Framework \TestCase ;
17+ use Sigwin \YASSG \Asset \AssetCopy ;
18+ use Sigwin \YASSG \Asset \AssetFetch ;
19+ use Sigwin \YASSG \AssetQueue ;
20+ use Symfony \Component \Filesystem \Filesystem ;
21+ use Symfony \Contracts \HttpClient \HttpClientInterface ;
22+
23+ class AssetQueueTest extends TestCase
24+ {
25+ private string $ tempDir ;
26+ private AssetQueue $ assetQueue ;
27+ private Filesystem $ filesystem ;
28+ private HttpClientInterface $ httpClient ;
29+
30+ protected function setUp (): void
31+ {
32+ $ this ->tempDir = sys_get_temp_dir () . '/yassg_test_ ' . uniqid ();
33+ mkdir ($ this ->tempDir , 0777 , true );
34+
35+ $ this ->filesystem = new Filesystem ();
36+ $ this ->httpClient = $ this ->createMock (HttpClientInterface::class);
37+ $ this ->assetQueue = new AssetQueue ($ this ->tempDir , $ this ->filesystem , $ this ->httpClient );
38+ }
39+
40+ protected function tearDown (): void
41+ {
42+ if (is_dir ($ this ->tempDir )) {
43+ $ this ->filesystem ->remove ($ this ->tempDir );
44+ }
45+ }
46+
47+ public function testFlushDoesNotReturnExistingAssets (): void
48+ {
49+ // Create a temporary source file
50+ $ sourceFile = $ this ->tempDir . '/source.txt ' ;
51+ file_put_contents ($ sourceFile , 'test content ' );
52+
53+ // Create existing destination file
54+ $ existingDestination = '/existing/asset.txt ' ;
55+ $ existingDestinationPath = $ this ->tempDir . $ existingDestination ;
56+ $ this ->filesystem ->mkdir (dirname ($ existingDestinationPath ));
57+ file_put_contents ($ existingDestinationPath , 'existing content ' );
58+
59+ // Create new destination path (doesn't exist yet)
60+ $ newDestination = '/new/asset.txt ' ;
61+
62+ // Add assets to queue
63+ $ existingAsset = new AssetCopy ($ sourceFile , $ existingDestination );
64+ $ newAsset = new AssetCopy ($ sourceFile , $ newDestination );
65+
66+ $ this ->assetQueue ->add ($ existingAsset );
67+ $ this ->assetQueue ->add ($ newAsset );
68+
69+ // Track which assets were processed via callback
70+ $ processedAssets = [];
71+
72+ // Flush the queue
73+ $ returnedAssets = $ this ->assetQueue ->flush (function ($ asset ) use (&$ processedAssets ) {
74+ $ processedAssets [] = $ asset ;
75+ });
76+
77+ // Currently this test will FAIL because the method returns all assets
78+ // After the fix, it should only return the new asset
79+ $ this ->assertCount (1 , $ returnedAssets , 'Should only return newly processed assets, not existing ones ' );
80+ $ this ->assertSame ($ newAsset , $ returnedAssets [0 ], 'Should return the new asset ' );
81+
82+ // Verify callback was only called for new asset
83+ $ this ->assertCount (1 , $ processedAssets , 'Callback should only be called for newly processed assets ' );
84+ $ this ->assertSame ($ newAsset , $ processedAssets [0 ], 'Callback should be called with the new asset ' );
85+
86+ // Verify the new asset was actually copied
87+ $ this ->assertFileExists ($ this ->tempDir . $ newDestination , 'New asset should be copied ' );
88+ $ this ->assertEquals ('test content ' , file_get_contents ($ this ->tempDir . $ newDestination ));
89+
90+ // Verify existing file was not modified
91+ $ this ->assertEquals ('existing content ' , file_get_contents ($ existingDestinationPath ));
92+ }
93+
94+ public function testFlushWithNoExistingAssets (): void
95+ {
96+ // Create a temporary source file
97+ $ sourceFile = $ this ->tempDir . '/source.txt ' ;
98+ file_put_contents ($ sourceFile , 'test content ' );
99+
100+ // Create assets that don't exist yet
101+ $ asset1 = new AssetCopy ($ sourceFile , '/new/asset1.txt ' );
102+ $ asset2 = new AssetCopy ($ sourceFile , '/new/asset2.txt ' );
103+
104+ $ this ->assetQueue ->add ($ asset1 );
105+ $ this ->assetQueue ->add ($ asset2 );
106+
107+ // Track which assets were processed via callback
108+ $ processedAssets = [];
109+
110+ // Flush the queue
111+ $ returnedAssets = $ this ->assetQueue ->flush (function ($ asset ) use (&$ processedAssets ) {
112+ $ processedAssets [] = $ asset ;
113+ });
114+
115+ // Should return all assets since none existed
116+ $ this ->assertCount (2 , $ returnedAssets , 'Should return all processed assets ' );
117+ $ this ->assertContains ($ asset1 , $ returnedAssets );
118+ $ this ->assertContains ($ asset2 , $ returnedAssets );
119+
120+ // Verify callback was called for all assets
121+ $ this ->assertCount (2 , $ processedAssets , 'Callback should be called for all processed assets ' );
122+ $ this ->assertContains ($ asset1 , $ processedAssets );
123+ $ this ->assertContains ($ asset2 , $ processedAssets );
124+
125+ // Verify both assets were copied
126+ $ this ->assertFileExists ($ this ->tempDir . '/new/asset1.txt ' );
127+ $ this ->assertFileExists ($ this ->tempDir . '/new/asset2.txt ' );
128+ }
129+
130+ public function testEmptyQueue (): void
131+ {
132+ $ returnedAssets = $ this ->assetQueue ->flush ();
133+
134+ $ this ->assertEmpty ($ returnedAssets , 'Empty queue should return empty array ' );
135+ }
136+ }
0 commit comments