-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.php
executable file
·2767 lines (2520 loc) · 97.1 KB
/
Test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Test methods for the P4 Model Iterator.
*
* @copyright 2011 Perforce Software. All rights reserved.
* @license Please see LICENSE.txt in top-level folder of this distribution.
* @version <release>/<patch>
*/
class P4_File_Test extends TestCase
{
/**
* Test fetchAll filespec handling.
*/
public function testFetchAllSingular()
{
$tests = array(
array(
'label' => __LINE__ .': null query',
'query' => P4_File_Query::create(),
'error' => 'Cannot fetch files. No filespecs provided in query.',
),
array(
'label' => __LINE__ .': valid, but nonexistant filespec',
'query' => P4_File_Query::create()->addFilespec('//depot/foobartesting'),
'error' => '',
),
array(
'label' => __LINE__ .': valid, but nonexistant depot filespec',
'query' => P4_File_Query::create()->addFilespec('//depotadsadsa/foobartesting'),
'error' => '',
)
);
foreach ($tests as $test) {
try {
$files = P4_File::fetchAll($test['query']);
if ($test['error']) {
$this->fail($test['label'] .' - unexpected success');
}
} catch (InvalidArgumentException $e) {
if ($test['error']) {
$this->assertSame(
$test['error'],
$e->getMessage(),
$test['label'] .' - Expected error'
);
} else {
$this->fail($test['error'] .' - unexpected failure: '. $e->getMessage());
}
} catch (Exception $e) {
$this->fail($test['label'] .' - Unexpected exception: '. $e->getMessage());
}
}
}
/**
* Test fetchAll with multiple filespecs.
*/
public function testFetchAllMultiple()
{
$basePath = '//depot/';
$testFiles = $this->_prepareFetchAllTests($basePath, false);
$query = P4_File_Query::create()->addFilespecs(array($basePath .'*small.txt', $basePath .'medium.jpg'));
$files = P4_File::fetchAll($query);
$filenames = array();
foreach ($files as $file) {
$filenames[] = $file->getFilespec();
}
$this->assertSame(
array(
'//depot/another_small.txt',
'//depot/small.txt',
'//depot/medium.jpg',
),
$filenames,
"Expected matching filenames when using multiple filespecs."
);
}
/**
* Test sync and flush.
*/
public function testSyncFlush()
{
// ensure sync of non-existent file throws exception.
$file = new P4_File;
$file->setFilespec('//depot/non-existent-file');
try {
$file->sync();
$this->fail("Excepted exception syncing non-existent file.");
} catch (P4_File_Exception $e) {
$this->assertTrue(true);
}
// ensure flush of non-existent file throws exception.
try {
$file->flush();
$this->fail("Excepted exception syncing non-existent file.");
} catch (P4_File_Exception $e) {
$this->assertTrue(true);
}
// create a file
$file = new P4_File;
$content = 'Content.';
$file->setFilespec('//depot/file.txt')
->add()
->setLocalContents($content)
->submit('Add file.');
$this->assertTrue(
is_file($file->getLocalFilename()),
'Expect file to exist after create/submit.'
);
// delete client file
$file->deleteLocalFile();
$this->assertFalse(
is_file($file->getLocalFilename()),
'Expect file to no longer exist after delete.'
);
// sync the file; should fail as server thinks we have the file.
$file->sync();
$this->assertFalse(
is_file($file->getLocalFilename()),
'Expect file to still exist after sync without force.'
);
// sync again, with force.
$file->sync(true);
$this->assertTrue(
is_file($file->getLocalFilename()),
'Expect file to now exist after sync with force.'
);
// verify content
$this->assertSame(
$content,
$file->getLocalContents(),
'Expected content after sync.'
);
// revise content and flush; sync should not affect client file.
$newContent = 'Some new content.';
$file->setLocalContents($newContent)
->flush()
->sync();
$this->assertSame(
$newContent,
$file->getLocalContents(),
'Expected content after flush/sync'
);
// force sync again, content should revert to original.
$file->sync(true);
$this->assertSame(
$content,
$file->getLocalContents(),
'Expected content after force sync'
);
}
/**
* Test getBasename.
*/
public function testGetBasename()
{
$file = new P4_File;
$path = 'a/path/to/a/file.txt';
$root = $this->p4->getClientRoot();
$filespec = "$root/$path";
$file->setFilespec($filespec);
$this->assertEquals(
'file.txt',
$file->getBasename(),
'Expected basename'
);
}
/**
* Test getFileSize and getLocalFileSize.
*/
public function testGetFileSizeAndGetLocalFileSize()
{
$basePath = '//depot/testFileSize/';
$files = $this->_prepareFetchAllTests($basePath);
$expectedSizes = array(
'small.txt' => 13,
'another_small.txt' => 13,
'medium.jpg' => 40,
'ze_medium_2.jpg' => 40,
'large.txt' => 3599,
'opened.txt' => 7,
);
foreach ($files as $file) {
$filename = basename($file->getFilespec());
if ($filename === 'opened.txt') {
$this->assertSame(
$expectedSizes[$filename],
$file->getLocalFileSize(),
"Expected filesize for '$filename'"
);
} else {
$this->assertSame(
$expectedSizes[$filename],
$file->getFilesize(),
"Expected filesize for '$filename'"
);
}
}
// test getting sizes for non-existant files.
$file = new P4_File;
$file->setFilespec('//depot/does_not_exist.txt');
try {
$size = $file->getFileSize();
$this->fail("Unexpected success for getFileSize().");
} catch (P4_File_Exception $e) {
$this->assertSame(
'The file does not have a fileSize attribute.',
$e->getMessage(),
'Expected error for getFileSize().'
);
} catch (Exception $e) {
$this->fail(
"Unexpected exception for getFileSize() ("
. get_class($e) .') :'. $e->getMessage()
);
}
try {
$size = $file->getLocalFileSize();
$this->fail("Unexpected success for getLocalFileSize().");
} catch (P4_File_Exception $e) {
$this->assertSame(
'The local file does not exist.',
$e->getMessage(),
'Expected error for getLocalFileSize().'
);
} catch (Exception $e) {
$this->fail(
"Unexpected exception for getLocalFileSize() ("
. get_class($e) .') :'. $e->getMessage()
);
}
}
/**
* Test filename in local-file syntax.
*/
public function testFilenameInLocalSyntax()
{
$file = new P4_File;
$path = 'a/path/to/a/file.txt';
$root = $this->p4->getClientRoot();
$filespec = "$root/$path";
$file->setFilespec($filespec);
$this->assertSame(
$filespec,
$file->getLocalFilename(),
'Expected local filename.'
);
$this->assertSame(
"//depot/$path",
$file->getDepotFilename(),
'Expected depot filename.'
);
// test file object with no filespec set
$file = new P4_File;
try {
$file->getLocalFilename();
$this->fail('Unexpected success for getLocalFilename with no filespec');
} catch (P4_File_Exception $e) {
$this->assertSame(
'Cannot complete operation, no filespec has been specified',
$e->getMessage(),
'Expected error for getLocalFilename with no filespec.'
);
} catch (Exception $e) {
$this->fail(
'Unexpected exception for getLocalFilename with no filespec ('
. get_class($e) .') :'. $e->getMessage()
);
}
try {
$file->getDepotFilename();
$this->fail('Unexpected success for getDepotFilename with no filespec');
} catch (P4_File_Exception $e) {
$this->assertSame(
'Cannot complete operation, no filespec has been specified',
$e->getMessage(),
'Expected error for getDepotFilename with no filespec.'
);
} catch (Exception $e) {
$this->fail(
'Unexpected exception for getDepotFilename with no filespec ('
. get_class($e) .') :'. $e->getMessage()
);
}
}
/**
* Test count method.
*/
public function testCount()
{
if (getenv('SKIP_LONG_TESTS')) {
$this->markTestSkipped('long fetchAll test skipped, because asked');
return;
}
$basePath = '//depot/testFetchOptions/';
$testFiles = $this->_prepareFetchAllTests($basePath, true);
$tests = array(
// sorting tests
array(
'label' => __LINE__ .': defaults',
'query' => P4_File_Query::create(),
'expected' => 6,
),
array(
'label' => __LINE__ .': reversed default sort',
'query' => P4_File_Query::create()->setReverseOrder(true),
'expected' => 6,
),
array(
'label' => __LINE__ .': sort',
'query' => P4_File_Query::create()->setSortBy('fileSize'),
'expected' => 6,
),
array(
'label' => __LINE__ .': reversed sort',
'query' => P4_File_Query::create()->setReverseOrder(true)->setSortBy('fileSize'),
'expected' => 6,
),
// limit results tests (limits should have no effect)
array(
'label' => __LINE__ .': limit',
'query' => P4_File_Query::create()->setMaxFiles(1),
'expected' => 1,
),
array(
'label' => __LINE__ .': reversed limit',
'query' => P4_File_Query::create()->setMaxFiles(1)->setReverseOrder(true),
'expected' => 1,
),
// filtering tests
array(
'label' => __LINE__ .': filter fileSize > 1000',
'query' => P4_File_Query::create()->setFilter('fileSize > 1000'),
'expected' => 1,
),
array(
'label' => __LINE__ .': filter fileSize <= 1000',
'query' => P4_File_Query::create()->setFilter('fileSize <= 1000'),
'expected' => 4,
),
array(
'label' => __LINE__ .': filter scrunchy',
'query' => P4_File_Query::create()->setFilter('scrunchy'),
'expected' => 0,
),
array(
'label' => __LINE__ .': filter with existing rowNumber',
'query' => P4_File_Query::create()->setFilter('fileSize <= 1000 & rowNumber >= 4'),
'expected' => 1,
),
array(
'label' => __LINE__ .': filter with rowNumber existing',
'query' => P4_File_Query::create()->setFilter('rowNumber >= 5 & fileSize <= 1000'),
'expected' => 0,
),
array(
'label' => __LINE__ .': filter with existing rowNumber existing',
'query' => P4_File_Query::create()->setFilter('fileSize <= 1000 rowNumber >= 2 & fileSize <= 1000'),
'expected' => 3,
),
// change and content tests
array(
'label' => __LINE__ .': change 1',
'query' => P4_File_Query::create()->setLimitToChangelist(1),
'expected' => 1,
),
// opened tests
array(
'label' => __LINE__ .': opened',
'query' => P4_File_Query::create()->setLimitToOpened(true),
'expected' => 1,
),
);
foreach ($tests as $test) {
$label = $test['label'];
$file = new P4_File;
$count = null;
$test['query']->addFilespec($basePath .'...');
try {
$count = P4_File::count($test['query']);
} catch (Exception $e) {
$this->fail("$label - unexpected failure (". get_class($e) .': '. $e->getMessage());
}
if (array_key_exists('dump', $test)) {
var_dump($file->getConnection()->run('fstat', array('-Ol', '//...')));
exit;
}
$this->assertEquals(
$test['expected'],
$count,
"$label - expected count"
);
}
}
/**
* Test count with no filespec.
*/
public function testCountWithoutFilespec()
{
try {
$count = P4_File::count(new P4_File_Query);
$this->fail('Unexpected success');
} catch (PHPUnit_Framework_AssertionFailedError $e) {
$this->fail($e->getMessage());
} catch (InvalidArgumentException $e) {
$this->assertSame(
'Cannot count files. No filespecs provided in query.',
$e->getMessage(),
'Expected exception'
);
} catch (Exception $e) {
$this->fail('Unexpected exception ('. get_class($e) .') :'. $e->getMessage());
}
}
/**
* Test fetchAll options.
*
* @todo fixup and remove skip for sorting tests when/if server sorts
* by size/data properly.
*/
public function testFetchAllOptions()
{
if (getenv('SKIP_LONG_TESTS')) {
$this->markTestSkipped('long fetchAll test skipped, because asked');
return;
}
$basePath = '//depot/testFetchOptions/';
$testFiles = $this->_prepareFetchAllTests($basePath, true);
$tests = array(
// sorting tests
array(
'label' => __LINE__ .': defaults',
'query' => P4_File_Query::create()->addFilespec($basePath .'...'),
'expected' => array(
'another_small.txt',
'large.txt',
'medium.jpg',
'opened.txt',
'small.txt',
'ze_medium_2.jpg',
),
'content' => false,
),
array(
'label' => __LINE__ .': reversed default sort',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setReverseOrder(true),
'expected' => array(
'ze_medium_2.jpg',
'small.txt',
'opened.txt',
'medium.jpg',
'large.txt',
'another_small.txt',
),
'content' => false,
),
array(
'label' => __LINE__ .': filesize sort',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setSortBy(P4_File_Query::SORT_FILE_SIZE),
'expected' => array(
'opened.txt',
'another_small.txt',
'small.txt',
'medium.jpg',
'ze_medium_2.jpg',
'large.txt',
),
'content' => false,
),
array(
'label' => __LINE__ .': reversed filesize sort',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setSortBy(P4_File_Query::SORT_FILE_SIZE)
->setReverseOrder(true),
'expected' => array(
'large.txt',
'medium.jpg',
'ze_medium_2.jpg',
'another_small.txt',
'small.txt',
'opened.txt',
),
'content' => false,
),
array(
'label' => __LINE__ .': date sort',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setSortBy(P4_File_Query::SORT_DATE),
'expected' => array(
'opened.txt',
'small.txt',
'medium.jpg',
'large.txt',
'another_small.txt',
'ze_medium_2.jpg',
),
'content' => false,
),
array(
'label' => __LINE__ .': reversed date sort',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setSortBy(P4_File_Query::SORT_DATE)
->setReverseOrder(true),
'expected' => array(
'ze_medium_2.jpg',
'another_small.txt',
'large.txt',
'medium.jpg',
'small.txt',
'opened.txt',
),
'content' => false,
),
array(
'label' => __LINE__ .': filetype sort',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setSortBy(P4_File_Query::SORT_FILE_TYPE),
'expected' => array(
'medium.jpg',
'ze_medium_2.jpg',
'another_small.txt',
'large.txt',
'opened.txt',
'small.txt',
),
'content' => false,
),
array(
'label' => __LINE__ .': reversed filetype sort',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setSortBy(P4_File_Query::SORT_FILE_TYPE)
->setReverseOrder(true),
'expected' => array(
'another_small.txt',
'large.txt',
'opened.txt',
'small.txt',
'medium.jpg',
'ze_medium_2.jpg',
),
'content' => false,
),
array(
'label' => __LINE__ .': attribute sort',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setSortBy('foo'),
'expected' => array(
'opened.txt',
'ze_medium_2.jpg',
'large.txt',
'medium.jpg',
'another_small.txt',
'small.txt',
),
'content' => false,
),
array(
'label' => __LINE__ .': reversed attribute sort',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setSortBy('foo', array(P4_File_Query::SORT_DESCENDING)),
'expected' => array(
'another_small.txt',
'small.txt',
'medium.jpg',
'large.txt',
'ze_medium_2.jpg',
'opened.txt',
),
'content' => false,
),
array(
'label' => __LINE__ .': attribute + date sort',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setSortBy(array('foo', P4_File_Query::SORT_DATE)),
'expected' => array(
'opened.txt',
'ze_medium_2.jpg',
'large.txt',
'medium.jpg',
'small.txt',
'another_small.txt',
),
'content' => false,
),
// limit results tests
array(
'label' => __LINE__ .': limit 1',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setMaxFiles(1),
'expected' => array(
'another_small.txt',
),
'content' => false,
),
array(
'label' => __LINE__ .': reversed limit 1',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setMaxFiles(1)
->setReverseOrder(true),
'expected' => array(
'ze_medium_2.jpg',
),
'content' => false,
),
array(
'label' => __LINE__ .': limit 2',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setMaxFiles(2),
'expected' => array(
'another_small.txt',
'large.txt',
),
'content' => false,
),
array(
'label' => __LINE__ .': reversed limit 2',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setMaxFiles(2)
->setReverseOrder(true),
'expected' => array(
'ze_medium_2.jpg',
'small.txt',
),
'content' => false,
),
// filtering tests
array(
'label' => __LINE__ .': filter fileSize > 1000',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setFilter('fileSize > 1000'),
'expected' => array(
'large.txt',
),
'content' => false,
),
array(
'label' => __LINE__ .': filter fileSize <= 1000',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setFilter('fileSize <= 1000'),
'expected' => array(
'another_small.txt',
'medium.jpg',
'small.txt',
'ze_medium_2.jpg',
),
'content' => false,
),
// change and content tests
array(
'label' => __LINE__ .': change 1',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setLimitToChangelist(1),
'expected' => array(
basename($testFiles[0]->getFilespec()),
),
'content' => false,
),
array(
'label' => __LINE__ .': change 2',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setLimitToChangelist(2),
'expected' => array(
basename($testFiles[1]->getFilespec()),
),
'content' => true,
),
array(
'label' => __LINE__ .': change 3',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setLimitToChangelist(3),
'expected' => array(
basename($testFiles[2]->getFilespec()),
),
'content' => true,
),
array(
'label' => __LINE__ .': change 4',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setLimitToChangelist(4),
'expected' => array(
basename($testFiles[3]->getFilespec()),
),
'content' => true,
),
array(
'label' => __LINE__ .': change 5',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setLimitToChangelist(5),
'expected' => array(
basename($testFiles[4]->getFilespec()),
),
'content' => true,
),
array(
'label' => __LINE__ .': change default',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setLimitToChangelist('default'),
'expected' => array(
basename($testFiles[5]->getFilespec()),
),
'content' => true,
),
// opened tests
array(
'label' => __LINE__ .': opened',
'query' => P4_File_Query::create()->addFilespec($basePath .'...')
->setLimitToOpened(true),
'expected' => array(
basename($testFiles[5]->getFilespec()),
),
'content' => true,
),
);
$showSizes = 0;
foreach ($tests as $test) {
$label = $test['label'];
$file = new P4_File;
if (array_key_exists('dumpQuery', $test) and $test['dumpQuery']) {
print var_export($test['query']->toArray(), true);
}
try {
$files = P4_File::fetchAll($test['query']);
} catch (Exception $e) {
$this->fail("$label - unexpected failure: ". $e->getMessage());
}
if (array_key_exists('dump', $test)) {
var_dump($file->getConnection()->run('info'));
var_dump($file->getConnection()->run('fstat', array('-Oal', '//depot/testFetchOptions/...')));
exit;
}
$this->assertEquals(
count($test['expected']),
count($files),
"$label - expected file count"
);
$fileNames = array();
foreach ($files as $file) {
$filename = $file->getFilespec();
$filesize = $file->getLocalFileSize();
$fileNames[] = basename($file->getFilespec());
if ($showSizes) print "'$filename' is '$filesize' bytes.\n";
}
$showSizes = 0;
$this->assertSame(
$test['expected'],
$fileNames,
"$label - expected file list"
);
if ($test['content']) {
for ($i = 0; $i < count($test['expected']); $i++) {
$method = ($testFiles[$test['expected'][$i]]->isOpened()) ? 'getLocalContents' : 'getDepotContents';
$this->assertSame(
$testFiles[$test['expected'][$i]]->$method(),
$files[$i]->$method(),
"$label - Expected content for file #$i"
);
}
}
}
}
/**
* Verify we are doing a proper natural order sort on attributes
*/
public function testNaturalSort()
{
$basePath = "//depot/test/";
$file = new P4_File;
$file->setFilespec($basePath .'10.txt')
->open()
->setLocalContents('i am a file')
->setAttribute('foo', 'give-me-a-10')
->submit('10');
$file->setFilespec($basePath .'2.txt')
->open()
->setLocalContents('i am a file')
->setAttribute('foo', 'give-me-a-2')
->submit('2');
$file->setFilespec($basePath .'1.txt')
->open()
->setLocalContents('i am a file')
->setAttribute('foo', 'give-me-a-1')
->submit('1');
$file->setFilespec($basePath .'just1.txt')
->open()
->setLocalContents('i am a file')
->setAttribute('foo', '1')
->submit('just digit 1');
$file->setFilespec($basePath .'a.txt')
->open()
->setLocalContents('i am a file')
->setAttribute('foo', 'a')
->submit('a');
$file->setFilespec($basePath .'Aa.txt')
->open()
->setLocalContents('i am a file')
->setAttribute('foo', 'Aa')
->submit('A');
$file->setFilespec($basePath .'z.txt')
->open()
->setLocalContents('i am a file')
->setAttribute('foo', 'z')
->submit('z');
$file->setFilespec($basePath .'Zz.txt')
->open()
->setLocalContents('i am a file')
->setAttribute('foo', 'Zz')
->submit('Zz');
$foos = P4_File::fetchAll(
P4_File_Query::create()->setFilespecs($basePath . "...")->setSortBy('foo')
);
$this->assertSame(
array(
'1',
'a',
'Aa',
'give-me-a-1',
'give-me-a-2',
'give-me-a-10',
'z',
'Zz'
),
$foos->invoke('getAttribute', array('foo')),
'Expecting sorted attributes'
);
}
/**
* Test an already opened file.
*/
public function testAlreadyOpened()
{
$file = new P4_File;
$file->setFilespec('//depot/notAlreadyOpened.txt');
$this->assertFalse($file->exists($file->getFilespec()), 'Expect file to not exist #1.');
$this->assertFalse($file->isOpened(), 'Expect file to not be opened #1.');
$file->open()->setLocalContents('Some content.');
$this->assertFalse($file->exists($file->getFilespec()), 'Still expect file to not exist #1.');
$this->assertTrue($file->isOpened(), 'Expect file to be opened #1.');
$file->open();
$this->assertFalse($file->exists($file->getFilespec()), 'Still expect file to not exist #1.');
$this->assertTrue($file->isOpened(), 'Expect file to still be opened #1.');
$file->revert();
// once again, with initial add instead of open
$file = new P4_File;
$file->setFilespec('//depot/notAlreadyOpened.txt');
$this->assertFalse($file->exists($file->getFilespec()), 'Expect file to not exist #2.');
$this->assertFalse($file->isOpened(), 'Expect file to not be opened. #2');
$file->add(null, 'binary')->setLocalContents('Some content.');
$this->assertFalse($file->exists($file->getFilespec()), 'Still expect file to not exist #2.');
$this->assertTrue($file->isOpened(), 'Expect file to be opened. #2');
$file->open();
$this->assertFalse($file->exists($file->getFilespec()), 'Still expect file to not exist #2.');
$this->assertTrue($file->isOpened(), 'Expect file to still be opened #2.');
}
/**
* Test file deletion.
*
* @todo enable when appropriate error handling exists
*/
public function testFileDeletion()
{
// first, create a file to delete.
$file = new P4_File;
$file->setFilespec('//depot/a_file_to_delete.txt');
$this->assertFalse($file->exists($file->getFilespec()), 'Expect file to not exist.');
$file->open()
->setLocalContents('Some content to delete.')
->submit('Adding a file to delete.');
$this->assertTrue((bool)$file->exists($file->getFilespec()), 'Expect file to exist.');
$this->assertFalse($file->isDeleted(), 'Expect file to not have deleted status after create');
// expect file to still exist if exclude deleted is true.
$this->assertTrue(
(bool)P4_File::exists($file->getFilespec(), null, true),
"Expected file to exist with excluded deleted = true."
);
// test deleting the opened file.
$file->open();
$this->assertTrue($file->isOpened(), 'Expect file to be opened.');
// first, with force set to false
try {
$file->delete(null, false);
$this->fail('Unexpected success deleting an opened file without force.');
} catch (P4_File_Exception $e) {
$this->assertSame(
"Failed to open file for delete: //depot/a_file_to_delete.txt"
. " - can't delete (already opened for edit)",
$e->getMessage(),
'Expected error message deleting an opened file without force.'
);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
$this->fail($e->getMessage());
} catch (Exception $e) {
$this->fail('Unexpected exception on delete opened file: '. $e->getMessage());
}
// again with force defaulting to true
try {
$file->delete();
} catch (Exception $e) {
$this->fail('Unexpected exception on delete opened file: '. $e->getMessage());
}
// test deleting the file again.
try {
$file->delete();
} catch (Exception $e) {
$this->fail('Unexpected exception on delete opened file again: '. $e->getMessage());
}
// now submit the deletion.
$file->submit('Delete the file.');
$this->assertTrue((bool)$file->exists($file->getFilespec()), 'Expect file to still exist.');
$this->assertTrue(
$file->hasStatusField('headAction')
&& $file->getStatus('headAction') == 'delete',
'Expect file to be deleted'
);
$this->assertTrue($file->isDeleted(), 'Expect file to have deleted status after delete.');
// expect file to not exist if exclude deleted is true.
$this->assertFalse(
P4_File::exists($file->getFilespec(), null, true),
"Expected file to not exist with excluded deleted = true."
);
// test deleting the file again after submission.
try {
$file->delete();
$this->fail('Unexpected success deleting an already deleted file');
} catch (P4_File_Exception $e) {
$this->assertSame(
"Failed to open file for delete: //depot/a_file_to_delete.txt - file(s) not on client.",
$e->getMessage(),
'Expected error message deleting an already deleted file.'