<commit_before><?php

namespace Collins\ShopApi\Test\Live;

use Collins\ShopApi\Model\ProductSearchResult;

/**
 * @group live
 */
class ProductSearchTest extends \Collins\ShopApi\Test\Live\AbstractShopApiLiveTest
{
    public function testProductSearchWithLimit()
    {
        $api = $this->getShopApi();
        $criteria = $this->getSearchCriteria();
        $criteria->setLimit(5);
        
        $result = $api->fetchProductSearch($criteria);
        
        $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\ProductSearchResult', $result);
        $this->assertCount(5, $result->getProducts()); 
        
        foreach($result->getProducts() as $product) {
            $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\Product', $product);
        }
    }
    
    public function testProductSearchWithId()
    {
        $api = $this->getShopApi();
        $criteria = $this->getSearchCriteria();
        $criteria->setLimit(5);
        
        $result = $api->fetchProductSearch($criteria);
        
        $productsArray = $result->getProducts();
        $product = $productsArray[0];
        
        $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\Product', $product);
        
        $resultProduct = $api->fetchProductsByIds(array($product->getId()));
        
        $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\ProductsResult', $resultProduct);
    }
    
    public function testProductSearchCategoryTree()
    {
        $api = $this->getShopApi();        
        $criteria = $this->getSearchCriteria();
        $criteria->setLimit(5);
        
        $result = $api->fetchProductSearch($criteria);

        $this->assertInternalType('array', $result->getCategoryTree());
    }
    
    public function testProductSearchCategory()
    {
        $api = $this->getShopApi();
        $criteria = $this->getSearchCriteria();
        $criteria->setLimit(5);
        
        $result = $api->fetchProductSearch($criteria);

        $this->assertInternalType('array', $result->getCategories());
    }    
    
    public function testProductSearchProductCount()
    {
        $api = $this->getShopApi();        
        $criteria = $this->getSearchCriteria();
        $criteria->setLimit(5);
        
        $result = $api->fetchProductSearch($criteria);

        $this->assertInternalType('int', $result->getProductCount());
        $this->assertCount(5, $result->getProducts());
    }

    public function testProductSearchProductBoosts()
    {
        $api = $this->getShopApi();
        $criteria = $this->getSearchCriteria()
            ->boostProducts(array(1))
            ->setLimit(2)
        ;

        $result = $api->fetchProductSearch($criteria);

        $this->assertInternalType('int', $result->getProductCount());
    }

    public function testProductSearchSort()
    {
        $shopApi = $this->getShopApi();

        // search products and sort
        $criteria = $shopApi->getProductSearchCriteria($this->getSessionId())
            ->sortBy(
                \Collins\ShopApi\Criteria\ProductSearchCriteria::SORT_TYPE_MOST_VIEWED
            )
        ;
        $productSearchResult = $shopApi->fetchProductSearch($criteria);
        $this->checkProductSearchResult($productSearchResult);
    }

    public function testProductSearchWithFacets()
    {
        $shopApi = $this->getShopApi();

        // search products and sort
        $criteria = $shopApi->getProductSearchCriteria($this->getSessionId())
            ->selectSales()
            ->selectPriceRanges()
            ->selectFacetsByGroupId(0, 10)
        ;
        $productSearchResult = $shopApi->fetchProductSearch($criteria);
        $this->checkProductSearchResult($productSearchResult);

        $saleFacets = $productSearchResult->getSaleCounts();
        $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\ProductSearchResult\\SaleCounts', $saleFacets);
        $this->assertInternalType('integer', $saleFacets->getProductCountTotal());
        $this->assertInternalType('integer', $saleFacets->getProductCountInSale());
        $this->assertInternalType('integer', $saleFacets->getProductCountNotInSale());

        $priceRanges = $productSearchResult->getPriceRanges();
        $this->assertInternalType('array', $priceRanges);

        $facets =  $productSearchResult->getFacets();
        $this->assertInternalType('array', $facets);
        $brandFacets = $facets[0];
        $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\ProductSearchResult\\FacetCounts', $brandFacets);
        $this->assertInternalType('integer', $brandFacets->getProductCountTotal());
        $this->assertEquals(0, $brandFacets->getGroupId());
        $facetCounts = $brandFacets->getFacetCounts();
        $this->assertInternalType('array', $facetCounts);
        $this->assertCount(10, $facetCounts);
        foreach ($facetCounts as $facetCount) {
            $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\ProductSearchResult\\FacetCount', $facetCount);
            $this->assertInternalType('integer', $facetCount->getProductCount());
            $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\Facet', $facetCount->getFacet());
        }
    }

    private function checkProduct(Product $product)
    {
        $this->assertObjectHasAttribute('id', $product);
        $this->assertObjectHasAttribute('name', $product);
    }

    private function checkProductSearchResult(ProductSearchResult $products)
    {
        foreach ($products as $product) {
            $this->checkProduct($product);
        }
    }    
}
<commit_msg>DC-817: add live tests for products by ean<commit_after><?php

namespace Collins\ShopApi\Test\Live;

use Collins\ShopApi\Model\ProductSearchResult;

/**
 * @group live
 */
