<commit_before><?php

namespace SimplyTestable\ApiBundle\Tests\Controller;

use SimplyTestable\ApiBundle\Tests\Controller\BaseControllerJsonTestCase;

class GetPlanActionTest extends BaseControllerJsonTestCase {
    
    const DEFAULT_TRIAL_PERIOD = 30;
    
    public static function setUpBeforeClass() {
        self::setupDatabaseIfNotExists();
    }    

    public function testForUserWithBasicPlan() {
        $email = '<EMAIL>';
        $password = '<PASSWORD>';
        
        $user = $this->createAndFindUser($email, $password);        
        $this->getUserService()->setUser($user);

        $responseObject = json_decode($this->getUserController('getPlanAction')->getPlanAction()->getContent());

        $this->assertEquals('basic', $responseObject->name);         
    }
    
    public function testForUserWithPremiumPlan() {
        $email = '<EMAIL>';
        $password = '<PASSWORD>';
        
        $user = $this->createAndFindUser($email, $password);        
        $this->getUserService()->setUser($user);
        
        $this->getUserAccountPlanService()->subscribe($user, $this->getAccountPlanService()->find('personal'));

        $responseObject = json_decode($this->getUserController('getPlanAction')->getPlanAction()->getContent());
        
        $this->assertEquals('personal', $responseObject->name);
        $this->assertEquals('month', $responseObject->summary->interval);
        $this->assertEquals(900, $responseObject->summary->amount);
        $this->assertEquals(5000, $responseObject->credits->limit);
        $this->assertEquals(0, $responseObject->credits->used);
        $this->assertEquals(50, $responseObject->urls_per_job);
        
        $this->assertEquals('trialing', $responseObject->summary->status);        
        $this->assertEquals(30, $responseObject->summary->trial_period_days);        
        $this->assertInternalType('int', $responseObject->summary->current_period_end);
        $this->assertInternalType('int', $responseObject->summary->trial_end);       
    }   
    
    public function testRetrieveForUserWhereIsActiveIsZero() {
        $email = '<EMAIL>';
        $password = '<PASSWORD>';
        
        $user = $this->createAndFindUser($email, $password);        
        $this->getUserService()->setUser($user);
        
        $this->getUserAccountPlanService()->deactivateAllForUser($user);

        $responseObject = json_decode($this->getUserController('getPlanAction')->getPlanAction()->getContent());

        $this->assertEquals('basic', $responseObject->name);             
    }
    
    public function testRetrieveForUserWhereIsActiveIsZeroAndUserHasMany() {
        $email = '<EMAIL>';
        $password = '<PASSWORD>';
        
        $user = $this->createAndFindUser($email, $password);        
        $this->getUserService()->setUser($user);
        
        $this->getUserAccountPlanService()->subscribe($user, $this->getAccountPlanService()->find('personal'));
        $this->getUserAccountPlanService()->subscribe($user, $this->getAccountPlanService()->find('agency'));
        
        $this->getUserAccountPlanService()->deactivateAllForUser($user);

        $responseObject = json_decode($this->getUserController('getPlanAction')->getPlanAction()->getContent());

        $this->assertEquals('agency', $responseObject->name);             
    }  
    
    
    public function testStartTrialPeriodForUserWithBasicPlan() {
        $email = '<EMAIL>';
        $password = '<PASSWORD>';
        
        $user = $this->createAndFindUser($email, $password);        
        $this->getUserService()->setUser($user);

        $responseObject = json_decode($this->getUserController('getPlanAction')->getPlanAction()->getContent());
        $this->assertEquals(self::DEFAULT_TRIAL_PERIOD, $responseObject->start_trial_period);         
    }    
    
