Classes List

Symfony\Component\DependencyInjection\Tests\ContainerBuilderTest

 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 
 1001 
 1002 
 1003 
 1004 
 1005 
 1006 
 1007 
 1008 
 1009 
 1010 
 1011 
 1012 
 1013 
 1014 
 1015 
 1016 
 1017 
 1018 
 1019 
 1020 
 1021 
 1022 
 1023 
 1024 
 1025 
 1026 
 1027 
 1028 
 1029 
 1030 
 1031 
 1032 
 1033 
 1034 
 1035 
 1036 
 1037 
 1038 
 1039 
 1040 
 1041 
 1042 
 1043 
 1044 
 1045 
 1046 
 1047 
 1048 
 1049 
 1050 
 1051 
 1052 
 1053 
 1054 
 1055 
 1056 
 1057 
 1058 
 1059 
 1060 
 1061 
 1062 
 1063 
 1064 
 1065 
 1066 
 1067 
 1068 
 1069 
 1070 
 1071 
 1072 
 1073 
 1074 
 1075 
 1076 
 1077 
 1078 
 1079 
 1080 
 1081 
 1082 
 1083 
 1084 
 1085 
 1086 
 1087 
 1088 
 1089 
 1090 
 1091 
 1092 
 1093 
 1094 
 1095 
 1096 
 1097 
 1098 
 1099 
 1100 
 1101 
 1102 
 1103 
 1104 
 1105 
 1106 
 1107 
 1108 
 1109 
 1110 
 1111 
 1112 
 1113 
 1114 
 1115 
 1116 
 1117 
 1118 
 1119 
 1120 
 1121 
 1122 
 1123 
 1124 
 1125 
 1126 
 1127 
 1128 
 1129 
 1130 
 1131 
 1132 
 1133 
 1134 
 1135 
 1136 
 1137 
 1138 
 1139 
 1140 
 1141 
 1142 
 1143 
 1144 
 1145 
 1146 
 1147 
 1148 
 1149 
 1150 
 1151 
 1152 
 1153 
 1154 
 1155 
 1156 
 1157 
 1158 
 1159 
 1160 
 1161 
 1162 
 1163 
 1164 
 1165 
 1166 
 1167 
 1168 
 1169 
 1170 
 1171 
 1172 
 1173 
 1174 
 1175 
 1176 
 1177 
 1178 
 1179 
 1180 
 1181 
 1182 
 1183 
 1184 
 1185 
 1186 
 1187 
 1188 
 1189 
 1190 
 1191 
 1192 
 1193 
 1194 
 1195 
 1196 
 1197 
 1198 
 1199 
 1200 
 1201 
 1202 
 1203 
 1204 
 1205 
 1206 
 1207 
 1208 
 1209 
 1210 
 1211 
 1212 
 1213 
 1214 
 1215 
 1216 
 1217 
 1218 
 1219 
 1220 
 1221 
 1222 
 1223 
 1224 
 1225 
 1226 
 1227 
 1228 
 1229 
 1230 
 1231 
 1232 
 1233 
 1234 
 1235 
 1236 
 1237 
 1238 
 1239 
 1240 
 1241 
 1242 
 1243 
 1244 
 1245 
 1246 
 1247 
 1248 
 1249 
 1250 
 1251 
 1252 
 1253 
 1254 
 1255 
 1256 
 1257 
 1258 
 1259 
 1260 
 1261 
 1262 
 1263 
 1264 
 1265 
 1266 
 1267 
 1268 
 1269 
 1270 
 1271 
 1272 
 1273 
 1274 
 1275 
 1276 
 1277 
 1278 
 1279 
 1280 
 1281 
 1282 
 1283 
 1284 
 1285 
 1286 
 1287 
 1288 
 1289 
 1290 
 1291 
 1292 
 1293 
 1294 
 1295 
 1296 
 1297 
 1298 
 1299 
 1300 
 1301 
 1302 
 1303 
 1304 
 1305 
 1306 
 1307 
 1308 
 1309 
 1310 
 1311 
 1312 
 1313 
 1314 
 1315 
 1316 
 1317 
 1318 
 1319 
 1320 
 1321 
 1322 
 1323 
 1324 
 1325 
 1326 
 1327 
 1328 
 1329 
 1330 
 1331 
 1332 
 1333 
 1334 
 1335 
 1336 
 1337 
 1338 
 1339 
 1340 
 1341 
 1342 
 1343 
 1344 
 1345 
 1346 
 1347 
 1348 
 1349 
 1350 
 1351 
 1352 
 1353 
 1354 
 1355 
 1356 
 1357 
 1358 
 1359 
 1360 
 1361 
 1362 
 1363 
 1364 
 1365 
 1366 
 1367 
 1368 
 1369 
 1370 
 1371 
 1372 
 1373 
 1374 
 1375 
 1376 
 1377 
 1378 
 1379 
 1380 
 1381 
 1382 
 1383 
 1384 
 1385 
 1386 
 1387 
 1388 
 1389 
 1390 
 1391 
 1392 
 1393 
 1394 
 1395 
 1396 
 1397 
 1398 
 1399 
 1400 
 1401 
 1402 
 1403 
 1404 
 1405 
 1406 
 1407 
 1408 
 1409 
 1410 
 1411 
 1412 
 1413 
 1414 
 1415 
 1416 
 1417 
 1418 
 1419 
 1420 
 1421 
 1422 
 1423 
 1424 
 1425 
 1426 
 1427 
 1428 
 1429 
 1430 
 1431 
 1432 
 1433 
 1434 
 1435 
 1436 
 1437 
 1438 
 1439 
 1440 
 1441 
 1442 
 1443 
 1444 
 1445 
 1446 
 1447 
 1448 
 1449 
 1450 
 1451 
 1452 
 1453 
<?php

/* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */


namespace Symfony\Component\DependencyInjection\Tests;

require_once __DIR__.'/Fixtures/includes/classes.php';
require_once __DIR__.'/Fixtures/includes/ProjectExtension.php';

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use Symfony\Component\Config\Resource\ComposerResource;
use Symfony\Component\Config\Resource\ResourceInterface;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy;
use Symfony\Component\DependencyInjection\TypedReference;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\ExpressionLanguage\Expression;

class ContainerBuilderTest extends TestCase
{
    public function testDefaultRegisteredDefinitions()
    {
        $builder = new ContainerBuilder();

        $this->assertCount(1$builder->getDefinitions());
        $this->assertTrue($builder->hasDefinition('service_container'));

        $definition = $builder->getDefinition('service_container');
        $this->assertInstanceOf(Definition::class$definition);
        $this->assertTrue($definition->isSynthetic());
        $this->assertSame(ContainerInterface::class$definition->getClass());
        $this->assertTrue($builder->hasAlias(PsrContainerInterface::class));
        $this->assertTrue($builder->hasAlias(ContainerInterface::class));
    }

