1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Tests\Relations; |
4
|
|
|
|
5
|
|
|
use Nip\Records\Collections\Collection; |
6
|
|
|
use Nip\Records\RecordManager; |
7
|
|
|
use Nip\Records\Relations\HasAndBelongsToMany; |
8
|
|
|
use Nip\Records\Tests\Fixtures\Records\Books\Book; |
9
|
|
|
use Nip\Records\Tests\Fixtures\Records\Books\Books; |
10
|
|
|
use Nip\Records\Tests\Fixtures\Records\Shelves\Shelves; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class HasAndBelongsToManyTest |
14
|
|
|
* @package Nip\Records\Tests\Relations |
15
|
|
|
*/ |
16
|
|
|
class HasAndBelongsToManyTest extends \Nip\Records\Tests\AbstractTest |
17
|
|
|
{ |
18
|
|
|
public function testTableNameGeneration() |
19
|
|
|
{ |
20
|
|
|
$tableA = new RecordManager(); |
21
|
|
|
$tableA->setTable('tableA'); |
22
|
|
|
|
23
|
|
|
$tableB = new RecordManager(); |
24
|
|
|
$tableB->setTable('tableB'); |
25
|
|
|
|
26
|
|
|
$relation = new HasAndBelongsToMany(); |
27
|
|
|
$relation->setManager($tableA); |
28
|
|
|
$relation->setWith($tableB); |
29
|
|
|
self::assertEquals('tableA_tableB', $relation->getTable()); |
30
|
|
|
|
31
|
|
|
$relation = new HasAndBelongsToMany(); |
32
|
|
|
$relation->setManager($tableB); |
33
|
|
|
$relation->setWith($tableA); |
34
|
|
|
self::assertEquals('tableA_tableB', $relation->getTable()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testEmptyRecordLoadRelation() |
38
|
|
|
{ |
39
|
|
|
$record = new Book(); |
40
|
|
|
$record->setManager(Books::instance()); |
41
|
|
|
|
42
|
|
|
$relation = new HasAndBelongsToMany(); |
43
|
|
|
$relation->setWith(Shelves::instance()); |
44
|
|
|
$relation->setItem($record); |
45
|
|
|
$results = $relation->getResults(); |
46
|
|
|
self::assertInstanceOf(Collection::class, $results); |
47
|
|
|
self::assertCount(0, $results); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|