    public function testStartTrialPeriodForUserWithPartExpiredPremiumTrial() {
        $trialDaysRemaining = rand(1, self::DEFAULT_TRIAL_PERIOD);
        $email = '<EMAIL>';
        $password = '<PASSWORD>';
        
        $user = $this->createAndFindUser($email, $password);
        $this->getUserService()->setUser($user);       
        
        // Mock the fact that the Stripe customer.subscription.trial_end is
        // $trialDaysPassed days from now      
        $this->getStripeService()->addResponseData('getCustomer', array(
            'subscription' => array(
                'trial_end' => time() + (86400 * $trialDaysRemaining)
            )
        )); 
        
        $this->getUserAccountPlanSubscriptionController('subscribeAction')->subscribeAction($email, 'personal');
        
        $responseObject = json_decode($this->getUserController('getPlanAction')->getPlanAction()->getContent());        
        $this->assertEquals($trialDaysRemaining, $responseObject->start_trial_period);                
    }  
    
    
    public function testStartTrialPeriodForUserWithPartExpiredPremiumTrialBackOnBasic() {
        $trialDaysRemaining = rand(1, self::DEFAULT_TRIAL_PERIOD);
        $email = '<EMAIL>';
        $password = '<PASSWORD>';
        <commit_msg>Fixed plan transition in test<commit_after><?php

namespace SimplyTestable\ApiBundle\Tests\Controller;

use SimplyTestable\ApiBundle\Tests\Controller\BaseControllerJsonTestCase;

class GetPlanActionTest extends BaseControllerJsonTestCase {
    
    const DEFAULT_TRIAL_PERIOD = 30;
    
    public static function setUpBeforeClass() {
        self::setupDatabaseIfNotExists();
    }    

    public function testForUserWithBasicPlan() {
        $email = '<EMAIL>';
        $password = '<PASSWORD>';
        
        $user = $this->createAndFindUser($email, $password);        
        $this->getUserService()->setUser($user);

        $responseObject = json_decode($this->getUserController('getPlanAction')->getPlanAction()->getContent());

        $this->assertEquals('basic', $responseObject->name);         
    }
    
    public function testForUserWithPremiumPlan() {
        $email = '<EMAIL>';
        $password = '<PASSWORD>';
        
        $user = $this->createAndFindUser($email, $password);        
        $this->getUserService()->setUser($user);
        
        $this->getUserAccountPlanService()->subscribe($user, $this->getAccountPlanService()->find('personal'));

        $responseObject = json_decode($this->getUserController('getPlanAction')->getPlanAction()->getContent());
        
        $this->assertEquals('personal', $responseObject->name);
        $this->assertEquals('month', $responseObject->summary->interval);
        $this->assertEquals(900, $responseObject->summary->amount);
        $this->assertEquals(5000, $responseObject->credits->limit);
        $this->assertEquals(0, $responseObject->credits->used);
        $this->assertEquals(50, $responseObject->urls_per_job);
        
        $this->assertEquals('trialing', $responseObject->summary->status);        
        $this->assertEquals(30, $responseObject->summary->trial_period_days);        
        $this->assertInternalType('int', $responseObject->summary->current_period_end);
        $this->assertInternalType('int', $responseObject->summary->trial_end);       
    }   
    
    public function testRetrieveForUserWhereIsActiveIsZero() {
        $email = '<EMAIL>';
        $password = '<PASSWORD>';
        
        $user = $this->createAndFindUser($email, $password);        
        $this->getUserService()->setUser($user);
        
        $this->getUserAccountPlanService()->deactivateAllForUser($user);

        $responseObject = json_decode($this->getUserController('getPlanAction')->getPlanAction()->getContent());

        $this->assertEquals('basic', $responseObject->name);             
    }
    
    public function testRetrieveForUserWhereIsActiveIsZeroAndUserHasMany() {
        $email = '<EMAIL>';
        $password = '<PASSWORD>';
        
        $user = $this->createAndFindUser($email, $password);        
        $this->getUserService()->setUser($user);
        
        $this->getUserAccountPlanService()->subscribe($user, $this->getAccountPlanService()->find('personal'));
        $this->getUserAccountPlanService()->subscribe($user, $this->getAccountPlanService()->find('agency'));
        
        $this->getUserAccountPlanService()->deactivateAllForUser($user);

        $responseObject = json_decode($this->getUserController('getPlanAction')->getPlanAction()->getContent());

        $this->assertEquals('agency', $responseObject->name);             
    }  
    
    
    public function testStartTrialPeriodForUserWithBasicPlan() {
        $email = '<EMAIL>';
        $password = '<PASSWORD>';
        
        $user = $this->createAndFindUser($email, $password);        
        $this->getUserService()->setUser($user);

        $responseObject = json_decode($this->getUserController('getPlanAction')->getPlanAction()->getContent());
        $this->assertEquals(self::DEFAULT_TRIAL_PERIOD, $responseObject->start_trial_period);         
    }    
    
    public function testStartTrialPeriodForUserWithPartExpiredPremiumTrial() {
        $trialDaysRemaining = rand(1, self::DEFAULT_TRIAL_PERIOD);
        $email = '<EMAIL>';
        $password = '<PASSWORD>';
        
        $user = $this->createAndFindUser($email, $password);
        $this->getUserService()->setUser($user);       
        
        $this->getUserAccountPlanSubscriptionController('subscribeAction')->subscribeAction($email, 'personal');
        
        // Mock the fact that the Stripe customer.subscription.trial_end is
        // $trialDaysPassed days from now      
        $this->getStripeService()->addResponseData('getCustomer', array(
            'subscription' => array(
                'trial_end' => time() + (86400 * $trialDaysRemaining)
            )
        ));        
        
        $this->getUserAccountPlanSubscriptionController('subscribeAction')->subscribeAction($email, 'agency');
        
        $responseObject = json_decode($this->getUserController('getPlanAction')->getPlanAction()->getContent());
        $this->assertEquals($trialDaysRemaining, $responseObject->start_trial_period);                
    }  
    
    
    public function testStartTrialPeriodForUserWithPartExpiredPremiumTrialBackOnBasic() {
        $trialDaysRemaining = rand(1, self::DEFAULT_TRIAL_PERIOD);
        $email = '<EMAIL>';
        $password = '<PASSWORD>';
        
{'fraction_non_alphanumeric': 0.14558724671084222, 'fraction_numerical': 0.004033419763756842, 'mean_word_length': 3.037999224505622, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 225, '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': '2655228', 'n_tokens_mistral': 2800, 'n_tokens_neox': 2710, 'n_words': 342}