    public function testDefinitions()
    {
        $builder = new ContainerBuilder();
        $definitions = array(
            'foo' => new Definition('Bar\FooClass'),
            'bar' => new Definition('BarClass'),
        );
        $builder->setDefinitions($definitions);
        $this->assertEquals($definitions$builder->getDefinitions(), '->setDefinitions() sets the service definitions');
        $this->assertTrue($builder->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists');
        $this->assertFalse($builder->hasDefinition('foobar'), '->hasDefinition() returns false if a service definition does not exist');

        $builder->setDefinition('foobar'$foo = new Definition('FooBarClass'));
        $this->assertEquals($foo$builder->getDefinition('foobar'), '->getDefinition() returns a service definition if defined');
        $this->assertSame($builder->setDefinition('foobar'$foo = new Definition('FooBarClass')), $foo'->setDefinition() implements a fluid interface by returning the service reference');

        $builder->addDefinitions($defs = array('foobar' => new Definition('FooBarClass')));
        $this->assertEquals(array_merge($definitions$defs), $builder->getDefinitions(), '->addDefinitions() adds the service definitions');

        try {
            $builder->getDefinition('baz');
            $this->fail('->getDefinition() throws a ServiceNotFoundException if the service definition does not exist');
        } catch (ServiceNotFoundException $e) {
            $this->assertEquals('You have requested a non-existent service "baz".'$e->getMessage(), '->getDefinition() throws a ServiceNotFoundException if the service definition does not exist');
        }
    }

    /** * @group legacy * @expectedDeprecation The "deprecated_foo" service is deprecated. You should stop using it, as it will soon be removed. */
    public function testCreateDeprecatedService()
    {
        $definition = new Definition('stdClass');
        $definition->setDeprecated(true);

        $builder = new ContainerBuilder();
        $builder->setDefinition('deprecated_foo'$definition);
        $builder->get('deprecated_foo');
    }

    public function testRegister()
    {
        $builder = new ContainerBuilder();
        $builder->register('foo''Bar\FooClass');
        $this->assertTrue($builder->hasDefinition('foo'), '->register() registers a new service definition');
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition'$builder->getDefinition('foo'), '->register() returns the newly created Definition instance');
    }

    public function testAutowire()
    {
        $builder = new ContainerBuilder();
        $builder->autowire('foo''Bar\FooClass');

        $this->assertTrue($builder->hasDefinition('foo'), '->autowire() registers a new service definition');
        $this->assertTrue($builder->getDefinition('foo')->isAutowired(), '->autowire() creates autowired definitions');
    }

    public function testHas()
    {
        $builder = new ContainerBuilder();
        $this->assertFalse($builder->has('foo'), '->has() returns false if the service does not exist');
        $builder->register('foo''Bar\FooClass');
        $this->assertTrue($builder->has('foo'), '->has() returns true if a service definition exists');
        $builder->set('bar'new \stdClass());
        $this->assertTrue($builder->has('bar'), '->has() returns true if a service exists');
    }

    /** * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException * @expectedExceptionMessage You have requested a non-existent service "foo". */
    public function testGetThrowsExceptionIfServiceDoesNotExist()
    {
        $builder = new ContainerBuilder();
        $builder->get('foo');
    }

    public function testGetReturnsNullIfServiceDoesNotExistAndInvalidReferenceIsUsed()
    {
        $builder = new ContainerBuilder();

        $this->assertNull($builder->get('foo'ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service does not exist and NULL_ON_INVALID_REFERENCE is passed as a second argument');
    }

    /** * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException */
    public function testGetThrowsCircularReferenceExceptionIfServiceHasReferenceToItself()
    {
        $builder = new ContainerBuilder();
        $builder->register('baz''stdClass')->setArguments(array(new Reference('baz')));
        $builder->get('baz');
    }

    public function testGetReturnsSameInstanceWhenServiceIsShared()
    {
        $builder = new ContainerBuilder();
        $builder->register('bar''stdClass');

        $this->assertTrue($builder->get('bar'=== $builder->get('bar'), '->get() always returns the same instance if the service is shared');
    }

    public function testGetCreatesServiceBasedOnDefinition()
    {
        $builder = new ContainerBuilder();
        $builder->register('foo''stdClass');

        $this->assertInternalType('object'$builder->get('foo'), '->get() returns the service definition associated with the id');
    }

    public function testGetReturnsRegisteredService()
    {
        $builder = new ContainerBuilder();
        $builder->set('bar'$bar = new \stdClass());

        $this->assertSame($bar$builder->get('bar'), '->get() returns the service associated with the id');
    }

    public function testRegisterDoesNotOverrideExistingService()
    {
        $builder = new ContainerBuilder();
        $builder->set('bar'$bar = new \stdClass());
        $builder->register('bar''stdClass');

        $this->assertSame($bar$builder->get('bar'), '->get() returns the service associated with the id even if a definition has been defined');
    }

    public function testNonSharedServicesReturnsDifferentInstances()
    {
        $builder = new ContainerBuilder();
        $builder->register('bar''stdClass')->setShared(false);

        $this->assertNotSame($builder->get('bar'), $builder->get('bar'));
    }

    /** * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException * @expectedExceptionMessage You have requested a synthetic service ("foo"). The DIC does not know how to construct this service. */
    public function testGetUnsetLoadingServiceWhenCreateServiceThrowsAnException()
    {
        $builder = new ContainerBuilder();
        $builder->register('foo''stdClass')->setSynthetic(true);

        // we expect a RuntimeException here as foo is synthetic
        try {
            $builder->get('foo');
        } catch (RuntimeException $e) {
        }

        // we must also have the same RuntimeException here
        $builder->get('foo');
    }

    public function testGetServiceIds()
    {
        $builder = new ContainerBuilder();
        $builder->register('foo''stdClass');
        $builder->bar = $bar = new \stdClass();
        $builder->register('bar''stdClass');
        $this->assertEquals(
            array(
                'service_container',
                'foo',
                'bar',
                'Psr\Container\ContainerInterface',
                'Symfony\Component\DependencyInjection\ContainerInterface',
            ),
            $builder->getServiceIds(),
            '->getServiceIds() returns all defined service ids'
        );
    }

    public function testAliases()
    {
        $builder = new ContainerBuilder();
        $builder->register('foo''stdClass');
        $builder->setAlias('bar''foo');
        $this->assertTrue($builder->hasAlias('bar'), '->hasAlias() returns true if the alias exists');
        $this->assertFalse($builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist');
        $this->assertEquals('foo'(string) $builder->getAlias('bar'), '->getAlias() returns the aliased service');
        $this->assertTrue($builder->has('bar'), '->setAlias() defines a new service');
        $this->assertSame($builder->get('bar'), $builder->get('foo'), '->setAlias() creates a service that is an alias to another one');

        try {
            $builder->setAlias('foobar''foobar');
            $this->fail('->setAlias() throws an InvalidArgumentException if the alias references itself');
        } catch (\InvalidArgumentException $e) {
            $this->assertEquals('An alias can not reference itself, got a circular reference on "foobar".'$e->getMessage(), '->setAlias() throws an InvalidArgumentException if the alias references itself');
        }

        try {
            $builder->getAlias('foobar');
            $this->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist');
        } catch (\InvalidArgumentException $e) {
            $this->assertEquals('The service alias "foobar" does not exist.'$e->getMessage(), '->getAlias() throws an InvalidArgumentException if the alias does not exist');
        }
    }

    public function testGetAliases()
    {
        $builder = new ContainerBuilder();
        $builder->setAlias('bar''foo');
        $builder->setAlias('foobar''foo');
        $builder->setAlias('moo'new Alias('foo'false));

        $aliases = $builder->getAliases();
        $this->assertEquals('foo'(string) $aliases['bar']);
        $this->assertTrue($aliases['bar']->isPublic());
        $this->assertEquals('foo'(string) $aliases['foobar']);
        $this->assertEquals('foo'(string) $aliases['moo']);
        $this->assertFalse($aliases['moo']->isPublic());

        $builder->register('bar''stdClass');
        $this->assertFalse($builder->hasAlias('bar'));

        $builder->set('foobar''stdClass');
        $builder->set('moo''stdClass');
        $this->assertCount(2$builder->getAliases(), '->getAliases() does not return aliased services that have been overridden');
    }

    public function testSetAliases()
    {
        $builder = new ContainerBuilder();
        $builder->setAliases(array('bar' => 'foo''foobar' => 'foo'));

        $aliases = $builder->getAliases();
        $this->assertArrayHasKey('bar'$aliases);
        $this->assertArrayHasKey('foobar'$aliases);
    }

    public function testAddAliases()
    {
        $builder = new ContainerBuilder();
        $builder->setAliases(array('bar' => 'foo'));
        $builder->addAliases(array('foobar' => 'foo'));

        $aliases = $builder->getAliases();
        $this->assertArrayHasKey('bar'$aliases);
        $this->assertArrayHasKey('foobar'$aliases);
    }

    public function testSetReplacesAlias()
    {
        $builder = new ContainerBuilder();
        $builder->setAlias('alias''aliased');
        $builder->set('aliased'new \stdClass());

        $builder->set('alias'$foo = new \stdClass());
        $this->assertSame($foo$builder->get('alias'), '->set() replaces an existing alias');
    }

    public function testAliasesKeepInvalidBehavior()
    {
        $builder = new ContainerBuilder();

        $aliased = new Definition('stdClass');
        $aliased->addMethodCall('setBar'array(new Reference('bar'ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
        $builder->setDefinition('aliased'$aliased);
        $builder->setAlias('alias''aliased');

        $this->assertEquals(new \stdClass(), $builder->get('alias'));
    }

    public function testAddGetCompilerPass()
    {
        $builder = new ContainerBuilder();
        $builder->setResourceTracking(false);
        $defaultPasses = $builder->getCompiler()->getPassConfig()->getPasses();
        $builder->addCompilerPass($pass1 = $this->getMockBuilder('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface')->getMock(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -5);
        $builder->addCompilerPass($pass2 = $this->getMockBuilder('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface')->getMock(), PassConfig::TYPE_BEFORE_OPTIMIZATION10);

        $passes = $builder->getCompiler()->getPassConfig()->getPasses();
        $this->assertCount(count($passes) - 2$defaultPasses);
        // Pass 1 is executed later
        $this->assertTrue(array_search($pass1$passestrue) > array_search($pass2$passestrue));
    }

    public function testCreateService()
    {
        $builder = new ContainerBuilder();
        $builder->register('foo1''Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
        $builder->register('foo2''Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/%file%.php');
        $builder->setParameter('file''foo');
        $this->assertInstanceOf('\Bar\FooClass'$builder->get('foo1'), '->createService() requires the file defined by the service definition');
        $this->assertInstanceOf('\Bar\FooClass'$builder->get('foo2'), '->createService() replaces parameters in the file provided by the service definition');
    }

    public function testCreateProxyWithRealServiceInstantiator()
    {
        $builder = new ContainerBuilder();

        $builder->register('foo1''Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
        $builder->getDefinition('foo1')->setLazy(true);

        $foo1 = $builder->get('foo1');

        $this->assertSame($foo1$builder->get('foo1'), 'The same proxy is retrieved on multiple subsequent calls');
        $this->assertSame('Bar\FooClass'get_class($foo1));
    }

    public function testCreateServiceClass()
    {
        $builder = new ContainerBuilder();
        $builder->register('foo1''%class%');
        $builder->setParameter('class''stdClass');
        $this->assertInstanceOf('\stdClass'$builder->get('foo1'), '->createService() replaces parameters in the class provided by the service definition');
    }

    public function testCreateServiceArguments()
    {
        $builder = new ContainerBuilder();
        $builder->register('bar''stdClass');
        $builder->register('foo1''Bar\FooClass')->addArgument(array('foo' => '%value%''%value%' => 'foo'new Reference('bar'), '%%unescape_it%%'));
        $builder->setParameter('value''bar');
        $this->assertEquals(array('foo' => 'bar''bar' => 'foo'$builder->get('bar'), '%unescape_it%'), $builder->get('foo1')->arguments'->createService() replaces parameters and service references in the arguments provided by the service definition');
    }

    public function testCreateServiceFactory()
    {
        $builder = new ContainerBuilder();
        $builder->register('foo''Bar\FooClass')->setFactory('Bar\FooClass::getInstance');
        $builder->register('qux''Bar\FooClass')->setFactory(array('Bar\FooClass''getInstance'));
        $builder->register('bar''Bar\FooClass')->setFactory(array(new Definition('Bar\FooClass'), 'getInstance'));
        $builder->register('baz''Bar\FooClass')->setFactory(array(new Reference('bar'), 'getInstance'));

        $this->assertTrue($builder->get('foo')->called'->createService() calls the factory method to create the service instance');
        $this->assertTrue($builder->get('qux')->called'->createService() calls the factory method to create the service instance');
        $this->assertTrue($builder->get('bar')->called'->createService() uses anonymous service as factory');
        $this->assertTrue($builder->get('baz')->called'->createService() uses another service as factory');
    }

    public function testCreateServiceMethodCalls()
    {
        $builder = new ContainerBuilder();
        $builder->register('bar''stdClass');
        $builder->register('foo1''Bar\FooClass')->addMethodCall('setBar'array(array('%value%'new Reference('bar'))));
        $builder->setParameter('value''bar');
        $this->assertEquals(array('bar'$builder->get('bar')), $builder->get('foo1')->bar'->createService() replaces the values in the method calls arguments');
    }

    public function testCreateServiceMethodCallsWithEscapedParam()
    {
        $builder = new ContainerBuilder();
        $builder->register('bar''stdClass');
        $builder->register('foo1''Bar\FooClass')->addMethodCall('setBar'array(array('%%unescape_it%%')));
        $builder->setParameter('value''bar');
        $this->assertEquals(array('%unescape_it%'), $builder->get('foo1')->bar'->createService() replaces the values in the method calls arguments');
    }

    public function testCreateServiceProperties()
    {
        $builder = new ContainerBuilder();
        $builder->register('bar''stdClass');
        $builder->register('foo1''Bar\FooClass')->setProperty('bar'array('%value%'new Reference('bar'), '%%unescape_it%%'));
        $builder->setParameter('value''bar');
        $this->assertEquals(array('bar'$builder->get('bar'), '%unescape_it%'), $builder->get('foo1')->bar'->createService() replaces the values in the properties');
    }

    public function testCreateServiceConfigurator()
    {
        $builder = new ContainerBuilder();
        $builder->register('foo1''Bar\FooClass')->setConfigurator('sc_configure');
        $builder->register('foo2''Bar\FooClass')->setConfigurator(array('%class%''configureStatic'));
        $builder->setParameter('class''BazClass');
        $builder->register('baz''BazClass');
        $builder->register('foo3''Bar\FooClass')->setConfigurator(array(new Reference('baz'), 'configure'));
        $builder->register('foo4''Bar\FooClass')->setConfigurator(array($builder->getDefinition('baz'), 'configure'));
        $builder->register('foo5''Bar\FooClass')->setConfigurator('foo');

        $this->assertTrue($builder->get('foo1')->configured'->createService() calls the configurator');
        $this->assertTrue($builder->get('foo2')->configured'->createService() calls the configurator');
        $this->assertTrue($builder->get('foo3')->configured'->createService() calls the configurator');
        $this->assertTrue($builder->get('foo4')->configured'->createService() calls the configurator');

        try {
            $builder->get('foo5');
            $this->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
        } catch (\InvalidArgumentException $e) {
            $this->assertEquals('The configure callable for class "Bar\FooClass" is not a callable.'$e->getMessage(), '->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
        }
    }

    public function testCreateServiceWithIteratorArgument()
    {
        $builder = new ContainerBuilder();
        $builder->register('bar''stdClass');
        $builder
            ->register('lazy_context''LazyContext')
            ->setArguments(array(
                new IteratorArgument(array('k1' => new Reference('bar'), new Reference('invalid'ContainerInterface::IGNORE_ON_INVALID_REFERENCE))),
                new IteratorArgument(array()),
            ))
        ;

        $lazyContext = $builder->get('lazy_context');
        $this->assertInstanceOf(RewindableGenerator::class$lazyContext->lazyValues);
        $this->assertInstanceOf(RewindableGenerator::class$lazyContext->lazyEmptyValues);
        $this->assertCount(1$lazyContext->lazyValues);
        $this->assertCount(0$lazyContext->lazyEmptyValues);

        $i = 0;
        foreach ($lazyContext->lazyValues as $k => $v) {
            ++$i;
            $this->assertEquals('k1'$k);
            $this->assertInstanceOf('\stdClass'$v);
        }

        // The second argument should have been ignored.
        $this->assertEquals(1$i);

        $i = 0;
        foreach ($lazyContext->lazyEmptyValues as $k => $v) {
            ++$i;
        }

        $this->assertEquals(0$i);
    }

    /** * @expectedException \RuntimeException */
    public function testCreateSyntheticService()
    {
        $builder = new ContainerBuilder();
        $builder->register('foo''Bar\FooClass')->setSynthetic(true);
        $builder->get('foo');
    }

    public function testCreateServiceWithExpression()
    {
        $builder = new ContainerBuilder();
        $builder->setParameter('bar''bar');
        $builder->register('bar''BarClass');
        $builder->register('foo''Bar\FooClass')->addArgument(array('foo' => new Expression('service("bar").foo ~ parameter("bar")')));
        $this->assertEquals('foobar'$builder->get('foo')->arguments['foo']);
    }

    public function testResolveServices()
    {
        $builder = new ContainerBuilder();
        $builder->register('foo''Bar\FooClass');
        $this->assertEquals($builder->get('foo'), $builder->resolveServices(new Reference('foo')), '->resolveServices() resolves service references to service instances');
        $this->assertEquals(array('foo' => array('foo'$builder->get('foo'))), $builder->resolveServices(array('foo' => array('foo'new Reference('foo')))), '->resolveServices() resolves service references to service instances in nested arrays');
        $this->assertEquals($builder->get('foo'), $builder->resolveServices(new Expression('service("foo")')), '->resolveServices() resolves expressions');
    }

    /** * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException * @expectedExceptionMessage Constructing service "foo" from a parent definition is not supported at build time. */
    public function testResolveServicesWithDecoratedDefinition()
    {
        $builder = new ContainerBuilder();
        $builder->setDefinition('grandpa'new Definition('stdClass'));
        $builder->setDefinition('parent'new ChildDefinition('grandpa'));
        $builder->setDefinition('foo'new ChildDefinition('parent'));

        $builder->get('foo');
    }

    public function testResolveServicesWithCustomDefinitionClass()
    {
        $builder = new ContainerBuilder();
        $builder->setDefinition('foo'new CustomDefinition('stdClass'));

        $this->assertInstanceOf('stdClass'$builder->get('foo'));
    }

    public function testMerge()
    {
        $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
        $container->setResourceTracking(false);
        $config = new ContainerBuilder(new ParameterBag(array('foo' => 'bar')));
        $container->merge($config);
        $this->assertEquals(array('bar' => 'foo''foo' => 'bar'), $container->getParameterBag()->all(), '->merge() merges current parameters with the loaded ones');

        $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
        $container->setResourceTracking(false);
        $config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%')));
        $container->merge($config);
        $container->compile();
        $this->assertEquals(array('bar' => 'foo''foo' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');

        $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
        $container->setResourceTracking(false);
        $config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%''baz' => '%foo%')));
        $container->merge($config);
        $container->compile();
        $this->assertEquals(array('bar' => 'foo''foo' => 'foo''baz' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');

        $container = new ContainerBuilder();
        $container->setResourceTracking(false);
        $container->register('foo''Bar\FooClass');
        $container->register('bar''BarClass');
        $config = new ContainerBuilder();
        $config->setDefinition('baz'new Definition('BazClass'));
        $config->setAlias('alias_for_foo''foo');
        $container->merge($config);
        $this->assertEquals(array('service_container''foo''bar''baz'), array_keys($container->getDefinitions()), '->merge() merges definitions already defined ones');

        $aliases = $container->getAliases();
        $this->assertArrayHasKey('alias_for_foo'$aliases);
        $this->assertEquals('foo'(string) $aliases['alias_for_foo']);

        $container = new ContainerBuilder();
        $container->setResourceTracking(false);
        $container->register('foo''Bar\FooClass');
        $config->setDefinition('foo'new Definition('BazClass'));
        $container->merge($config);
        $this->assertEquals('BazClass'$container->getDefinition('foo')->getClass(), '->merge() overrides already defined services');

        $container = new ContainerBuilder();
        $bag = new EnvPlaceholderParameterBag();
        $bag->get('env(Foo)');
        $config = new ContainerBuilder($bag);
        $this->assertSame(array('%env(Bar)%'), $config->resolveEnvPlaceholders(array($bag->get('env(Bar)'))));
        $container->merge($config);
        $this->assertEquals(array('Foo' => 0'Bar' => 1), $container->getEnvCounters());

        $container = new ContainerBuilder();
        $config = new ContainerBuilder();
        $childDefA = $container->registerForAutoconfiguration('AInterface');
        $childDefB = $config->registerForAutoconfiguration('BInterface');
        $container->merge($config);
        $this->assertSame(array('AInterface' => $childDefA'BInterface' => $childDefB), $container->getAutoconfiguredInstanceof());
    }

    /** * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException * @expectedExceptionMessage "AInterface" has already been autoconfigured and merge() does not support merging autoconfiguration for the same class/interface. */
    public function testMergeThrowsExceptionForDuplicateAutomaticInstanceofDefinitions()
    {
        $container = new ContainerBuilder();
        $config = new ContainerBuilder();
        $container->registerForAutoconfiguration('AInterface');
        $config->registerForAutoconfiguration('AInterface');
        $container->merge($config);
    }

    public function testResolveEnvValues()
    {
        $_ENV['DUMMY_ENV_VAR'] = 'du%%y';
        $_SERVER['DUMMY_SERVER_VAR'] = 'ABC';
        $_SERVER['HTTP_DUMMY_VAR'] = 'DEF';

        $container = new ContainerBuilder();
        $container->setParameter('bar''%% %env(DUMMY_ENV_VAR)% %env(DUMMY_SERVER_VAR)% %env(HTTP_DUMMY_VAR)%');
        $container->setParameter('env(HTTP_DUMMY_VAR)''123');

        $this->assertSame('%% du%%%%y ABC 123'$container->resolveEnvPlaceholders('%bar%'true));

        unset($_ENV['DUMMY_ENV_VAR'], $_SERVER['DUMMY_SERVER_VAR'], $_SERVER['HTTP_DUMMY_VAR']);
    }

    public function testResolveEnvValuesWithArray()
    {
        $_ENV['ANOTHER_DUMMY_ENV_VAR'] = 'dummy';

        $dummyArray = array('1' => 'one''2' => 'two');

        $container = new ContainerBuilder();
        $container->setParameter('dummy''%env(ANOTHER_DUMMY_ENV_VAR)%');
        $container->setParameter('dummy2'$dummyArray);

        $container->resolveEnvPlaceholders('%dummy%'true);
        $container->resolveEnvPlaceholders('%dummy2%'true);

        $this->assertInternalType('array'$container->resolveEnvPlaceholders('%dummy2%'true));

        foreach ($dummyArray as $key => $value) {
            $this->assertArrayHasKey($key$container->resolveEnvPlaceholders('%dummy2%'true));
        }

        unset($_ENV['ANOTHER_DUMMY_ENV_VAR']);
    }

    public function testCompileWithResolveEnv()
    {
        putenv('DUMMY_ENV_VAR=du%%y');
        $_SERVER['DUMMY_SERVER_VAR'] = 'ABC';
        $_SERVER['HTTP_DUMMY_VAR'] = 'DEF';

        $container = new ContainerBuilder();
        $container->setParameter('env(FOO)''Foo');
        $container->setParameter('env(DUMMY_ENV_VAR)''GHI');
        $container->setParameter('bar''%% %env(DUMMY_ENV_VAR)% %env(DUMMY_SERVER_VAR)% %env(HTTP_DUMMY_VAR)%');
        $container->setParameter('foo''%env(FOO)%');
        $container->setParameter('baz''%foo%');
        $container->setParameter('env(HTTP_DUMMY_VAR)''123');
        $container->register('teatime''stdClass')
            ->setProperty('foo''%env(DUMMY_ENV_VAR)%')
            ->setPublic(true)
        ;
        $container->compile(true);

        $this->assertSame('% du%%y ABC 123'$container->getParameter('bar'));
        $this->assertSame('Foo'$container->getParameter('baz'));
        $this->assertSame('du%%y'$container->get('teatime')->foo);

        unset($_SERVER['DUMMY_SERVER_VAR'], $_SERVER['HTTP_DUMMY_VAR']);
        putenv('DUMMY_ENV_VAR');
    }

    public function testCompileWithArrayResolveEnv()
    {
        putenv('ARRAY={"foo":"bar"}');

        $container = new ContainerBuilder();
        $container->setParameter('foo''%env(json:ARRAY)%');
        $container->compile(true);

        $this->assertSame(array('foo' => 'bar'), $container->getParameter('foo'));

        putenv('ARRAY');
    }

    public function testCompileWithArrayAndAnotherResolveEnv()
    {
        putenv('DUMMY_ENV_VAR=abc');
        putenv('ARRAY={"foo":"bar"}');

        $container = new ContainerBuilder();
        $container->setParameter('foo''%env(json:ARRAY)%');
        $container->setParameter('bar''%env(DUMMY_ENV_VAR)%');
        $container->compile(true);

        $this->assertSame(array('foo' => 'bar'), $container->getParameter('foo'));
        $this->assertSame('abc'$container->getParameter('bar'));

        putenv('DUMMY_ENV_VAR');
        putenv('ARRAY');
    }

    /** * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException * @expectedExceptionMessage A string value must be composed of strings and/or numbers, but found parameter "env(json:ARRAY)" of type array inside string value "ABC %env(json:ARRAY)%". */
    public function testCompileWithArrayInStringResolveEnv()
    {
        putenv('ARRAY={"foo":"bar"}');

        $container = new ContainerBuilder();
        $container->setParameter('foo''ABC %env(json:ARRAY)%');
        $container->compile(true);

        putenv('ARRAY');
    }

    /** * @expectedException \Symfony\Component\DependencyInjection\Exception\EnvNotFoundException * @expectedExceptionMessage Environment variable not found: "FOO". */
    public function testCompileWithResolveMissingEnv()
    {
        $container = new ContainerBuilder();
        $container->setParameter('foo''%env(FOO)%');
        $container->compile(true);
    }

    public function testDynamicEnv()
    {
        putenv('DUMMY_FOO=some%foo%');
        putenv('DUMMY_BAR=%bar%');

        $container = new ContainerBuilder();
        $container->setParameter('foo''Foo%env(resolve:DUMMY_BAR)%');
        $container->setParameter('bar''Bar');
        $container->setParameter('baz''%env(resolve:DUMMY_FOO)%');

        $container->compile(true);
        putenv('DUMMY_FOO');
        putenv('DUMMY_BAR');

        $this->assertSame('someFooBar'$container->getParameter('baz'));
    }

    public function testCastEnv()
    {
        $container = new ContainerBuilder();
        $container->setParameter('env(FAKE)''123');

        $container->register('foo''stdClass')
            ->setPublic(true)
            ->setProperties(array(
                'fake' => '%env(int:FAKE)%',
            ));

        $container->compile(true);

        $this->assertSame(123$container->get('foo')->fake);
    }

    public function testEnvAreNullable()
    {
        $container = new ContainerBuilder();
        $container->setParameter('env(FAKE)'null);

        $container->register('foo''stdClass')
            ->setPublic(true)
            ->setProperties(array(
            'fake' => '%env(int:FAKE)%',
        ));

        $container->compile(true);

        $this->assertNull($container->get('foo')->fake);
    }

    public function testEnvInId()
    {
        $container = include __DIR__.'/Fixtures/containers/container_env_in_id.php';
        $container->compile(true);

        $expected = array(
            'service_container',
            'foo',
            'bar',
            'bar_%env(BAR)%',
        );
        $this->assertSame($expectedarray_keys($container->getDefinitions()));

        $expected = array(
            PsrContainerInterface::class => true,
            ContainerInterface::class => true,
            'baz_%env(BAR)%' => true,
            'bar_%env(BAR)%' => true,
        );
        $this->assertSame($expected$container->getRemovedIds());

        $this->assertSame(array('baz_bar'), array_keys($container->getDefinition('foo')->getArgument(1)));
    }

    /** * @expectedException \Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException * @expectedExceptionMessage Circular reference detected for parameter "env(resolve:DUMMY_ENV_VAR)" ("env(resolve:DUMMY_ENV_VAR)" > "env(resolve:DUMMY_ENV_VAR)"). */
    public function testCircularDynamicEnv()
    {
        putenv('DUMMY_ENV_VAR=some%foo%');

        $container = new ContainerBuilder();
        $container->setParameter('foo''%bar%');
        $container->setParameter('bar''%env(resolve:DUMMY_ENV_VAR)%');

        try {
            $container->compile(true);
        } finally {
            putenv('DUMMY_ENV_VAR');
        }
    }

    /** * @expectedException \LogicException */
    public function testMergeLogicException()
    {
        $container = new ContainerBuilder();
        $container->setResourceTracking(false);
        $container->compile();
        $container->merge(new ContainerBuilder());
    }

    public function testfindTaggedServiceIds()
    {
        $builder = new ContainerBuilder();
        $builder
            ->register('foo''Bar\FooClass')
            ->addTag('foo'array('foo' => 'foo'))
            ->addTag('bar'array('bar' => 'bar'))
            ->addTag('foo'array('foofoo' => 'foofoo'))
        ;
        $this->assertEquals($builder->findTaggedServiceIds('foo'), array(
            'foo' => array(
                array('foo' => 'foo'),
                array('foofoo' => 'foofoo'),
            ),
        ), '->findTaggedServiceIds() returns an array of service ids and its tag attributes');
        $this->assertEquals(array(), $builder->findTaggedServiceIds('foobar'), '->findTaggedServiceIds() returns an empty array if there is annotated services');
    }

    public function testFindUnusedTags()
    {
        $builder = new ContainerBuilder();
        $builder
            ->register('foo''Bar\FooClass')
            ->addTag('kernel.event_listener'array('foo' => 'foo'))
            ->addTag('kenrel.event_listener'array('bar' => 'bar'))
        ;
        $builder->findTaggedServiceIds('kernel.event_listener');
        $this->assertEquals(array('kenrel.event_listener'), $builder->findUnusedTags(), '->findUnusedTags() returns an array with unused tags');
    }

    public function testFindDefinition()
    {
        $container = new ContainerBuilder();
        $container->setDefinition('foo'$definition = new Definition('Bar\FooClass'));
        $container->setAlias('bar''foo');
        $container->setAlias('foobar''bar');
        $this->assertEquals($definition$container->findDefinition('foobar'), '->findDefinition() returns a Definition');
    }

    public function testAddObjectResource()
    {
        $container = new ContainerBuilder();

        $container->setResourceTracking(false);
        $container->addObjectResource(new \BarClass());

        $this->assertEmpty($container->getResources(), 'No resources get registered without resource tracking');

        $container->setResourceTracking(true);
        $container->addObjectResource(new \BarClass());

        $resources = $container->getResources();

        $this->assertCount(2$resources'2 resources were registered');

        /* @var $resource \Symfony\Component\Config\Resource\FileResource */

        $resource = end($resources);

        $this->assertInstanceOf('Symfony\Component\Config\Resource\FileResource'$resource);
        $this->assertSame(realpath(__DIR__.'/Fixtures/includes/classes.php'), realpath($resource->getResource()));
    }

    /** * @group legacy */
    public function testAddClassResource()
    {
        $container = new ContainerBuilder();

        $container->setResourceTracking(false);
        $container->addClassResource(new \ReflectionClass('BarClass'));

        $this->assertEmpty($container->getResources(), 'No resources get registered without resource tracking');

        $container->setResourceTracking(true);
        $container->addClassResource(new \ReflectionClass('BarClass'));

        $resources = $container->getResources();

        $this->assertCount(2$resources'2 resources were registered');

        /* @var $resource \Symfony\Component\Config\Resource\FileResource */

        $resource = end($resources);

        $this->assertInstanceOf('Symfony\Component\Config\Resource\FileResource'$resource);
        $this->assertSame(realpath(__DIR__.'/Fixtures/includes/classes.php'), realpath($resource->getResource()));
    }

    public function testGetReflectionClass()
    {
        $container = new ContainerBuilder();

        $container->setResourceTracking(false);
        $r1 = $container->getReflectionClass('BarClass');

        $this->assertEmpty($container->getResources(), 'No resources get registered without resource tracking');

        $container->setResourceTracking(true);
        $r2 = $container->getReflectionClass('BarClass');
        $r3 = $container->getReflectionClass('BarClass');

        $this->assertNull($container->getReflectionClass('BarMissingClass'));

        $this->assertEquals($r1$r2);
        $this->assertSame($r2$r3);

        $resources = $container->getResources();

        $this->assertCount(3$resources'3 resources were registered');

        $this->assertSame('reflection.BarClass'(string) $resources[1]);
        $this->assertSame('BarMissingClass'(string) end($resources));
    }

    public function testGetReflectionClassOnInternalTypes()
    {
        $container = new ContainerBuilder();

        $this->assertNull($container->getReflectionClass('int'));
        $this->assertNull($container->getReflectionClass('float'));
        $this->assertNull($container->getReflectionClass('string'));
        $this->assertNull($container->getReflectionClass('bool'));
        $this->assertNull($container->getReflectionClass('resource'));
        $this->assertNull($container->getReflectionClass('object'));
        $this->assertNull($container->getReflectionClass('array'));
        $this->assertNull($container->getReflectionClass('null'));
        $this->assertNull($container->getReflectionClass('callable'));
        $this->assertNull($container->getReflectionClass('iterable'));
        $this->assertNull($container->getReflectionClass('mixed'));
    }

    public function testCompilesClassDefinitionsOfLazyServices()
    {
        $container = new ContainerBuilder();

        $this->assertEmpty($container->getResources(), 'No resources get registered without resource tracking');

        $container->register('foo''BarClass')->setPublic(true);
        $container->getDefinition('foo')->setLazy(true);

        $container->compile();

        $matchingResources = array_filter(
            $container->getResources(),
            function (ResourceInterface $resource) {
                return 'reflection.BarClass' === (string) $resource;
            }
        );

        $this->assertNotEmpty($matchingResources);
    }

    public function testResources()
    {
        $container = new ContainerBuilder();
        $container->addResource($a = new FileResource(__DIR__.'/Fixtures/xml/services1.xml'));
        $container->addResource($b = new FileResource(__DIR__.'/Fixtures/xml/services2.xml'));
        $resources = array();
        foreach ($container->getResources() as $resource) {
            if (false === strpos($resource'.php')) {
                $resources[] = $resource;
            }
        }
        $this->assertEquals(array($a$b), $resources'->getResources() returns an array of resources read for the current configuration');
        $this->assertSame($container$container->setResources(array()));
        $this->assertEquals(array(), $container->getResources());
    }

    public function testFileExists()
    {
        $container = new ContainerBuilder();
        $A = new ComposerResource();
        $a = new FileResource(__DIR__.'/Fixtures/xml/services1.xml');
        $b = new FileResource(__DIR__.'/Fixtures/xml/services2.xml');
        $c = new DirectoryResource($dir = dirname($b));

        $this->assertTrue($container->fileExists((string) $a&& $container->fileExists((string) $b&& $container->fileExists($dir));

        $resources = array();
        foreach ($container->getResources() as $resource) {
            if (false === strpos($resource'.php')) {
                $resources[] = $resource;
            }
        }

        $this->assertEquals(array($A$a$b$c), $resources'->getResources() returns an array of resources read for the current configuration');
    }

    public function testExtension()
    {
        $container = new ContainerBuilder();
        $container->setResourceTracking(false);

        $container->registerExtension($extension = new \ProjectExtension());
        $this->assertSame($container->getExtension('project'), $extension'->registerExtension() registers an extension');

        $this->{method_exists($this$_ = 'expectException') ? $_ : 'setExpectedException'}('LogicException');
        $container->getExtension('no_registered');
    }

    public function testRegisteredButNotLoadedExtension()
    {
        $extension = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface')->getMock();
        $extension->expects($this->once())->method('getAlias')->will($this->returnValue('project'));
        $extension->expects($this->never())->method('load');

        $container = new ContainerBuilder();
        $container->setResourceTracking(false);
        $container->registerExtension($extension);
        $container->compile();
    }

    public function testRegisteredAndLoadedExtension()
    {
        $extension = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface')->getMock();
        $extension->expects($this->exactly(2))->method('getAlias')->will($this->returnValue('project'));
        $extension->expects($this->once())->method('load')->with(array(array('foo' => 'bar')));

        $container = new ContainerBuilder();
        $container->setResourceTracking(false);
        $container->registerExtension($extension);
        $container->loadFromExtension('project'array('foo' => 'bar'));
        $container->compile();
    }

    public function testPrivateServiceUser()
    {
        $fooDefinition = new Definition('BarClass');
        $fooUserDefinition = new Definition('BarUserClass'array(new Reference('bar')));
        $container = new ContainerBuilder();
        $container->setResourceTracking(false);

        $fooDefinition->setPublic(false);

        $container->addDefinitions(array(
            'bar' => $fooDefinition,
            'bar_user' => $fooUserDefinition->setPublic(true),
        ));

        $container->compile();
        $this->assertInstanceOf('BarClass'$container->get('bar_user')->bar);
    }

    /** * @expectedException \BadMethodCallException */
    public function testThrowsExceptionWhenSetServiceOnACompiledContainer()
    {
        $container = new ContainerBuilder();
        $container->setResourceTracking(false);
        $container->register('a''stdClass')->setPublic(true);
        $container->compile();
        $container->set('a'new \stdClass());
    }

    public function testThrowsExceptionWhenAddServiceOnACompiledContainer()
    {
        $container = new ContainerBuilder();
        $container->compile();
        $container->set('a'$foo = new \stdClass());
        $this->assertSame($foo$container->get('a'));
    }

    public function testNoExceptionWhenSetSyntheticServiceOnACompiledContainer()
    {
        $container = new ContainerBuilder();
        $def = new Definition('stdClass');
        $def->setSynthetic(true)->setPublic(true);
        $container->setDefinition('a'$def);
        $container->compile();
        $container->set('a'$a = new \stdClass());
        $this->assertEquals($a$container->get('a'));
    }

    /** * @expectedException \BadMethodCallException */
    public function testThrowsExceptionWhenSetDefinitionOnACompiledContainer()
    {
        $container = new ContainerBuilder();
        $container->setResourceTracking(false);
        $container->compile();
        $container->setDefinition('a'new Definition());
    }

    public function testExtensionConfig()
    {
        $container = new ContainerBuilder();

        $configs = $container->getExtensionConfig('foo');
        $this->assertEmpty($configs);

        $first = array('foo' => 'bar');
        $container->prependExtensionConfig('foo'$first);
        $configs = $container->getExtensionConfig('foo');
        $this->assertEquals(array($first), $configs);

        $second = array('ding' => 'dong');
        $container->prependExtensionConfig('foo'$second);
        $configs = $container->getExtensionConfig('foo');
        $this->assertEquals(array($second$first), $configs);
    }

    public function testAbstractAlias()
    {
        $container = new ContainerBuilder();

        $abstract = new Definition('AbstractClass');
        $abstract->setAbstract(true)->setPublic(true);

        $container->setDefinition('abstract_service'$abstract);
        $container->setAlias('abstract_alias''abstract_service')->setPublic(true);

        $container->compile();

        $this->assertSame('abstract_service'(string) $container->getAlias('abstract_alias'));
    }

    public function testLazyLoadedService()
    {
        $loader = new ClosureLoader($container = new ContainerBuilder());
        $loader->load(function (ContainerBuilder $container) {
            $container->set('a'new \BazClass());
            $definition = new Definition('BazClass');
            $definition->setLazy(true);
            $definition->setPublic(true);
            $container->setDefinition('a'$definition);
        });

        $container->setResourceTracking(true);

        $container->compile();

        $r = new \ReflectionProperty($container'resources');
        $r->setAccessible(true);
        $resources = $r->getValue($container);

        $classInList = false;
        foreach ($resources as $resource) {
            if ('reflection.BazClass' === (string) $resource) {
                $classInList = true;
                break;
            }
        }

        $this->assertTrue($classInList);
    }

    public function testInlinedDefinitions()
    {
        $container = new ContainerBuilder();

        $definition = new Definition('BarClass');

        $container->register('bar_user''BarUserClass')
            ->addArgument($definition)
            ->setProperty('foo'$definition);

        $container->register('bar''BarClass')
            ->setProperty('foo'$definition)
            ->addMethodCall('setBaz'array($definition));

        $barUser = $container->get('bar_user');
        $bar = $container->get('bar');

        $this->assertSame($barUser->foo$barUser->bar);
        $this->assertSame($bar->foo$bar->getBaz());
        $this->assertNotSame($bar->foo$barUser->foo);
    }

    /** * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException * @expectedExceptionMessage Circular reference detected for service "app.test_class", path: "app.test_class -> App\TestClass -> app.test_class". */
    public function testThrowsCircularExceptionForCircularAliases()
    {
        $builder = new ContainerBuilder();

        $builder->setAliases(array(
            'foo' => new Alias('app.test_class'),
            'app.test_class' => new Alias('App\\TestClass'),
            'App\\TestClass' => new Alias('app.test_class'),
        ));

        $builder->findDefinition('foo');
    }

    public function testInitializePropertiesBeforeMethodCalls()
    {
        $container = new ContainerBuilder();
        $container->register('foo''stdClass');
        $container->register('bar''MethodCallClass')
            ->setPublic(true)
            ->setProperty('simple''bar')
            ->setProperty('complex'new Reference('foo'))
            ->addMethodCall('callMe');

        $container->compile();

        $this->assertTrue($container->get('bar')->callPassed(), '->compile() initializes properties before method calls');
    }

    public function testAutowiring()
    {
        $container = new ContainerBuilder();

        $container->register(A::class)->setPublic(true);
        $bDefinition = $container->register('b'__NAMESPACE__.'\B');
        $bDefinition->setAutowired(true);
        $bDefinition->setPublic(true);

        $container->compile();

        $this->assertEquals(A::class(string) $container->getDefinition('b')->getArgument(0));
    }

    public function testClassFromId()
    {
        $container = new ContainerBuilder();

        $unknown = $container->register('Acme\UnknownClass');
        $autoloadClass = $container->register(CaseSensitiveClass::class);
        $container->compile();

        $this->assertSame('Acme\UnknownClass'$unknown->getClass());
        $this->assertEquals(CaseSensitiveClass::class$autoloadClass->getClass());
    }

    /** * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException * @expectedExceptionMessage The definition for "DateTime" has no class attribute, and appears to reference a class or interface in the global namespace. */
    public function testNoClassFromGlobalNamespaceClassId()
    {
        $container = new ContainerBuilder();

        $definition = $container->register(\DateTime::class);
        $container->compile();
    }

    /** * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException * @expectedExceptionMessage The definition for "123_abc" has no class. */
    public function testNoClassFromNonClassId()
    {
        $container = new ContainerBuilder();

        $definition = $container->register('123_abc');
        $container->compile();
    }

    /** * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException * @expectedExceptionMessage The definition for "\foo" has no class. */
    public function testNoClassFromNsSeparatorId()
    {
        $container = new ContainerBuilder();

        $definition = $container->register('\\foo');
        $container->compile();
    }

    public function testServiceLocator()
    {
        $container = new ContainerBuilder();
        $container->register('foo_service'ServiceLocator::class)
            ->setPublic(true)
            ->addArgument(array(
                'bar' => new ServiceClosureArgument(new Reference('bar_service')),
                'baz' => new ServiceClosureArgument(new TypedReference('baz_service''stdClass')),
            ))
        ;
        $container->register('bar_service''stdClass')->setArguments(array(new Reference('baz_service')))->setPublic(true);
        $container->register('baz_service''stdClass')->setPublic(false);
        $container->compile();

        $this->assertInstanceOf(ServiceLocator::class$foo = $container->get('foo_service'));
        $this->assertSame($container->get('bar_service'), $foo->get('bar'));
    }

    public function testUninitializedReference()
    {
        $container = include __DIR__.'/Fixtures/containers/container_uninitialized_ref.php';
        $container->compile();

        $bar = $container->get('bar');

        $this->assertNull($bar->foo1);
        $this->assertNull($bar->foo2);
        $this->assertNull($bar->foo3);
        $this->assertNull($bar->closures[0]());
        $this->assertNull($bar->closures[1]());
        $this->assertNull($bar->closures[2]());
        $this->assertSame(array(), iterator_to_array($bar->iter));

        $container = include __DIR__.'/Fixtures/containers/container_uninitialized_ref.php';
        $container->compile();

        $container->get('foo1');
        $container->get('baz');

        $bar = $container->get('bar');

        $this->assertEquals(new \stdClass(), $bar->foo1);
        $this->assertNull($bar->foo2);
        $this->assertEquals(new \stdClass(), $bar->foo3);
        $this->assertEquals(new \stdClass(), $bar->closures[0]());
        $this->assertNull($bar->closures[1]());
        $this->assertEquals(new \stdClass(), $bar->closures[2]());
        $this->assertEquals(array('foo1' => new \stdClass(), 'foo3' => new \stdClass()), iterator_to_array($bar->iter));
    }

    /** * @dataProvider provideAlmostCircular */
    public function testAlmostCircular($visibility)
    {
        $container = include __DIR__.'/Fixtures/containers/container_almost_circular.php';

        $foo = $container->get('foo');
        $this->assertSame($foo$foo->bar->foobar->foo);

        $foo2 = $container->get('foo2');
        $this->assertSame($foo2$foo2->bar->foobar->foo);

        $this->assertSame(array(), (array) $container->get('foobar4'));

        $foo5 = $container->get('foo5');
        $this->assertSame($foo5$foo5->bar->foo);
    }

    public function provideAlmostCircular()
    {
        yield array('public');
        yield array('private');
    }

    public function testRegisterForAutoconfiguration()
    {
        $container = new ContainerBuilder();
        $childDefA = $container->registerForAutoconfiguration('AInterface');
        $childDefB = $container->registerForAutoconfiguration('BInterface');
        $this->assertSame(array('AInterface' => $childDefA'BInterface' => $childDefB), $container->getAutoconfiguredInstanceof());

        // when called multiple times, the same instance is returned
        $this->assertSame($childDefA$container->registerForAutoconfiguration('AInterface'));
    }

    /** * This test checks the trigger of a deprecation note and should not be removed in major releases. * * @group legacy * @expectedDeprecation The "foo" service is deprecated. You should stop using it, as it will soon be removed. */
    public function testPrivateServiceTriggersDeprecation()
    {
        $container = new ContainerBuilder();
        $container->register('foo''stdClass')
            ->setPublic(false)
            ->setDeprecated(true);
        $container->register('bar''stdClass')
            ->setPublic(true)
            ->setProperty('foo'new Reference('foo'));

        $container->compile();

        $container->get('bar');
    }

    /** * @group legacy * @expectedDeprecation Parameter names will be made case sensitive in Symfony 4.0. Using "FOO" instead of "foo" is deprecated since Symfony 3.4. */
    public function testParameterWithMixedCase()
    {
        $container = new ContainerBuilder(new ParameterBag(array('foo' => 'bar')));
        $container->register('foo''stdClass')
            ->setPublic(true)
            ->setProperty('foo''%FOO%');

        $container->compile();

        $this->assertSame('bar'$container->get('foo')->foo);
    }

    public function testArgumentsHaveHigherPriorityThanBindings()
    {
        $container = new ContainerBuilder();
        $container->register('class.via.bindings'CaseSensitiveClass::class)->setArguments(array(
            'via-bindings',
        ));
        $container->register('class.via.argument'CaseSensitiveClass::class)->setArguments(array(
            'via-argument',
        ));
        $container->register('foo'SimilarArgumentsDummy::class)->setPublic(true)->setBindings(array(
            CaseSensitiveClass::class => new Reference('class.via.bindings'),
            '$token' => '1234',
        ))->setArguments(array(
            '$class1' => new Reference('class.via.argument'),
        ));

        $this->assertSame(array('service_container''class.via.bindings''class.via.argument''foo''Psr\Container\ContainerInterface''Symfony\Component\DependencyInjection\ContainerInterface'), $container->getServiceIds());

        $container->compile();

        $this->assertSame('via-argument'$container->get('foo')->class1->identifier);
        $this->assertSame('via-bindings'$container->get('foo')->class2->identifier);
    }
}

class FooClass
{
}

class A
{
}

class B
{
    public function __construct(A $a)
    {
    }
}