Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ProductTest.php
1<?php
2
4
8
9class ProductTest extends BaseTest
10{
12 private static $factory;
14 private static $repository;
15
16 public static function setUpBeforeClass() : void
17 {
18 parent::setUpBeforeClass();
19 static::$factory = static::$container->make(Dependency::PRODUCT_FACTORY, ['iblockId' => 8]);
20 static::$repository = static::$container->make(Dependency::PRODUCT_REPOSITORY, ['iblockId' => 8]);
21 }
22
23 protected function setUp() : void
24 {
25 }
26
27 private function createNewProduct(): BaseProduct
28 {
29 return static::$factory->createEntity(static::$factory::PRODUCT);
30 }
31
32 private function loadExistingProduct(): BaseProduct
33 {
34 // ToDo mock it
35 return static::$repository->getEntityById(509);
36 }
37
38 // ToDo remove temporary test for qcachegrind profiler
39 public function testPerformanceOfCreation(): void
40 {
41 $products = [];
42
43 foreach (range(0, 100) as $item)
44 {
45 $products[] = $this->createNewProduct();
46 }
47
48 $this->assertCount(101, $products);
49 }
50
51 public function testChangeExistingProduct(): void
52 {
53 $product = $this->loadExistingProduct();
54
55 $previousValue = $product->getField('NAME');
56 $this->assertFalse($product->isChanged());
57
58 $product->setField('NAME', 'TEST');
59 $this->assertTrue($product->isChanged());
60
61 $product->setField('NAME', $previousValue);
62 $this->assertFalse($product->isChanged());
63 }
64
66 {
67 $product = $this->loadExistingProduct();
68
69 $product->setField('NAME', 'TEST');
70 $product->save();
71
72 $this->assertFalse($product->isChanged());
73 $this->assertEquals('TEST', $product->getField('NAME'));
74 }
75
77 {
78 $product = $this->loadExistingProduct();
79
80 foreach ($product->getSkuCollection() as $sku)
81 {
82 $sku->setField('NAME', 'TEST');
83 $this->assertTrue($sku->isChanged());
84 $this->assertEquals('TEST', $sku->getField('NAME'));
85 }
86
87 $this->assertTrue($product->isChanged());
88 $this->assertTrue($product->getSkuCollection()->isChanged());
89
90 $product->save();
91
92 $this->assertFalse($product->isChanged());
93 $this->assertFalse($product->getSkuCollection()->isChanged());
94
95 foreach ($product->getSkuCollection() as $sku)
96 {
97 $this->assertFalse($sku->isChanged());
98 $this->assertEquals('TEST', $sku->getField('NAME'));
99 }
100 }
101
103 {
104 $product = $this->loadExistingProduct();
105
106 foreach ($product->getSkuCollection() as $sku)
107 {
108 $sku->setField('NAME', 'TEST');
109 $sku->save();
110
111 $this->assertFalse($sku->isChanged());
112 $this->assertEquals('TEST', $sku->getField('NAME'));
113 }
114
115 $this->assertFalse($product->isChanged());
116 $this->assertFalse($product->getSkuCollection()->isChanged());
117 }
118}