/
TestCase.php
968 lines (892 loc) · 30.9 KB
/
TestCase.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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @since 1.2.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\TestSuite;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
use Cake\Event\EventManager;
use Cake\Http\BaseApplication;
use Cake\ORM\Entity;
use Cake\ORM\Exception\MissingTableClassException;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\Routing\Router;
use Cake\TestSuite\Constraint\EventFired;
use Cake\TestSuite\Constraint\EventFiredWith;
use Cake\Utility\Inflector;
use Exception;
use PHPUnit\Framework\TestCase as BaseTestCase;
/**
* Cake TestCase class
*/
abstract class TestCase extends BaseTestCase
{
use LocatorAwareTrait;
/**
* The class responsible for managing the creation, loading and removing of fixtures
*
* @var \Cake\TestSuite\Fixture\FixtureManager|null
*/
public $fixtureManager;
/**
* Fixtures used by this test case.
*
* @var string[]|string|null
*/
public $fixtures;
/**
* By default, all fixtures attached to this class will be truncated and reloaded after each test.
* Set this to false to handle manually
*
* @var bool
*/
public $autoFixtures = true;
/**
* Control table create/drops on each test method.
*
* If true, tables will still be dropped at the
* end of each test runner execution.
*
* @var bool
*/
public $dropTables = false;
/**
* Configure values to restore at end of test.
*
* @var array
*/
protected $_configure = [];
/**
* Overrides SimpleTestCase::skipIf to provide a boolean return value
*
* @param bool $shouldSkip Whether or not the test should be skipped.
* @param string $message The message to display.
* @return bool
*/
public function skipIf($shouldSkip, $message = '')
{
if ($shouldSkip) {
$this->markTestSkipped($message);
}
return $shouldSkip;
}
/**
* Helper method for tests that needs to use error_reporting()
*
* @param int $errorLevel value of error_reporting() that needs to use
* @param callable $callable callable function that will receive asserts
* @return void
*/
public function withErrorReporting($errorLevel, $callable)
{
$default = error_reporting();
error_reporting($errorLevel);
try {
$callable();
} finally {
error_reporting($default);
}
}
/**
* Helper method for check deprecation methods
*
* @param callable $callable callable function that will receive asserts
* @return void
*/
public function deprecated($callable)
{
$errorLevel = error_reporting();
error_reporting(E_ALL ^ E_USER_DEPRECATED);
try {
$callable();
} finally {
error_reporting($errorLevel);
}
}
/**
* Setup the test case, backup the static object values so they can be restored.
* Specifically backs up the contents of Configure and paths in App if they have
* not already been backed up.
*
* @return void
*/
public function setUp()
{
parent::setUp();
if (!$this->_configure) {
$this->_configure = Configure::read();
}
if (class_exists('Cake\Routing\Router', false)) {
Router::reload();
}
EventManager::instance(new EventManager());
}
/**
* teardown any static object changes and restore them.
*
* @return void
*/
public function tearDown()
{
parent::tearDown();
if ($this->_configure) {
Configure::clear();
Configure::write($this->_configure);
}
$this->getTableLocator()->clear();
$this->_configure = [];
$this->_tableLocator = null;
$this->fixtureManager = null;
}
/**
* Chooses which fixtures to load for a given test
*
* Each parameter is a model name that corresponds to a fixture, i.e. 'Posts', 'Authors', etc.
* Passing no parameters will cause all fixtures on the test case to load.
*
* @return void
* @see \Cake\TestSuite\TestCase::$autoFixtures
* @throws \Exception when no fixture manager is available.
*/
public function loadFixtures()
{
if ($this->fixtureManager === null) {
throw new Exception('No fixture manager to load the test fixture');
}
$args = func_get_args();
foreach ($args as $class) {
$this->fixtureManager->loadSingle($class, null, $this->dropTables);
}
if (empty($args)) {
$autoFixtures = $this->autoFixtures;
$this->autoFixtures = true;
$this->fixtureManager->load($this);
$this->autoFixtures = $autoFixtures;
}
}
/**
* Load plugins into a simulated application.
*
* Useful to test how plugins being loaded/not loaded interact with other
* elements in CakePHP or applications.
*
* @param array $plugins List of Plugins to load.
* @return \Cake\Http\BaseApplication
*/
public function loadPlugins(array $plugins = [])
{
/** @var \Cake\Http\BaseApplication $app */
$app = $this->getMockForAbstractClass(
BaseApplication::class,
['']
);
foreach ($plugins as $pluginName => $config) {
if (is_array($config)) {
$app->addPlugin($pluginName, $config);
} else {
$app->addPlugin($config);
}
}
$app->pluginBootstrap();
$builder = Router::createRouteBuilder('/');
$app->pluginRoutes($builder);
return $app;
}
/**
* Remove plugins from the global plugin collection.
*
* Useful in test case teardown methods.
*
* @param string[] $names A list of plugins you want to remove.
* @return void
*/
public function removePlugins(array $names = [])
{
$collection = Plugin::getCollection();
foreach ($names as $name) {
$collection->remove($name);
}
}
/**
* Clear all plugins from the global plugin collection.
*
* Useful in test case teardown methods.
*
* @return void
*/
public function clearPlugins()
{
Plugin::getCollection()->clear();
}
/**
* Asserts that a global event was fired. You must track events in your event manager for this assertion to work
*
* @param string $name Event name
* @param EventManager|null $eventManager Event manager to check, defaults to global event manager
* @param string $message Assertion failure message
* @return void
*/
public function assertEventFired($name, $eventManager = null, $message = '')
{
if (!$eventManager) {
$eventManager = EventManager::instance();
}
$this->assertThat($name, new EventFired($eventManager), $message);
}
/**
* Asserts an event was fired with data
*
* If a third argument is passed, that value is used to compare with the value in $dataKey
*
* @param string $name Event name
* @param string $dataKey Data key
* @param string $dataValue Data value
* @param EventManager|null $eventManager Event manager to check, defaults to global event manager
* @param string $message Assertion failure message
* @return void
*/
public function assertEventFiredWith($name, $dataKey, $dataValue, $eventManager = null, $message = '')
{
if (!$eventManager) {
$eventManager = EventManager::instance();
}
$this->assertThat($name, new EventFiredWith($eventManager, $dataKey, $dataValue), $message);
}
/**
* Assert text equality, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
*
* @param string $expected The expected value.
* @param string $result The actual value.
* @param string $message The message to use for failure.
* @return void
*/
public function assertTextNotEquals($expected, $result, $message = '')
{
$expected = str_replace(["\r\n", "\r"], "\n", $expected);
$result = str_replace(["\r\n", "\r"], "\n", $result);
$this->assertNotEquals($expected, $result, $message);
}
/**
* Assert text equality, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
*
* @param string $expected The expected value.
* @param string $result The actual value.
* @param string $message The message to use for failure.
* @return void
*/
public function assertTextEquals($expected, $result, $message = '')
{
$expected = str_replace(["\r\n", "\r"], "\n", $expected);
$result = str_replace(["\r\n", "\r"], "\n", $result);
$this->assertEquals($expected, $result, $message);
}
/**
* Asserts that a string starts with a given prefix, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
*
* @param string $prefix The prefix to check for.
* @param string $string The string to search in.
* @param string $message The message to use for failure.
* @return void
*/
public function assertTextStartsWith($prefix, $string, $message = '')
{
$prefix = str_replace(["\r\n", "\r"], "\n", $prefix);
$string = str_replace(["\r\n", "\r"], "\n", $string);
$this->assertStringStartsWith($prefix, $string, $message);
}
/**
* Asserts that a string starts not with a given prefix, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
*
* @param string $prefix The prefix to not find.
* @param string $string The string to search.
* @param string $message The message to use for failure.
* @return void
*/
public function assertTextStartsNotWith($prefix, $string, $message = '')
{
$prefix = str_replace(["\r\n", "\r"], "\n", $prefix);
$string = str_replace(["\r\n", "\r"], "\n", $string);
$this->assertStringStartsNotWith($prefix, $string, $message);
}
/**
* Asserts that a string ends with a given prefix, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
*
* @param string $suffix The suffix to find.
* @param string $string The string to search.
* @param string $message The message to use for failure.
* @return void
*/
public function assertTextEndsWith($suffix, $string, $message = '')
{
$suffix = str_replace(["\r\n", "\r"], "\n", $suffix);
$string = str_replace(["\r\n", "\r"], "\n", $string);
$this->assertStringEndsWith($suffix, $string, $message);
}
/**
* Asserts that a string ends not with a given prefix, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
*
* @param string $suffix The suffix to not find.
* @param string $string The string to search.
* @param string $message The message to use for failure.
* @return void
*/
public function assertTextEndsNotWith($suffix, $string, $message = '')
{
$suffix = str_replace(["\r\n", "\r"], "\n", $suffix);
$string = str_replace(["\r\n", "\r"], "\n", $string);
$this->assertStringEndsNotWith($suffix, $string, $message);
}
/**
* Assert that a string contains another string, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
*
* @param string $needle The string to search for.
* @param string $haystack The string to search through.
* @param string $message The message to display on failure.
* @param bool $ignoreCase Whether or not the search should be case-sensitive.
* @return void
*/
public function assertTextContains($needle, $haystack, $message = '', $ignoreCase = false)
{
$needle = str_replace(["\r\n", "\r"], "\n", $needle);
$haystack = str_replace(["\r\n", "\r"], "\n", $haystack);
$this->assertContains($needle, $haystack, $message, $ignoreCase);
}
/**
* Assert that a text doesn't contain another text, ignoring differences in newlines.
* Helpful for doing cross platform tests of blocks of text.
*
* @param string $needle The string to search for.
* @param string $haystack The string to search through.
* @param string $message The message to display on failure.
* @param bool $ignoreCase Whether or not the search should be case-sensitive.
* @return void
*/
public function assertTextNotContains($needle, $haystack, $message = '', $ignoreCase = false)
{
$needle = str_replace(["\r\n", "\r"], "\n", $needle);
$haystack = str_replace(["\r\n", "\r"], "\n", $haystack);
$this->assertNotContains($needle, $haystack, $message, $ignoreCase);
}
/**
* Asserts HTML tags.
*
* @param string $string An HTML/XHTML/XML string
* @param array $expected An array, see above
* @param bool $fullDebug Whether or not more verbose output should be used.
* @return void
* @deprecated 3.0. Use assertHtml() instead.
*/
public function assertTags($string, $expected, $fullDebug = false)
{
deprecationWarning('TestCase::assertTags() is deprecated. Use TestCase::assertHtml() instead.');
$this->assertHtml($expected, $string, $fullDebug);
}
/**
* Asserts HTML tags.
*
* Takes an array $expected and generates a regex from it to match the provided $string.
* Samples for $expected:
*
* Checks for an input tag with a name attribute (contains any non-empty value) and an id
* attribute that contains 'my-input':
*
* ```
* ['input' => ['name', 'id' => 'my-input']]
* ```
*
* Checks for two p elements with some text in them:
*
* ```
* [
* ['p' => true],
* 'textA',
* '/p',
* ['p' => true],
* 'textB',
* '/p'
* ]
* ```
*
* You can also specify a pattern expression as part of the attribute values, or the tag
* being defined, if you prepend the value with preg: and enclose it with slashes, like so:
*
* ```
* [
* ['input' => ['name', 'id' => 'preg:/FieldName\d+/']],
* 'preg:/My\s+field/'
* ]
* ```
*
* Important: This function is very forgiving about whitespace and also accepts any
* permutation of attribute order. It will also allow whitespace between specified tags.
*
* @param array $expected An array, see above
* @param string $string An HTML/XHTML/XML string
* @param bool $fullDebug Whether or not more verbose output should be used.
* @return bool
*/
public function assertHtml($expected, $string, $fullDebug = false)
{
$regex = [];
$normalized = [];
foreach ((array)$expected as $key => $val) {
if (!is_numeric($key)) {
$normalized[] = [$key => $val];
} else {
$normalized[] = $val;
}
}
$i = 0;
foreach ($normalized as $tags) {
if (!is_array($tags)) {
$tags = (string)$tags;
}
$i++;
if (is_string($tags) && $tags[0] === '<') {
$tags = [substr($tags, 1) => []];
} elseif (is_string($tags)) {
$tagsTrimmed = preg_replace('/\s+/m', '', $tags);
if (preg_match('/^\*?\//', $tags, $match) && $tagsTrimmed !== '//') {
$prefix = [null, null];
if ($match[0] === '*/') {
$prefix = ['Anything, ', '.*?'];
}
$regex[] = [
sprintf('%sClose %s tag', $prefix[0], substr($tags, strlen($match[0]))),
sprintf('%s\s*<[\s]*\/[\s]*%s[\s]*>[\n\r]*', $prefix[1], substr($tags, strlen($match[0]))),
$i,
];
continue;
}
if (!empty($tags) && preg_match('/^preg\:\/(.+)\/$/i', $tags, $matches)) {
$tags = $matches[1];
$type = 'Regex matches';
} else {
$tags = '\s*' . preg_quote($tags, '/');
$type = 'Text equals';
}
$regex[] = [
sprintf('%s "%s"', $type, $tags),
$tags,
$i,
];
continue;
}
foreach ($tags as $tag => $attributes) {
$regex[] = [
sprintf('Open %s tag', $tag),
sprintf('[\s]*<%s', preg_quote($tag, '/')),
$i,
];
if ($attributes === true) {
$attributes = [];
}
$attrs = [];
$explanations = [];
$i = 1;
foreach ($attributes as $attr => $val) {
if (is_numeric($attr) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
$attrs[] = $matches[1];
$explanations[] = sprintf('Regex "%s" matches', $matches[1]);
continue;
}
$quotes = '["\']';
if (is_numeric($attr)) {
$attr = $val;
$val = '.+?';
$explanations[] = sprintf('Attribute "%s" present', $attr);
} elseif (!empty($val) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
$val = str_replace(
['.*', '.+'],
['.*?', '.+?'],
$matches[1]
);
$quotes = $val !== $matches[1] ? '["\']' : '["\']?';
$explanations[] = sprintf('Attribute "%s" matches "%s"', $attr, $val);
} else {
$explanations[] = sprintf('Attribute "%s" == "%s"', $attr, $val);
$val = preg_quote($val, '/');
}
$attrs[] = '[\s]+' . preg_quote($attr, '/') . '=' . $quotes . $val . $quotes;
$i++;
}
if ($attrs) {
$regex[] = [
'explains' => $explanations,
'attrs' => $attrs,
];
}
$regex[] = [
sprintf('End %s tag', $tag),
'[\s]*\/?[\s]*>[\n\r]*',
$i,
];
}
}
foreach ($regex as $i => $assertion) {
$matches = false;
if (isset($assertion['attrs'])) {
$string = $this->_assertAttributes($assertion, $string, $fullDebug, $regex);
if ($fullDebug === true && $string === false) {
debug($string, true);
debug($regex, true);
}
continue;
}
list($description, $expressions, $itemNum) = $assertion;
$expression = null;
foreach ((array)$expressions as $expression) {
$expression = sprintf('/^%s/s', $expression);
if (preg_match($expression, $string, $match)) {
$matches = true;
$string = substr($string, strlen($match[0]));
break;
}
}
if (!$matches) {
if ($fullDebug === true) {
debug($string);
debug($regex);
}
$this->assertRegExp($expression, $string, sprintf('Item #%d / regex #%d failed: %s', $itemNum, $i, $description));
return false;
}
}
$this->assertTrue(true, '%s');
return true;
}
/**
* Check the attributes as part of an assertTags() check.
*
* @param array $assertions Assertions to run.
* @param string $string The HTML string to check.
* @param bool $fullDebug Whether or not more verbose output should be used.
* @param array|string $regex Full regexp from `assertHtml`
* @return string|bool
*/
protected function _assertAttributes($assertions, $string, $fullDebug = false, $regex = '')
{
$asserts = $assertions['attrs'];
$explains = $assertions['explains'];
do {
$matches = false;
$j = null;
foreach ($asserts as $j => $assert) {
if (preg_match(sprintf('/^%s/s', $assert), $string, $match)) {
$matches = true;
$string = substr($string, strlen($match[0]));
array_splice($asserts, $j, 1);
array_splice($explains, $j, 1);
break;
}
}
if ($matches === false) {
if ($fullDebug === true) {
debug($string);
debug($regex);
}
$this->assertTrue(false, 'Attribute did not match. Was expecting ' . $explains[$j]);
}
$len = count($asserts);
} while ($len > 0);
return $string;
}
/**
* Normalize a path for comparison.
*
* @param string $path Path separated by "/" slash.
* @return string Normalized path separated by DIRECTORY_SEPARATOR.
*/
protected function _normalizePath($path)
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
// @codingStandardsIgnoreStart
/**
* Compatibility function to test if a value is between an acceptable range.
*
* @param float $expected
* @param float $result
* @param float $margin the rage of acceptation
* @param string $message the text to display if the assertion is not correct
* @return void
*/
protected static function assertWithinRange($expected, $result, $margin, $message = '')
{
$upper = $result + $margin;
$lower = $result - $margin;
static::assertTrue(($expected <= $upper) && ($expected >= $lower), $message);
}
/**
* Compatibility function to test if a value is not between an acceptable range.
*
* @param float $expected
* @param float $result
* @param float $margin the rage of acceptation
* @param string $message the text to display if the assertion is not correct
* @return void
*/
protected static function assertNotWithinRange($expected, $result, $margin, $message = '')
{
$upper = $result + $margin;
$lower = $result - $margin;
static::assertTrue(($expected > $upper) || ($expected < $lower), $message);
}
/**
* Compatibility function to test paths.
*
* @param string $expected
* @param string $result
* @param string $message the text to display if the assertion is not correct
* @return void
*/
protected static function assertPathEquals($expected, $result, $message = '')
{
$expected = str_replace(DIRECTORY_SEPARATOR, '/', $expected);
$result = str_replace(DIRECTORY_SEPARATOR, '/', $result);
static::assertEquals($expected, $result, $message);
}
/**
* Compatibility function for skipping.
*
* @param bool $condition Condition to trigger skipping
* @param string $message Message for skip
* @return bool
*/
protected function skipUnless($condition, $message = '')
{
if (!$condition) {
$this->markTestSkipped($message);
}
return $condition;
}
// @codingStandardsIgnoreEnd
/**
* @inheritDoc
*/
public function getMockBuilder($className)
{
return new MockBuilder($this, $className);
}
/**
* @inheritDoc
*/
protected function getMockClass(
$originalClassName,
$methods = [],
array $arguments = [],
$mockClassName = '',
$callOriginalConstructor = false,
$callOriginalClone = true,
$callAutoload = true,
$cloneArguments = false
) {
MockBuilder::setSupressedErrorHandler();
try {
return parent::getMockClass(
$originalClassName,
$methods,
$arguments,
$mockClassName,
$callOriginalConstructor,
$callOriginalClone,
$callAutoload,
$cloneArguments
);
} finally {
restore_error_handler();
}
}
/**
* @inheritDoc
*/
protected function getMockForTrait(
$traitName,
array $arguments = [],
$mockClassName = '',
$callOriginalConstructor = true,
$callOriginalClone = true,
$callAutoload = true,
$mockedMethods = [],
$cloneArguments = false
) {
MockBuilder::setSupressedErrorHandler();
try {
return parent::getMockForTrait(
$traitName,
$arguments,
$mockClassName,
$callOriginalConstructor,
$callOriginalClone,
$callAutoload,
$mockedMethods,
$cloneArguments
);
} finally {
restore_error_handler();
}
}
/**
* @inheritDoc
*/
protected function getMockForAbstractClass(
$originalClassName,
array $arguments = [],
$mockClassName = '',
$callOriginalConstructor = true,
$callOriginalClone = true,
$callAutoload = true,
$mockedMethods = [],
$cloneArguments = false
) {
MockBuilder::setSupressedErrorHandler();
try {
return parent::getMockForAbstractClass(
$originalClassName,
$arguments,
$mockClassName,
$callOriginalConstructor,
$callOriginalClone,
$callAutoload,
$mockedMethods,
$cloneArguments
);
} finally {
restore_error_handler();
}
}
/**
* Mock a model, maintain fixtures and table association
*
* @param string $alias The model to get a mock for.
* @param string[]|null $methods The list of methods to mock
* @param array $options The config data for the mock's constructor.
* @throws \Cake\ORM\Exception\MissingTableClassException
* @return \Cake\ORM\Table|\PHPUnit_Framework_MockObject_MockObject
*/
public function getMockForModel($alias, $methods = [], array $options = [])
{
/** @var \Cake\ORM\Table $className */
$className = $this->_getTableClassName($alias, $options);
$connectionName = $className::defaultConnectionName();
$connection = ConnectionManager::get($connectionName);
$locator = $this->getTableLocator();
list(, $baseClass) = pluginSplit($alias);
$options += ['alias' => $baseClass, 'connection' => $connection];
$options += $locator->getConfig($alias);
/** @var \Cake\ORM\Table|\PHPUnit_Framework_MockObject_MockObject $mock */
$mock = $this->getMockBuilder($className)
->setMethods($methods)
->setConstructorArgs([$options])
->getMock();
if (empty($options['entityClass']) && $mock->getEntityClass() === Entity::class) {
$parts = explode('\\', $className);
$entityAlias = Inflector::classify(Inflector::underscore(substr(array_pop($parts), 0, -5)));
$entityClass = implode('\\', array_slice($parts, 0, -1)) . '\\Entity\\' . $entityAlias;
if (class_exists($entityClass)) {
$mock->setEntityClass($entityClass);
}
}
if (stripos($mock->getTable(), 'mock') === 0) {
$mock->setTable(Inflector::tableize($baseClass));
}
$locator->set($baseClass, $mock);
$locator->set($alias, $mock);
return $mock;
}
/**
* Gets the class name for the table.
*
* @param string $alias The model to get a mock for.
* @param array $options The config data for the mock's constructor.
* @return string
* @throws \Cake\ORM\Exception\MissingTableClassException
*/
protected function _getTableClassName($alias, array $options)
{
if (empty($options['className'])) {
$class = Inflector::camelize($alias);
$className = App::className($class, 'Model/Table', 'Table');
if (!$className) {
throw new MissingTableClassException([$alias]);
}
$options['className'] = $className;
}
return $options['className'];
}
/**
* Set the app namespace
*
* @param string $appNamespace The app namespace, defaults to "TestApp".
* @return void
*/
public static function setAppNamespace($appNamespace = 'TestApp')
{
Configure::write('App.namespace', $appNamespace);
}
/**
* Adds a fixture to this test case.
*
* Examples:
* - core.Tags
* - app.MyRecords
* - plugin.MyPluginName.MyModelName
*
* Use this method inside your test cases' {@link getFixtures()} method
* to build up the fixture list.
*
* @param string $fixture Fixture
* @return $this
*/
protected function addFixture($fixture)
{
if (!isset($this->fixtures)) {
$this->fixtures = [];
} elseif (is_string($this->fixtures)) {
deprecationWarning(
'Setting fixtures as string is deprecated and will be removed in 4.0.' .
' Set TestCase::$fixtures as array instead.'
);
$this->fixtures = array_map('trim', explode(',', $this->fixtures));
}
$this->fixtures[] = $fixture;
return $this;
}
/**
* Gets fixtures.
*
* @return array
*/
public function getFixtures()
{
if (!isset($this->fixtures)) {
return [];
}
if (is_string($this->fixtures)) {
deprecationWarning(
'Setting fixtures as string is deprecated and will be removed in 4.0.' .
' Set TestCase::$fixtures as array instead.'
);
return array_map('trim', explode(',', $this->fixtures));
}
return $this->fixtures;
}
}