class ProductSearchTest extends \Collins\ShopApi\Test\Live\AbstractShopApiLiveTest
{
    public function testProductSearchWithLimit()
    {
        $api = $this->getShopApi();
        $criteria = $this->getSearchCriteria();
        $criteria->setLimit(5);

        $result = $api->fetchProductSearch($criteria);

        $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\ProductSearchResult', $result);
        $this->assertCount(5, $result->getProducts());

        foreach($result->getProducts() as $product) {
            $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\Product', $product);
        }
    }

    public function testProductSearchWithId()
    {
        $api = $this->getShopApi();
        $criteria = $this->getSearchCriteria();
        $criteria->setLimit(5);

        $result = $api->fetchProductSearch($criteria);

        $productsArray = $result->getProducts();
        $product = $productsArray[0];

        $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\Product', $product);

        $resultProduct = $api->fetchProductsByIds(array($product->getId()));

        $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\ProductsResult', $resultProduct);
    }

    public function testProductSearchWithEANS()
    {
        $api = $this->getShopApi();

        $result = $api->fetchProductsByEans(['NO_VALID_EAN']);
        $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\ProductsEansResult', $result);

        $this->assertCount(1, $result->getErrors());
    }

    public function testProductSearchCategoryTree()
    {
        $api = $this->getShopApi();
        $criteria = $this->getSearchCriteria();
        $criteria->setLimit(5);

        $result = $api->fetchProductSearch($criteria);

        $this->assertInternalType('array', $result->getCategoryTree());
    }

    public function testProductSearchCategory()
    {
        $api = $this->getShopApi();
        $criteria = $this->getSearchCriteria();
        $criteria->setLimit(5);

        $result = $api->fetchProductSearch($criteria);

        $this->assertInternalType('array', $result->getCategories());
    }

    public function testProductSearchProductCount()
    {
        $api = $this->getShopApi();
        $criteria = $this->getSearchCriteria();
        $criteria->setLimit(5);

        $result = $api->fetchProductSearch($criteria);

        $this->assertInternalType('int', $result->getProductCount());
        $this->assertCount(5, $result->getProducts());
    }

    public function testProductSearchProductBoosts()
    {
        $api = $this->getShopApi();
        $criteria = $this->getSearchCriteria()
            ->boostProducts(array(1))
            ->setLimit(2)
        ;

        $result = $api->fetchProductSearch($criteria);

        $this->assertInternalType('int', $result->getProductCount());
    }

    public function testProductSearchSort()
    {
        $shopApi = $this->getShopApi();

        // search products and sort
        $criteria = $shopApi->getProductSearchCriteria($this->getSessionId())
            ->sortBy(
                \Collins\ShopApi\Criteria\ProductSearchCriteria::SORT_TYPE_MOST_VIEWED
            )
        ;
        $productSearchResult = $shopApi->fetchProductSearch($criteria);
        $this->checkProductSearchResult($productSearchResult);
    }

    public function testProductSearchWithFacets()
    {
        $shopApi = $this->getShopApi();

        // search products and sort
        $criteria = $shopApi->getProductSearchCriteria($this->getSessionId())
            ->selectSales()
            ->selectPriceRanges()
            ->selectFacetsByGroupId(0, 10)
        ;
        $productSearchResult = $shopApi->fetchProductSearch($criteria);
        $this->checkProductSearchResult($productSearchResult);

        $saleFacets = $productSearchResult->getSaleCounts();
        $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\ProductSearchResult\\SaleCounts', $saleFacets);
        $this->assertInternalType('integer', $saleFacets->getProductCountTotal());
        $this->assertInternalType('integer', $saleFacets->getProductCountInSale());
        $this->assertInternalType('integer', $saleFacets->getProductCountNotInSale());

        $priceRanges = $productSearchResult->getPriceRanges();
        $this->assertInternalType('array', $priceRanges);

        $facets =  $productSearchResult->getFacets();
        $this->assertInternalType('array', $facets);
        $brandFacets = $facets[0];
        $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\ProductSearchResult\\FacetCounts', $brandFacets);
        $this->assertInternalType('integer', $brandFacets->getProductCountTotal());
        $this->assertEquals(0, $brandFacets->getGroupId());
        $facetCounts = $brandFacets->getFacetCounts();
        $this->assertInternalType('array', $facetCounts);
        $this->assertCount(10, $facetCounts);
        foreach ($facetCounts as $facetCount) {
            $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\ProductSearchResult\\FacetCount', $facetCount);
            $this->assertInternalType('integer', $facetCount->getProductCount());
            $this->assertInstanceOf('\\Collins\\ShopApi\\Model\\Facet', $facetCount->getFacet());
        }
    }

    private function checkProduct(Product $product)
    {
        $this->assertObjectHasAttribute('id', $product);
        $this->assertObjectHasAttribute('name', $product);
    }

    private function checkProductSearchResult(ProductSearchResult $products)
    {
        foreach ($products as $product) {
            $this->checkProduct($product);
        }
    }
}

{'fraction_non_alphanumeric': 0.15159010600706713, 'fraction_numerical': 0.0033568904593639574, 'mean_word_length': 2.9322681486627302, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 182, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '342813', 'n_tokens_mistral': 3222, 'n_tokens_neox': 3116, 'n_words': 406}