BindingKeyTest.php 3.8 KB

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