BindingKeyTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * Integration tests for using the bindingKey in associations
  19. */
  20. class BindingKeyTest extends TestCase
  21. {
  22. /**
  23. * Fixture to be used
  24. *
  25. * @var array
  26. */
  27. public $fixtures = [
  28. 'core.AuthUsers',
  29. 'core.SiteAuthors',
  30. 'core.Users',
  31. ];
  32. /**
  33. * Data provider for the two types of strategies BelongsTo and HasOne implements
  34. *
  35. * @return array
  36. */
  37. public function strategiesProviderJoinable()
  38. {
  39. return [['join'], ['select']];
  40. }
  41. /**
  42. * Data provider for the two types of strategies HasMany and BelongsToMany implements
  43. *
  44. * @return array
  45. */
  46. public function strategiesProviderExternal()
  47. {
  48. return [['subquery'], ['select']];
  49. }
  50. /**
  51. * Tests that bindingKey can be used in belongsTo associations
  52. *
  53. * @dataProvider strategiesProviderJoinable
  54. * @return void
  55. */
  56. public function testBelongsto($strategy)
  57. {
  58. $users = $this->getTableLocator()->get('Users');
  59. $users->belongsTo('AuthUsers', [
  60. 'bindingKey' => 'username',
  61. 'foreignKey' => 'username',
  62. 'strategy' => $strategy,
  63. ]);
  64. $result = $users->find()
  65. ->contain(['AuthUsers']);
  66. $expected = ['mariano', 'nate', 'larry', 'garrett'];
  67. $expected = array_combine($expected, $expected);
  68. $this->assertEquals(
  69. $expected,
  70. $result->combine('username', 'auth_user.username')->toArray()
  71. );
  72. $expected = [1 => 1, 2 => 5, 3 => 2, 4 => 4];
  73. $this->assertEquals(
  74. $expected,
  75. $result->combine('id', 'auth_user.id')->toArray()
  76. );
  77. }
  78. /**
  79. * Tests that bindingKey can be used in hasOne associations
  80. *
  81. * @dataProvider strategiesProviderJoinable
  82. * @return void
  83. */
  84. public function testHasOne($strategy)
  85. {
  86. $users = $this->getTableLocator()->get('Users');
  87. $users->hasOne('SiteAuthors', [
  88. 'bindingKey' => 'username',
  89. 'foreignKey' => 'name',
  90. 'strategy' => $strategy,
  91. ]);
  92. $users->updateAll(['username' => 'jose'], ['username' => 'garrett']);
  93. $result = $users->find()
  94. ->contain(['SiteAuthors'])
  95. ->where(['username' => 'jose'])
  96. ->first();
  97. $this->assertEquals(3, $result->site_author->id);
  98. }
  99. /**
  100. * Tests that bindingKey can be used in hasOne associations
  101. *
  102. * @dataProvider strategiesProviderExternal
  103. * @return void
  104. */
  105. public function testHasMany($strategy)
  106. {
  107. $users = $this->getTableLocator()->get('Users');
  108. $authors = $users->hasMany('SiteAuthors', [
  109. 'bindingKey' => 'username',
  110. 'foreignKey' => 'name',
  111. 'strategy' => $strategy,
  112. ]);
  113. $authors->updateAll(['name' => 'garrett'], ['id >' => 2]);
  114. $result = $users->find()
  115. ->contain(['SiteAuthors'])
  116. ->where(['username' => 'garrett']);
  117. $expected = [3, 4];
  118. $result = $result->extract('site_authors.{*}.id')->toList();
  119. $this->assertEquals($expected, $result);
  120. }
  121. }